Skip to content

Instantly share code, notes, and snippets.

@monchier
Created October 25, 2019 04:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monchier/4ba216cc9168a7d2bea7f3f31a1c4491 to your computer and use it in GitHub Desktop.
Save monchier/4ba216cc9168a7d2bea7f3f31a1c4491 to your computer and use it in GitHub Desktop.
dashboard.py
"""Simple sliding window dashboard-style
plot that updates periodically pulling from
a random generator
"""
import streamlit as st
import time
import random
# session_state from https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
from session_state import get
# st_rerun from https://gist.github.com/tvst/ef477845ac86962fa4c92ec6a72bb5bd
from st_rerun import rerun
session_state = get(values=[])
if len(session_state.values) == 0:
session_state.values.append(0)
st.line_chart(session_state.values[-100:])
new_value = session_state.values[-1] + random.randrange(-100, 100) / 100
session_state.values.append(new_value)
time.sleep(.2)
rerun()
@palmarytech
Copy link

Hi, monchier
run this code i met the error as below

AttributeError: 'ReportSession' object has no attribute '_main_dg'
Traceback:
File "c:\users\dave-\envs\rangoenv\lib\site-packages\streamlit\ScriptRunner.py", line 314, in _run_script
exec(code, module.dict)
File "D:\OneDrive\WeChatArticle\pyStreamlit\dashboard.py", line 26, in
rerun()
File "D:\OneDrive\WeChatArticle\pyStreamlit\st_rerun.py", line 9, in rerun
widget_states = _get_widget_states()
File "D:\OneDrive\WeChatArticle\pyStreamlit\st_rerun.py", line 22, in _get_widget_states
if session_info.session._main_dg == ctx.main_dg:

could you give me some advice, Thanks

@jasleon
Copy link

jasleon commented Mar 9, 2022

@palmarytech @monchier 's example is now supported natively in newer streamlit versions.

"""Simple sliding window dashboard-style
plot that updates periodically pulling from
a random generator
"""
import streamlit as st
import time
import random

# values cannot be used in st.session_state!!
if 'my_values' not in st.session_state:
    st.session_state.my_values = list()

if not st.session_state.my_values:
  st.session_state.my_values.append(0)

st.line_chart(st.session_state.my_values[-100:])

new_value = st.session_state.my_values[-1] + random.randrange(-100, 100) / 100

st.session_state.my_values.append(new_value)

time.sleep(.2)

st.experimental_rerun()

This script is working fine with Streamlit, version 1.6.0

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