Last active
March 4, 2021 21:04
-
-
Save okld/41d4854722f49ad0fc9a6ad8a49af3da to your computer and use it in GitHub Desktop.
Streamlit - Periodic app rerun
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 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