Unable to display plots in a chat with CSV app


I am making a chat with CSV chatbot where we can upload a csv and talk with it it works okay in chat secanrios but when I ask it to plot a graph it doesn’t show the plot on the GUI

Steps to reproduce

Code snippet:

agent = create_pandas_dataframe_agent(
        OpenAI(temperature=0), df , verbose=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
        handle_parsing_errors=True, memory=memory)

if prompt := st.chat_input("Ask a question about your CSV: "):
    # st.chat_message("user").write(prompt)
#     # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
#     # Display user message in chat message container
    with st.chat_message("user"):

    with st.chat_message("assistant"):
        st_callback = StreamlitCallbackHandler(st.container())
        response = agent.run(prompt, callbacks=[st_callback])
        st.session_state.messages.append({"role": "assistant", "content": response})

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

SO when I ask a question for exaple plot a histogram it doesn’t show the hisogram on the GUI in my browser, I know i can use st.pyplot but I want to have the output in a chat window like chatgpt

Actual behavior:

Instead the output is shown as “A histogram of the year column is plotted.”

Debug info

  • Streamlit version: streamlit==1.26.0
  • Python version: 3.11
  • Using Virtual env
  • OS version: Mac OS
  • Browser version: CHrome

Additional information

If needed, add any other context about the problem here.

Hi @Shounak_Daptardar,

Thanks for posting! You might need to add more condition statements to check if a user asked for a graph/chart then display the chart using st.pyplot.

Can you share a screenshot of an example output for when you ask it to plot a graph?

Looks like you are trying to display chat messages in a chat format but this package does not support displaying plots inside chat messages, you can use st.plotly_chart outside the chat message container, below the chat input widget or you can use st.chat_message
without the streamlit_chat package, and insert any streamlit element into the returned container using with notation

I am looking for something where when I ask a question suppose “Plot a chart of certain column” then I want the reply from the assistant to be a “Chart/vis of that column”.

have the same question…

I think somewhere you need to implement streamlit plot functionalities. Agents, under the hood solving the data frame operations. but no where they incorporate streamlit plotting functionality . However PandasAI library does that by including streamlit functionalities in form of middleware (if I’m not wrong!)

here’s an example you can try out for testing, but for this one needs to ensure that the initialised data frame is assigned as “df” ( also, you need to add conditional plots - which is not at all elegant way to do )

if "bar" in response_dict:
    data = response_dict["bar"]
        df_data = {
            col: [x[i] if isinstance(x, list) else x for x in data["data"]]
            for i, col in enumerate(data["columns"])
        df = pd.DataFrame(df_data)
        if "Products" in df.columns:
            df.set_index("Products", inplace=True)
    except ValueError:
        print(f"Couldn't create DataFrame from data: {data}")

I would rather take an alternative approach in solving it by just implementing own prompts and executing fruther. ( drawing inspiration from PandasAI ) . Here’s one of the app I built and demonstrated -

Deployed App link . Hope this helps :slight_smile: