Update sidebar

Hello,

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?

Thank you for your help.

Hey @j0bby,

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:

  1. Download this Gist, which adds the ability to rerun a Streamlit script from anywhere in its execution: https://gist.github.com/tvst/ef477845ac86962fa4c92ec6a72bb5bd

  2. 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

Let me know if this helps!