st.stop()
guards. You never know if you want to restructure your app in the future, so it’s better to write every line as if it could be the first line of the script.null
s and None
s. You end up writing a lot of if-else
conditions before changing any Streamlit global variable. If you don’t do it, Streamlit is not smart enough to create global variables for you. For example, this raises an exception:import streamlit as st
st.session_state.model = 'gpt-4'
But this works fine:
import streamlit as st
if 'model' not in st.session_state:
st.session_state.model = 'gpt-4'
# or st.session_state['model'] = 'gpt-4'
Streamlit supports get
like so:
import streamlit as st
model = st.session_state.get('model', 'gpt-4')
But get
does NOT update its object—it only returns a default value in case the object doesn’t have the said attribute (e.g., model
). This means the following now raises an error:
import streamlit as st
model = st.session_state.get('model', 'gpt-4')
st.session_state.model = 'gpt-3.5-turbo'
I found it useful to write my own version of get
like this:
def st_get(value: str, default: Any, where=st.session_state):
if hasattr(where, value):
return getattr(where, value)
else:
setattr(where, value, default)
return getattr(where, value)
Now if I use it like this, it will also update st.session_state
:
model = st_get('model', 'gpt-4')
st.session_state.model
#> 'gpt-4'