pandas-ai
pandas-ai copied to clipboard
Streamlit application is not displaying charts in upgraded pandasai version
System Info
pandasai version: 1.5.17 Python: 3.11
This is my llm config:
user_defined_path = os.getcwd()
df = SmartDataframe(pd.DataFrame(gsk), config={"save_charts": True,"save_charts_path": user_defined_path,"llm": llm, "verbose": True, "response_parser": StreamlitResponse})
Now all my charts/visuals are storing in user defined path. It is not displaying in streamlit. I like to see my chart both in streamlit as well as save to the directory. Is it possible?
Thank you for all your help.
🐛 Describe the bug
I am using same config in older versions of pandasai and new. Older versions are displaying plots in streamlit.
Hi @mraguth ,
I looked into this and was able to replicate the issue.
@gventuri I looked through the code and realized that in StreamlitResponse we don't have any method to plot the charts/images for the latest version. However, if we go back to earlier versions of PandasAI, we can see those instructions are there.
If we update our current code with some of the earlier methods, we can replicate the earlier results. However is there any reason, those functionalities were dropped from the code ?
In case we require these functionalities with suggested changes, let me know, and I'll fix this issue.
@gventuri @dudesparsh I find the functionalities that allow us to add images to our PowerPoint presentations on Streamlit very valuable. It was easy to use them before, but now it seems difficult since we have to go to the local drive and get the .png file. I believe that upgrades should make our lives easier, not more complicated. I would appreciate it if we could add those functionalities back if possible. Thank you for your feedback.
@dudesparsh I believe this is actually a regression. We recently changed the behavior so that we have a unified way to handle the charts. In fact, given that many people are implementing it as part of backend frameworks like "flask" or "fast-api", trying to open the chart doesn't make sense. During the process, however, the streamlit parser might have been broken.
@mraguth you should be able to visualize the chart with st.image(path) in the meanwhile, we'll try to fix the issue asap
@gventuri Thank you for your feedback and looking forward to.
Please help with this fix...
I tested with pandas ai v2, still not working.
@DataScientistTX please try to use st.image(response)
, that should work. We're releasing a streamlit repo soon tho, so you'll be able to take inspiration from there!
For those of us who prefer st.pyplot
over st.image
, the following alternative solution works for me:
- Place a file called matplotlibrc.txt at ~/.matplotlib/, where the file is downloaded from https://matplotlib.org/1.2.1/users/customizing.html#matplotlibrc-sample
- Replace
backend : GTKAgg
in matplotlibrc.txt withecho "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
, as suggested by https://docs.streamlit.io/develop/api-reference/charts/st.pyplot - Then, to display the last figure generated:
fig_to_plot = plt.gcf()
st.pyplot(fig = fig_to_plot)
where plt
denotes matplotlib.pyplot
.
(Alternatively, to display all figures generated, just iterate over plt.get_fignums()
).
I prefer st.pyplot
over st.image
only because st.image
often crops out lengthy labels.
Please help with this fix...