Contents

Python Streamlit - Cheat Sheet

1. Installation/Import

Install Streamlit
pip install streamlit
Import Streamlit
import streamlit as st

2. Components

Streamlit has a number of built-in components that you can use to build your app. Some of the most useful ones include:

Add a header to your app
st.header()
Add a subheader to your app
st.subheader()
Add some text to your app
st.text()
Add Markdown-formatted text to your app
st.markdown()
Add a button to your app
st.button()
Add a checkbox to your app
st.checkbox()
Add a radio button to your app
st.radio()
Add a dropdown menu to your app
st.selectbox()
Add a slider to your app
st.slider()
Add an image to your app
st.image()
Add a Plotly chart to your app
st.plotly_chart()

3. Interactivity

One of the key features of Streamlit is its ability to add interactivity to your app. You can do this by using the st.sidebar function to create a sidebar, and adding interactive widgets such as buttons and sliders to it.

For example:

import streamlit as st

# Add a sidebar
st.sidebar.header("Options")

# Add a slider to the sidebar
value = st.sidebar.slider("Select a value", 0, 100, 50)

# Use the value selected on the slider
st.write(f"You selected {value}")

You can also use the st.cache decorator to cache the results of expensive computations, so that they are only re-computed when the relevant inputs change.

For example:

import streamlit as st

@st.cache
def expensive_computation(x, y):
    return x + y

# Add a slider and a button to the app
x = st.slider("Select a value for x", 0, 100, 50)
y = st.slider("Select a value for y", 0, 100, 50)
button = st.button("Compute")

# When the button is clicked, compute the result of the expensive computation
if button:
    result = expensive_computation(x, y)
    st.write(f"Result: {result}")