Streamlit Cheat Sheet: Python Data Apps Fast
Streamlit turns Python scripts into interactive web apps: one call per widget or chart, with st.session_state keeping values across reruns.
Key Terms
- st.session_state: Dictionary-like store that survives reruns; initialize with 'key' not in st.session_state before reading.
- st.form: Groups inputs so the script runs only when its form_submit_button is clicked.
- st.cache_data: Decorator that stores a function's return value so reruns skip recomputing it.
- st.navigation: Multipage API: build st.Page objects, pass them to st.navigation, then call pg.run().
- st.empty: Placeholder container whose content is overwritten by later write() calls.
Pro Tips
- Wrap multi-field entry in st.form because every widget change otherwise reruns the whole script.
- Decorate data-loading functions with @st.cache_data so reruns don't reload the same data.
- Pass value=st.session_state.get('name','') to keep typed text across reruns.
- Bind to 0.0.0.0 with --server.address in Docker, because localhost-only binding blocks outside access to the container.
Pitfalls to Avoid
- Reading st.session_state.counter before initializing it raises an error that crashes the page.
- Omitting --server.address=0.0.0.0 in the Dockerfile CMD leaves the app unreachable from the host.
- Skipping requirements.txt makes the container build fail, because pip install -r has no file to read.
Related cheat sheets
More like this