Streamlit
streamlit run script.py
as you develop code it'll re-run on save
from io import StringIO
import holoviews as hv
import matplotlib.pyplot as plt
import streamlit as st
st.set_page_config(layout="wide")
make sidebar wider
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
width: 500px;
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
width: 500px;
margin-left: -500px;
}
</style>
""",
unsafe_allow_html=True,
)
text
st.title("Hello world!")
st.header("Header")
st.markdown("hello")
select box
letter = st.selectbox("letter:", ["a", "b", "c"], year_months.index("2017-01"))
letter = st.sidebar.selectbox("letter:", [a, b, c])
Layout
col1, col2 = st.columns(2)
with col1:
st.header("Left")
with col1:
st.header("Right")
plots
line chart
st.line_chart(df)
matplotlib
fig, ax = plt.subplots()
df["COL"].plot(ax=ax)
st.pyplot(fig)
bokeh/holoviws
hvfig = df.hvplot()
st.bokeh_chart(hv.render(hvfig, backend="bokeh"))
spatial map/geopandas
st.map(df)
Cache data
@st.cache(show_spinner=False, hash_funcs={xr.core.dataarray.DataArray: id})
@st.cache(show_spinner=False)
def load_data():
df = pd.read_parquet(...)
return df
df = load_data()
@st.cache_data
def fetch_community_data():
return pd.read_parquet("file.parquet")
df = fetch_community_data()
Streamlit write
Display dataframe
st.write(df)
File uploader
uploaded_file = st.file_uploader("Choose a file", type=".json", help="user display for ? hover")
if uploaded_file is not None:
df = pd.read_json(uploaded_file, orient="index")
File downloader
st.download_button(
label="Download data",
data=df.to_json(orient="index", date_format="iso"),
file_name="out.json",
mime="application/json",
)
Upload and download
uploaded_file = st.file_uploader(label="Choose a file", type=".csv")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.download_button(
label="Download data",
data=df.to_csv(index=False),
file_name="out.csv",
)