Load data if it is necessary

Hi,
i have a program that does something like that:

@st.cache_data
def histo_fonds(numero)::
    # Load data from internet 
   return df

def info_fonds(numero):
   # load data from internet
   return df

# select funds 
fonds = st.selectbox("Choix du fonds", 
                     list_fonds.sort_values().unique()
                     )
numero_fonds = df.query("Nom==@fonds")['ID_Produit'].squeeze()

# information on the fund
try:
    info = info_fonds(numero_fonds)
    info
exept: 
   pass

# Dates selection 
dt_min, dt_max = date_fonds(numero_fonds)
col = st.columns(2)
start_date = pd.to_datetime(col[0].date_input("Date de début:", 
                                             format="DD/MM/YYYY", 
                                             value=dt_min, 
                                             min_value=dt_min,
                                             max_value=dt_max
                                             )
                                )
end_date = pd.to_datetime(col[1].date_input("Date de fin:", 
                                                format="DD/MM/YYYY", 
                                                value=dt_max,
                                                max_value=dt_max
                                                )
                            )
try:
       histo = histo_fonds(numero_fonds, start_date=start_date.strftime("%Y-%m-%d")).dropna()
        histo = 100*histo.loc[:end_date]/histo.loc[start_date]
        histo
        except:
            "Pas d'historique pour ce fonds"

What I would like is:

  • the info_fonds() function won’t be change except if I change the fonds variable in st.selectbox(). It will print info variable (if it exists) as I don’t change the numero_fonds variable
  • that histo_fonds() function won’t go to the internet if start_date > dt_min else it will charge the data from internet with histo_fonds()

How should I do to get this ? I hope it is enough clear. Let me know if it is not the case.
Thx for your help.

Hi @Jacques2101

In regards to the above 2 tasks, you can define an if conditional statement with the precise condition and if the condition is met, then you can define the downstream task that should be done.

Here’s a pseudo-code that you can adapt to an actual Python code

if condition:
   # Perform task 1
   # Perform task 2
   # etc.

Hope this helps!

Yes but how to test in a if statement that st.selectbox() was not change ?

You can achieve this by using the st.cache decorator on your info_fonds and histo_fonds functions, I hope you can figure it out.

i suppose that I have to use st.cache decorator but how should I use it ?
Thx.

Something like this should work, give it a try

@st.cache
def histo_fonds(numero, start_date):
    if start_date > dt_min:
        # Load data from cache
        return df
    else:
        # Load data from internet
        return df

@st.cache
def info_fonds(numero):
    # Load data from internet
    return df

# Select funds
fonds = st.selectbox("Choix du fonds", list_fonds.sort_values().unique())
numero_fonds = df.query("Nom==@fonds")['ID_Produit'].squeeze()

# Information on the fund
try:
    info = info_fonds(numero_fonds)
    st.write(info)
except:
    pass

# Dates selection
dt_min, dt_max = date_fonds(numero_fonds)
col = st.columns(2)
start_date = pd.to_datetime(col[0].date_input("Date de début:",
                                               format="DD/MM/YYYY",
                                               value=dt_min,
                                               min_value=dt_min,
                                               max_value=dt_max))
end_date = pd.to_datetime(col[1].date_input("Date de fin:",
                                            format="DD/MM/YYYY",
                                            value=dt_max,
                                            max_value=dt_max))
try:
    histo = histo_fonds(numero_fonds, start_date=start_date.strftime("%Y-%m-%d")).dropna()
    histo = 100 * histo.loc[:end_date] / histo.loc[start_date]
    st.write(histo)
except:
    st.write("Pas d'historique pour ce fonds")

it is strange (I am using 1.26.0 version of streamlit but when I tried to use:

@st.cache
def info_fonds(numero):
    # Load data from internet
    return df

I had an error message st.cache is deprecated. Please use one of Streamlit's new caching commands, st.cache_data or st.cache_resource.

but when I used:

@st.cache_data
def info_fonds(numero):
    # Load data from internet
    return df

nothing happened

It worked only with: @st.cache_resource

I don’t understand why because info_fonds return a datafrale with text

Hey @Jacques2101 and @mathcuber,

st.cache is deprecated – please use st.cache_resource or st.cache_data instead