Dictionaries empty / not persistent


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]:

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:

# In[31]:


Expected behavior:

Streamlit should show contents of the dict_df.

Actual behavior:

dict_df is empty.

First, cache a function that actually returns something.

def get_stockdata(ticker, start=startdate, end=today):
    return db.get_stockdata(ticker, start, end)

Then build the dictionary as a dict comprehesion.

dict_df = {ticker: get_stockdata(ticker) for ticker in tickerlist}

Thank you very much. This works well.

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