Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Streamlit - Periodic app rerun
import random
import streamlit as st
from streamlit.ReportThread import get_report_ctx
from streamlit.server.Server import Server
from threading import Thread
from time import sleep
def main():
st.line_chart(random.sample(range(100), 20))
st.write(st.text_input("You can still interact with widgets."))
st.write(st.slider("You can even slide normally.", 0, 10))
rerun(.5)
def _get_session():
session_id = get_report_ctx().session_id
session_info = Server.get_current()._get_session_info(session_id)
if session_info is None:
raise RuntimeError("Couldn't get your Streamlit Session object.")
return session_info.session
def _rerun_session(session, delay):
sleep(delay)
session.request_rerun()
def rerun(delay=0):
session = _get_session()
if delay <= 0:
session.request_rerun()
elif hasattr(session, "_rerun_thread") and session._rerun_thread.is_alive():
return
session._rerun_thread = Thread(target=_rerun_session, args=(session, delay))
session._rerun_thread.start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.