Caching Seems to not work. Function is re-run every time a slider is changed

Hello Everyone and thank you for this great lib and your support!

I wrote an app that downloads a dataset using certain API and shows the results on a streamlit dashboard.
All data are stored into a pandas DataFrame (let’s call it DATA).

get_return() is the function I use to download these data and is decorated with @st.cache since it takes quite a while to run.

The dashboard has two widgets:

  • a Dates Range selector (date_input) which is used as a parameter of get_return() and hence should re-run get_return() every time a new value is chosen. This works fine.

  • a slider which is used to filter the data in the DATA. I am expecting this slider to not re-run get_return() everytime a new value is set. On the contrary it keeps re-running this expensive function every time. I tried to create a deep copy of DATA and filter on the copy, but it does not fix the issue.

Here is my code:

#Initialize dataset
with st.spinner('Loading Description'):
    des = dataset.get_text()

#Calendar Selector. 
start = st.date_input('Start Date', value=dataset.get_last_trading_day() - datetime.timedelta(days=1),
                      min_value = datetime.datetime.today() - datetime.timedelta(days=60), 
                      max_value= datetime.datetime.today() - datetime.timedelta(days=1))


end = st.date_input('End Date',min_value= start + datetime.timedelta(days=1), 
                    max_value = datetime.datetime.today(), 
                    value= dataset.get_last_trading_day())

start_ = str(start) + 'T09:30:00-04:00'
end_ = str(end) + 'T09:30:00-04:00'

with st.spinner('Downloading returns for selected dates range. This may take a while'):
    des_ret = get_returns(des, start=start_, end=end_)
    
new_des = des_ret.copy(deep=True)


#Slider to Allow the selection of the quantile to show data for
quantile = st.slider('Quantile',min_value=1, max_value=5,step=1,value=5)
text = new_des.loc[new_des.quant==quantile-1]#.des.values

st.dataframe(new_des)   #shows dataframe function

Hi @Dr_Hofmann, welcome to the Streamlit community!

It feels like this is working as expected? In your code, the date_input are passing new values into get_returns each time. Only after the second run where a set of inputs are duplicated, would you have the cached result returned rather than calculate it from scratch.

Can you post the entire set of code (don’t need API credentials obviously).

Best,
Randy

Are you defining get_returns() in your main.py file, or are you importing it as a module?

1 Like