Open Emad-Eldin-G opened 1 month ago
Hey @Emad-Eldin-G! I'm Dosu, your friendly bot here to help you solve bugs, answer questions, and become a contributor. I'm here to assist you while you wait for a human maintainer. Thanks for reporting the issue! I'll take a look at it and get back to you shortly.
@jonbiemond, is there a way to save the charts without opening it automatically, save_charts calls plt.show(), acting like open_charts, which is confusing
Have you tried setting open_charts
to False
?
Agent(df, config={"verbose": True, "llm": llm, "save_charts": True, "open_charts": False, "enable_cache": True})
It looks like the open_charts
parameter is not a valid configuration option in the PandasAI library. However, you can save charts without opening them by specifying a custom save path. Hereβs an updated version of your code:
import os
import pandas as pd
from pandasai import Agent
from openai import OpenAI
from pandasai.helpers import path
def test():
os.environ["PANDASAI_API_KEY"] = os.environ.get("PANDASAI_API_KEY")
llm = OpenAI(api_token=os.environ.get("OPENAI_API_KEY"))
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]
})
try:
user_defined_path = path.find_project_root()
except ValueError:
user_defined_path = os.getcwd()
user_defined_path = os.path.join(user_defined_path, "exports", "charts")
pAI = Agent(
df,
config={
"verbose": True,
"llm": llm,
"save_charts": True,
"save_charts_path": user_defined_path,
"enable_cache": True,
},
)
llm_analysis_response = pAI.chat("Create a plot for GDP in relation to happiness")
return llm_analysis_response
test()
This code sets a custom path for saving charts, ensuring they are saved without being displayed automatically. This should work well with applications like Streamlit. Let me know if you have any further questions!
To continue the conversation, mention @dosu.
The bot solution is completely incorrect, the image is till opened automatically (@dosu)
@jonbiemond your solution is correct, can I add this in the documentation somewhere? As it causes a real problem for streamlit applications and other apps that integrate pandasai.
@Emad-Eldin-G Glad to hear that worked!
The open_charts
parameter is listed in the Config section on the Getting Started page of the docs. Is there somewhere you think it might be also be helpful?
Yes, you can add information to the documentation about setting open_charts
to False
to prevent charts from opening automatically when save_charts
is set to True
in the PandasAI library. The relevant section in the documentation already mentions the open_charts
setting:
- `open_charts`: whether to open the chart during parsing of the response from the LLM. Defaults to `True`. You can completely disable displaying of charts by setting this option to `False`.
You can add an example to make it clearer:
import os
import pandas as pd
from pandasai import Agent
from openai import OpenAI
def test():
os.environ["PANDASAI_API_KEY"] = os.environ.get("PANDASAI_API_KEY")
llm = OpenAI(api_token=os.environ.get("OPENAI_API_KEY"))
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]
})
pAI = Agent(df, config={"verbose": True, "llm": llm, "save_charts": True, "open_charts": False, "enable_cache": True})
llm_analysis_response = pAI.chat("Create a plot for GDP in relation to happiness")
return llm_analysis_response
test()
Additionally, you can refer to the default configuration settings in the pandasai.json
file for more context:
{
"save_logs": true,
"verbose": false,
"enforce_privacy": false,
"enable_cache": true,
"use_error_correction_framework": true,
"max_retries": 3,
"open_charts": true,
"save_charts": false,
"save_charts_path": "exports/charts",
"custom_whitelisted_dependencies": [],
"llm": "BambooLLM",
"llm_options": null
}
This shows the default values for various settings, including open_charts
and save_charts_path
.
By adding this information to the documentation, it will help others who face similar issues, especially when integrating PandasAI with Streamlit or other applications.
@gventuri, could you help with this?
To continue the conversation, mention @dosu.
System Info
Python 3.11.7 PandasAI 2.0.37 Pandas 1.5.3
π Describe the bug
The above code should create the chart and save it to the default path (and it does) However, it opens the chart automatically, similar to what open_charts does, which causes problems when integrating pandasai in applications like streamlit for example.