I use the sidebar to filter data points plotted on a map. I have several selectBoxes to filter on different columns. I would like to change the options in the selectbox according to the data remaining in the filtered dataset.
When creating the selectBox, I use a variable that is updated after the user selects a value. However, the sidebar never gets updated and the options in my sidebar do not match my actual data.
Is there a way to work around this? Re-rendering the sidebar? Deleting a component in the sidebar?
That’s a deceptively complex question! The tricky part is that you need to have one widget modify another that was instantiated earlier in the script — and vice-versa
This is not officially supported in Streamlit today but we’ve been thinking a lot about it lately. We want to come up with a simple and polished API for this use case, and that will take some time. So I’ve created a feature request here where you can follow up on our progress.
In the meantime, here’s a hacky solution that should work for you:
Now run this code, which should be similar to what you’re trying to do:
from streamlit.ScriptRequestQueue import RerunData
from streamlit.ScriptRunner import RerunException
from streamlit.server.Server import Server
import streamlit.ReportThread as ReportThread
def rerun():
"""Rerun a Streamlit app from the top!"""
widget_states = _get_widget_states()
raise RerunException(RerunData(widget_states))
def _get_widget_states():
# Hack to get the session object from Streamlit.
ctx = ReportThread.get_report_ctx()
session = None
session_infos = Server.get_current()._session_infos.values()
for session_info in session_infos:
if session_info.session._main_dg == ctx.main_dg:
session = session_info.session
if session is None:
raise RuntimeError(
"Oh noes. Couldn't get your Streamlit Session object"
'Are you doing something fancy with threads?')
# Got the session object!
return session._widget_states
ctx = ReportThread.get_report_ctx()
session = None
session_infos = Server.get_current()._session_infos.values()
for session_info in session_infos:
if session_info.session._main_dg == ctx.main_dg:
session = session_info.session
if session is None:
raise RuntimeError(
"Oh noes. Couldn't get your Streamlit Session object"
'Are you doing something fancy with threads?')
# Got the session object!
return session._widget_states
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.