Created
March 11, 2024 03:09
-
-
Save HandcartCactus/0f0304a30a3f8730a9bfd7271193705d to your computer and use it in GitHub Desktop.
A streamlit app which displays a table that updates automatically (on a fixed schedule) or manually.
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 as st | |
import pandas as pd | |
from time import sleep | |
def update_data(): | |
"""you can change this to modify st.session_state.data however you like""" | |
d = st.session_state.data | |
newrow = { | |
'abc': d.iloc[-1]['abc']+1, | |
'def': (d.iloc[-1]['def'] + d.iloc[-2]['def']) % 9, | |
} | |
st.session_state.data = pd.concat([st.session_state.data, pd.DataFrame(data=[newrow])], ignore_index=True) | |
def setup(): | |
"""runs every page render, declares varibales if undeclared. st.session_state persists between page renders""" | |
if 'data' not in st.session_state: | |
st.session_state.data = pd.DataFrame(data=[{'abc': 0, 'def':1}, {'abc':1, 'def':1}]) | |
if 'autorefresh' not in st.session_state: | |
st.session_state.autorefresh = True | |
if 'refresh_frequency_seconds' not in st.session_state: | |
st.session_state.refresh_frequency_seconds = 2 | |
def draw_dataframe(st_element): | |
"""puts a dataframe in st_element, which should be an st.empty() instance""" | |
with st_element.container(): | |
st.dataframe(st.session_state.data, use_container_width=True, hide_index=True,) | |
# dash setup, runs every page render (after action taken) | |
st.set_page_config(layout = "wide") | |
setup() | |
################################################## | |
# page layout | |
################################################## | |
st.title('Auto Refresh Table 🔄') | |
with st.expander('Refresh controls'): | |
# toggle for auto refresh | |
st.toggle( | |
'Auto-refresh', | |
key='autorefresh', # maps toggle value to st.session_state.autorefresh | |
) | |
st.slider(label='refresh every ... seconds', key='refresh_frequency_seconds', min_value=1, max_value=30) | |
# manual refresh button | |
manual_refresh = st.button('manual refresh', disabled=st.session_state.autorefresh) | |
refresh_warning = st.empty() | |
# table | |
st.header('The Table') | |
dataframe_container = st.empty() | |
############################################################### | |
# page logic | |
############################################################### | |
# what to do if manual refresh button clicked | |
if manual_refresh: | |
update_data() | |
draw_dataframe(dataframe_container) | |
# what to do if autorefresh is off | |
if not st.session_state.autorefresh: | |
with refresh_warning.container(): | |
st.warning("Auto Refresh is off. ☝️🤓", icon="ℹ️") | |
# autorefresh loop (anything below will not render unless autorefresh=False) | |
while st.session_state.autorefresh: | |
update_data() | |
draw_dataframe(dataframe_container) | |
sleep(st.session_state.refresh_frequency_seconds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment