Skip to content

Instantly share code, notes, and snippets.

@goraj
Forked from demmerichs/st_rerun.py
Last active April 2, 2021 19:15
Show Gist options
  • Save goraj/47f4e365a13b11c4c0eba4f233acda76 to your computer and use it in GitHub Desktop.
Save goraj/47f4e365a13b11c4c0eba4f233acda76 to your computer and use it in GitHub Desktop.
Rerun a Streamlit 0.57.3 app from the top! See https://discuss.streamlit.io/t/update-sidebar/531
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
current_server = Server.get_current()
if hasattr(current_server, '_session_infos'):
# Streamlit < 0.56
session_infos = Server.get_current()._session_infos.values()
else:
session_infos = Server.get_current()._session_info_by_id.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
@ernielikesapple
Copy link

ModuleNotFoundError: No module named 'streamlit.ReportThread'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment