I can draw it in the chat using st.bar_chart([1,5,3,2,7]), but it is not added to the session state list, therefore, when i write a new message, this graph disappears
How should i write a bar chart to the session state? I have tried st.session_state[“messages”].append(st.bar_chart([1,5,3,2,7])), but it does not work
To include a bar chart in the chat interface, you’ll need to add a separate field, such as 'bar_chart', in your message. Then, use an if statement to check if that field exists and render the chart accordingly.
if 'messages' not in st.session_state:
st.session_state.messages = [
{'role': 'assistant', 'content': 'Hello, human!', 'bar_chart': np.random.randn(30, 3)}
]
for message in st.session_state.messages:
if message['role'] != 'system': # Skip system messages
with st.chat_message(message['role']):
st.markdown(message['content'])
# Check if the message includes a bar chart
if 'bar_chart' in message:
st.bar_chart(message['bar_chart'])
streamlit.errors.StreamlitAPIException: to_dict() is not a valid Streamlit command.
I have debugged the problem and it seems to be in this sentence st.altair_chart(message[‘altair_chart’])
for message in st.session_state["messages"]:
with st.chat_message(message["role"]):
if 'altair_chart' in message:
st.altair_chart(message['altair_chart'])
I don’t see anything here suggesting an attempt to print a chart. And it is unclear to me how this snippet is related to an altair chart. Also I don’t understand what you mean by “I added to altair chart the numpy object”. I don’t even know which line of code triggered the error let alone what the call stack is.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.