Slow Data Display

Hello,

I am new to streamlit and coding in general and Iā€™m working on a custom app to display some of our organizationā€™s data. The app Iā€™ve created works, but it is very slow and zooming in and out and other interactive features are very time consuming. I think this can be solved with caching but Iā€™m not sure how to integrate that into my current app. Any help would be great!

import streamlit as st
import altair as alt
import pandas as pd
import erddapy
from vega_datasets import data

Connect to ERDDAP

st.title(ā€˜SWOT Prawler Dataā€™)
e = erddapy.ERDDAP(ā€˜http://heron.pmel.noaa.gov:8080/erddapā€™, protocol=ā€˜tabledapā€™)
e.dataset_id = ā€˜TELON001_PRAWC_N001ā€™ # Data Set to Use

Pull Data from ERDDAP

dfp = e.to_pandas()
dfp[ā€˜time (UTC)ā€™] = pd.to_datetime(dfp[ā€˜time (UTC)ā€™])

Create a subset of the datas

sub = dfp.loc[:,[ā€˜time (UTC)ā€™, ā€˜SB_Depthā€™, ā€˜SB_Tempā€™, ā€˜SB_Conductivityā€™, ā€˜wetlab_Chlorophyllā€™]]
source = sub
brush = alt.selection(type=ā€˜intervalā€™)

#dropdown to select y-axis
option = st.selectbox(
ā€˜Select a Datasetā€™,
[ā€˜SB_Temp:Qā€™, ā€˜SB_Conductivity:Qā€™, ā€˜Optode_Dissolved_O2:Oā€™, ā€˜wetlab_Chlorophyll:Qā€™])

#top panel to plot the data

c = alt.Chart(
source,
title=ā€œSWOT Prawler Dataā€
).mark_circle(size=30).encode(
x=alt.X(ā€˜time (UTC):Tā€™, scale=alt.Scale(
clamp=True, padding=10)
),
y=alt.Y(ā€˜SB_Depth:Qā€™,axis=alt.Axis(title=ā€˜Depth (m)ā€™),
scale=alt.Scale(zero=False, padding=5, domain=[500,0])),
color=alt.condition(brush, option, alt.value(ā€˜lightgrayā€™))
).add_selection(
brush

).properties(
width=700,
height=300
)

#2nd chart
second = alt.Chart(
source,
title=ā€œZoomed in Plotā€
).mark_circle(size=30).encode(
y=alt.Y(ā€˜SB_Depth:Qā€™, sort = ā€œdescendingā€),
color=alt.Color(option, sort = ā€˜descendingā€™, scale=alt.Scale(scheme=ā€œredblueā€)),

x='time (UTC):T',
	tooltip=[
	alt.Tooltip('time (UTC):O', title='Datetime'),
	alt.Tooltip('SB_Depth:O', title='Depth'),
	alt.Tooltip(option, title=option)
]

).transform_filter(
brush
).properties(
height=500, width=700
).interactive()

c & second

Hey @drdevereaux,

Welcome to the forum :wave:

Iā€™m getting timeoutā€™s from ERDDAP when I run your code so I wasnā€™t able to test it fully, but hereā€™s an example gist that moves the e.to_pandas() code into a cached function. That should be enough to get you the speedup that you need.

Possible tweaks to the parameters of the @st.cache() call might be needed but I wasnā€™t able to run it to check.

Thanks so much for taking the time to look at this! This works on my end and gives me a much better idea of how to integrate caching for future streamlit projects. I appreciate the help!