Skip to content

Instantly share code, notes, and snippets.

@okld
Last active March 4, 2021 21:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save okld/41d4854722f49ad0fc9a6ad8a49af3da to your computer and use it in GitHub Desktop.
Save okld/41d4854722f49ad0fc9a6ad8a49af3da to your computer and use it in GitHub Desktop.
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