Cashing a function that reads different CSV's

Summary

Does cashing work with this code or not?
i am trying not to repeat code again and again for every CSV file i am reading.

st.cache_data


def eco_load_and_transform(url, Gross, g):
    df = pd.read_csv(url)
    df = df.sort_values(by=[Gross], ascending=False)
    df['Share'] = df['Share'].apply(
        lambda x: str(round(x, 2))+"%")
    df[g] = df[Gross].apply(
        lambda x: human_format(x))
    return df

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.11.0
  • Using pip

First for syntax, you need an @st.cache_data decorator to be on the line above the function you are caching.

@st.cache_data
def eco_load_and_transform(url, Gross, g):

Other than that, if the urls are distinct for the different csv files you are reading, it would work. The caching uses the input of the function to save and lookup the output. If you change a csv file without changing the url being used to access it, your function wouldn’t know that the csv file has changed; it would only see the same url and thus use the previously computed data (assuming the values for all three inputs had been previously used in that combination).

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