Skip to content

Instantly share code, notes, and snippets.

@pmbaumgartner
Last active December 20, 2019 16:12
Show Gist options
  • Save pmbaumgartner/fffaa96cd81a8da3139398e3c80387eb to your computer and use it in GitHub Desktop.
Save pmbaumgartner/fffaa96cd81a8da3139398e3c80387eb to your computer and use it in GitHub Desktop.
An example of refactoring a streamlit app
# ⛔️ BAD EXAMPLE: PRE-REFACTOR
## app.py
import streamlit as st
import pandas as pd
data = pd.read_csv("data.csv") # no function β†’ no cache, requires pandas import: πŸ‘Ž,πŸ‘Ž
sample = data.head(100) # not input into streamlit object: πŸ‘Ž
described_sample = sample.describe() # input into streamlit object: βœ…
st.write(described_sample)
# βœ… GOOD EXAMPLE
## app.py
import streamlit as st
from helpers import load_data, describe_sample
data = load_data() # data import: βœ…
described_sample = describe_sample(data, 100) # input into streamlit object: βœ…
st.write(described_sample)
## helpers.py
import streamlit as st
import pandas as pd
@st.cache
def load_data():
return pd.read_csv("data.csv")
@st.cache
def describe_sample(dataset, nrows):
sample = dataset.head(nrows)
return sample.describe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment