Summary
Hello, I am a newbie and experiencing the following problem:
I use a cached function to store multiple dataframes in a dict in order to avoid longer loading times.
However, when I execute the code below the “dict_df” is always empty in streamlit as I assume that streamlit reruns the script multiple times which reinitializes an empty “dict_df”.
Is there an elegant way to make the dict persistant?
Steps to reproduce
Code snippet:
import dbtools as db
import pandas as pd
import numpy as np
import streamlit as st
from pandas.tseries.offsets import DateOffset
import datetime
import plotly as plt
# In[25]:
## Dicts
dict_df = {}
## Dates
today = datetime.datetime.today()
today_calc = datetime.datetime.today()
today = today.strftime('%Y-%m-%d')
startdate = pd.to_datetime('2000-01-01')
# In[26]:
## Read watchlist.csv
watchlist = pd.read_csv("watchlist.csv", delimiter=';')
watchlist = watchlist[['ticker','buy','sell']]
tickerlist = pd.DataFrame(watchlist)['ticker'].tolist()
tickerlist = list(map(lambda x: x.upper(), tickerlist))
# In[27]:
@st.cache_data(persist=True)
def create_stock_df(ticker, start=startdate, end=today):
dict_df[ticker] = db.get_stockdata(ticker, start, end)
# In[28]:
def update_watchlist(streamlit_watchlist_df):
streamlit_watchlist_df = streamlit_watchlist_df[['ticker','buy','sell']]
streamlit_watchlist_df.to_csv("watchlist.csv", sep=';')
tickerlist = pd.DataFrame(streamlit_watchlist_df)['ticker'].tolist()
# In[29]:
with st.sidebar:
st.title("Watchlist Stocks")
streamlit_watchlist_df = st.experimental_data_editor(watchlist, num_rows="dynamic", use_container_width = True)
st.button('Update', on_click=update_watchlist(streamlit_watchlist_df))
# In[30]:
for ticker in tickerlist:
create_stock_df(ticker)
# In[31]:
st.write(dict_df)
Expected behavior:
Streamlit should show contents of the dict_df.
Actual behavior:
dict_df is empty.