I have two containers in an app. The first container has a button. When user clicks the button, the second container should show a message. But I get this error:
Bad message format
i[o] is undefined
Steps to reproduce
Code snippet:
import streamlit as st
st.title('Test Page')
# first container
c1 = st.container()
# second container
c2 = st.container()
def show_msg():
c2.write('You clicked Hello!')
c1.button(label='Hello', on_click=show_msg)
Debug info
Streamlit version: Streamlit, version 1.18.1
Python version: Python 3.10.9
Using Conda
OS version: Ubuntu 22.04
Browser version: Firefox 110.0
Additional Info:
It works if c2 is defined before c1, but the message appears before the button.
Paraphrasing Ken’s comments on some issues: This is unintuitive behavior. A part of me thinks this is expected behavior (with a bad error message). Callbacks were designed to manipulate session state and not change the display of the app. Essentially, callbacks are called before the script runs, so while the variables c1 and c2 are defined, the containers do not currently exist (until the script run and is recreated again).
The solution is to not use callbacks for this use-case. Instead use a simple conditional to check if the button was clicked and, if so, call show_msg():
import streamlit as st
def show_msg():
c2.write("You clicked Hello!")
st.title("Test Page")
# first container
c1 = st.container()
# second container
c2 = st.container()
if c1.button(label="Hello"):
show_msg()
If you make the above change, you should no longer run into the error. If you feel none of the above linked issues adequately captures this issue, you’re welcome to submit a bug report in the Streamlit repo. Note: your bug report may be closed as a duplicate of one of the above issues.
Thanks @snehankekre for the explanation. I ended up updating the session state through the button callback and check the status in the script to call the show_msg() function.
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.