Skip to content

Instantly share code, notes, and snippets.

@treuille
Last active May 30, 2023 17:18
This demonstrates how piping cached functions into one another automatically sets up an efficient directed acyclic computational graph.
import streamlit as st
import pandas as pd
@st.cache
def load_metadata():
DATA_URL = "https://streamlit-self-driving.s3-us-west-2.amazonaws.com/labels.csv.gz"
return pd.read_csv(DATA_URL, nrows=1000)
@st.cache
def create_summary(metadata, summary_type):
one_hot_encoded = pd.get_dummies(metadata[["frame", "label"]], columns=["label"])
return getattr(one_hot_encoded.groupby(["frame"]), summary_type)()
# Piping one st.cache function into another forms a computation DAG.
summary_type = st.selectbox("Type of summary:", ["sum", "any"])
metadata = load_metadata()
summary = create_summary(metadata, summary_type)
st.write('## Metadata', metadata, '## Summary', summary)
@treuille
Copy link
Author

treuille commented Sep 30, 2019

You can run this gist directly from the command line with:

pip install --upgrade streamlit
streamlit run https://gist.githubusercontent.com/treuille/ac7755eb37c63a78fac7dfef89f3517e/raw/d7b3b2c42a3634d368f980f3cf8f59fc35e8b965/caching_DAG_example.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment