Collection of Streamlit recipes and learnings.
-
Create a virtual environment (optional):
$ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ # Should see the virtual env name as a prefix the shell prompt
- Set up for Steamlit Community Cloud hosting.
- Sign up with Github.
Assume st
as a prefix for the package streamlit
, ie. import streamlit as st
.
- Anytime something on the web page is updated, streamlit reruns the entire python script.
- When there is an function associated to event handlers eg.
on_change
oron_click
in a widget, the evetn handler will br run before the rest of the script.
- To speed things up, data or rendered content of a function (or event handler) can be cached via the
@st.cache_data
or@st.cache_resource
decorator. - Use the
@st.cache_data
decorator for anything we can persist in the database eg. python primitive values, dataframes, values from api calls, etc. - Use the
@st.cache_resource
decorator for anything we can't persist in the database eg. ML models, database connections, etc.
-
Use
st.write
to write content to the web page. The function has some "smartness" built into it where it will interpret the passed value type and determine how the content should be rendered. For example passing a dataframedf
tost.write
would renderdf
as an interactive table. -
Sometimes we may want to render the (above) dataframe
df
as a static table, so there are times when callingst. table(df)
is more appropriate. -
We can implicitly write content to the app without calling
st.write
by using streamlit "magic" commands by simply calling the variable literal value. So instead of the following:st.write('Started computation') st.write(df)
We can just call the string expression or variable.
'Started computation' df