Why each function in this Streamlit code executes 3 times?

Hello,

Please advice how I can get my code executed only once during data load?

@st.cache() and @st.cache(allow_output_mutation=True) decorations doesn’t help.

see example below:

import streamlit as st
import pandas as pd

fname = "test.xlsx"


@st.cache(allow_output_mutation=True)
def prep_stage(fname):
    print("start reading excel data to memory")
    data = pd.read_excel(fname) 
    print("finished excel data read")
    return data

data = prep_stage(fname)

Received output:

start reading excel data to memory
start reading excel data to memory
start reading excel data to memory
finished excel data read
finished excel data read
finished excel data read

Thanks!

I’ve tried running this with streamlit run script.py and it only seems to run once for me. (Even if I try accessing the app through multiple browsers to load it many times.)

Of course I’m not using the same Excel file as you.

How are you running the app?

What happens if you comment out the pd.read_excel line, and just return an empty array or something instead?

1 Like