New to Streamlit - how do I speed up an app?

I am new to Streamlit and Python. I made a simple app and was wondering if there’s anyway to speed it up. The trauma.json file has the raw data that is pulled in and then converted into another format.

import pandas as pd
import streamlit as st
from pygris import tracts
import geopandas as gp
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

st.set_page_config(layout = 'wide')
st.title('U.S. Trauma Hospital Locations')
st.caption('Based on the tutorial "Distance and proximity analysis in Python" by Kyle Walker')

with st.sidebar:
    st.write('Filter state')
    state = st.selectbox('State', 
                         options = ('CA', 'AZ', 'WI'))

def get_data(st_filter):
    state_tracts = tracts(st_filter, cb = True, 
                          year = 2021, 
                          cache = True).to_crs(6571)
    state_tracts = state_tracts.to_crs('EPSG:4326')

state_tracts = get_data(state)

def load_data(state):
    trauma = gp.read_file('trauma.geojson')
    trauma = trauma[trauma['STATE'] == state]
trauma = load_data(state)

col1, col2 = st.columns(2)

with st.container():
    with col1:
        st.subheader('Location of trauma centers')

    with col2:
        st.subheader('Raw data')
        st.write(trauma[['NAME', 'ADDRESS', 'CITY', 'STATE', 'ZIP']])
        state_tracts = state_tracts.to_crs(6571)
        trauma = trauma.to_crs(6571)
        state_buffer = gp.GeoDataFrame(geometry = state_tracts.dissolve().buffer(100000))
        state_trauma = gp.sjoin(trauma, state_buffer, how = "inner")
        tract_centroids = state_tracts.centroid
        dist = tract_centroids.geometry.apply(lambda g: state_trauma.distance(g, align = False))
        min_dist = dist.min(axis = 'columns') / 1000
        hist_values = np.histogram(min_dist, bins = 24, range=(0,24))[0]

with st.container():
        st.subheader('Minimum distance to trauma center from tract centroid')

Thanks for your help!

Hi @ivelasq3

Thanks for sharing your app!

Perhaps you can start by remove unused library imports:

In the get_data() method, it seems that you have first started conversion of data to 6571 then to EPSG:4326. Afterwards in col2, state_tracts is converted from EPSG:4326 back to 6571. Perhaps you can refactor the code so that the latter conversion step in col2 is omitted in order to remove redundancy in conversions.

Hope this helps!

Best regards,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.