How do I display generated charts in streamlit? #264
Replies: 4 comments 3 replies
-
@kneelesh48 what happens if, for example, you ask to plot a chart? |
Beta Was this translation helpful? Give feedback.
-
The chart is not displayed in streamlit web app and I get a message in terminal that there is no display available. I think this is common behaviour if you try to plot a matplotlib chart in a .py file. This can be fixed by returning the plt object. However nothing is returned on calling pandas_ai.run() when the prompt asks to create a chart but when it's supposed to return a dataframe then a dataframe is returned so I believe this behavior can be fixed. |
Beta Was this translation helpful? Give feedback.
-
@gventuri still doesn't seem to be working. Here's the code I used if you want to give it a try. The first prompt displays in streamlit, but the second doesn't import os
import pandas as pd
import streamlit as st
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
from pandasai.middlewares.streamlit import StreamlitMiddleware
from dotenv import load_dotenv
load_dotenv()
llm = OpenAI(api_token=os.environ["OPENAI_API_TOKEN"])
pandas_ai = PandasAI(llm, middlewares=[StreamlitMiddleware()], verbose=True, enable_cache=True)
df = pd.DataFrame({
"country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
"gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})
with st.expander("Show DataFrame"):
st.dataframe(df.head())
prompt = 'Which are the 5 happiest countries?'
# prompt = 'Plot a bar chart of gdp and happines index of each country'
prompt = st.text_input("Enter prompt", value=prompt)
response = pandas_ai(df, prompt=prompt)
st.write(response) However adding this piece of code displays the charts, but only one of them if there are multiple if plt.get_fignums():
fig = plt.gcf()
st.pyplot(fig) |
Beta Was this translation helpful? Give feedback.
-
Yes, it started working as of I don't need to use the below code anymore if plt.get_fignums():
fig = plt.gcf()
st.pyplot(fig)``` |
Beta Was this translation helpful? Give feedback.
-
Generating charts works fine with jupyter notebooks but since pandas_ai.run() doesn't return anything, I'm not able to display the charts in streamlit.
Any help would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions