Last active
December 20, 2019 16:12
-
-
Save pmbaumgartner/fffaa96cd81a8da3139398e3c80387eb to your computer and use it in GitHub Desktop.
An example of refactoring a streamlit app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# βοΈ 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