I created a very simple wrapper for session_state which allows you to do this:
import streamlit as st
from extensions import session_state_auto as ssa
st.title('Counter Example')
if not ssa.count:
ssa.count = 0
st.write('Count = ', ssa.count)
ssa.count = st.number_input("Count", value=ssa.count)
if st.button('Increment'):
ssa.count += 1
st.write('Count = ', ssa.count)
I’m new to streamlit & python, so i’m not sure why it was made so complicated with keys, and checking if keys exist, adding update functions, and on_clicks… why?
Anyways, this is ‘extensions.py’ I wrote:
import streamlit as st
from streamlit import session_state
class SessionStateAutoClass:
def __setattr__(self, name, value):
if getattr(self, name) != value:
session_state[name] = value
st.experimental_rerun()
def __getattr__(self, name):
if name not in session_state:
return None
return session_state[name]
session_state_auto = SessionStateAutoClass()
Now tell me what I’m missing here and why this is bad.