Displaying Matplotlib Chart (using Pandas AI)

Hello everyone,
I am building a Chatbot based on Panda AI, that would “analyse data” and “create plots” according to the data and prompt given by the user.
for that i use this code :

file = st.file_uploader(label="None", type=["xls", "xlsx"])
df = pd.read_excel(io=file, sheet_name=0, header=0)
prompt = st.text_input("what's your question ?")
with st.spinner("Processing..."):
    llm = OpenAI(model="gpt-3.5-turbo")
    agent = Agent([df], config={"llm": llm}, memory_size=10)
    response = agent.chat(prompt) 

As you can see, i have used “matplotlib.use(backend=“TkAgg”)” to handle chart renderings when user ask for a chart.

But here is the problem i have encountered :
When i run the App locally, everything goes well, i have charts and responses as the user Ask.
But when i have tried to host the app, i couldnt have the charts, i get this Error :

  File "/home/appuser/venv/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
  File "/home/appuser/venv/lib/python3.9/site-packages/matplotlib/pyplot.py", line 342, in switch_backend
    module = importlib.import_module(cbook._backend_module_name(newbackend))
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/appuser/venv/lib/python3.9/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
  File "/home/appuser/venv/lib/python3.9/site-packages/matplotlib/backends/_backend_tk.py", line 9, in <module>
    import tkinter as tk
  File "/usr/local/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

Can anyone help me to handle this issue?
Is there a way, to have the matplotlib figure poping even after hosting the app ?. if not how can i display the chart “response” direclty on streamlit (may be using st.pyplot ?)
Thank you all !

Hi @Snow_Yoo

According to the error message it seems that Tk is not recognized and may need to be installed in your app deployment

You can specify tk in your packages.txt file (for packages that you’d like the Ubuntu OS to install.

Hope this helps!

1 Like

so i just have to create a file “package.txt” and write in it “tk” ?
and is this could make the app have the same behavior as working locally ?, i mean, after the user asks for a chart, a figure would pop in a new window ?

Thank you for your help !

I don’t think that’s going to work. How about just changing the matplotlib backend to something non-interactive (Backends — Matplotlib 3.8.1 documentation) and then present the plot to the user using st.pyplot.

1 Like