Data caching across multipage app

Hi, I am trying to create an app with multiple pages which perform different statistical tests on the same dataset. What I want is to have a homepage where the data is loaded and then a number of other pages dedicated to different statistical procedures.

What I have now is adapted from the awesome-streamlit project. I currently have a home page for general landing and loading the data, a data viz page which was for visualising raw data but also needs to reload the data, and a stats test which again needs to reload the data then runs a simple sklearn function.

I have my main app.py which looks like this:

import streamlit as st

import awesome_streamlit as ast
import od_pages.home
import od_pages.nntest
import od_pages.dataread

ast.core.services.other.set_logging_format()

PAGES = {
    "Home": od_pages.home,
    "Data Viz": od_pages.dataread,
    "Stats Test A": od_pages.nntest,
}


def main():
    """Main function of the App"""
    st.sidebar.title("Navigation")
    selection = st.sidebar.radio("Go to", list(PAGES.keys()))

    page = PAGES[selection]

    with st.spinner(f"Loading {selection} ..."):
        ast.shared.components.write_page(page)

    st.sidebar.title("Folder location")
    fpath = st.sidebar.text_input(
        "Data filepath"
    )
    st.sidebar.title("About")
    st.sidebar.info(
        """
        This is an info box
"""
    )


if __name__ == "__main__":
    main()

What I want is for the data to be loaded in the home.py and then to become a global variable such that it can be used in Data Viz and Stats Test A. My home.py looks like this:

import streamlit as st
import glob


@st.cache
def get_data(filelist):
    data, freqs, time, pi_dict_list = <readdata>(filelist)

    return data, freqs, time, pi_dict_list


def write():
    """Used to write the page in the app.py file"""
    with st.spinner("Loading Home ..."):
        st.write("""
        # Prototyping environment of stat tests
        blah blah
    """
                 )
        filelist = glob.glob(<folder>)
        data, freqs, time, pi_dict_list = get_data(filelist)

Despite adding in the caching, everytime I renavigate to home, the page is rewrote so the data is reloaded.

Does anyone have any advice on:

  1. How I can pass data between pages?
  2. How I can cache the pages such as to not rewrite them everytime I move off and then back onto them?

Thank you!