-
-
Save demmerichs/822310a2ed9f600362ac31ee49223f86 to your computer and use it in GitHub Desktop.
Rerun a Streamlit app from the top! See https://discuss.streamlit.io/t/update-sidebar/531
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit.ReportThread as ReportThread | |
from streamlit.ScriptRequestQueue import RerunData | |
from streamlit.ScriptRunner import RerunException | |
from streamlit.server.Server import Server | |
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.enqueue == ctx.enqueue: | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script won't work for 0.57.3, fixed version: https://gist.github.com/goraj/47f4e365a13b11c4c0eba4f233acda76