Passing Variable from one page to Other

Hi Team,

I have created a sidebar with radio buttons and each radio button will load their individual pages but the variable information will be passed from one page to another page.
The sequence is like this:
Data Load -> transform -> EDA

So a person has to click load the dataset and pass the loaded dataset into next page i.e., EDA or Transform.

I have created a sample code for it:

import src.pages.data_injection as data_load
import src.pages.EDA as EDA
import streamlit as st
import pandas as pd
import numpy as np


def main():
     st.sidebar.title("Navigate")
    selection = st.sidebar.radio("Choose a module", PAGES)
    
    if selection == "Data Injection":
        df = data_load.load_page() # this loads page of Data Injection
        
   data = df # create copy of  df .... I am getting error here "local variable 'df' referenced before assignment"
        
    if selection == "EDA":
        EDA.load_page(data)   # this should load EDA page by passing "data" as a variable
    
        
        
if __name__ == '__main__':
	main()

Please let me know how to solve this issue?

Hello @anshul,

Streamlit reruns the whole script each time a widget is updated. So, if you load your data in the Data Injection page, your df will be defined. However, once you click on EDA, the whole script will reload, it won’t branch into the first if statement, and df won’t be defined anymore.

If you want to keep a paging system with some state, I invite you to take a look at this to give you some ideas : Multi-page app with session state.

Beware that this solution is not perfect as it uses some hacks to get everything to work.

3 Likes

Hi @okld,

I was using your session state code and it was running pretty well till I updated the streamlit version.

Now i am getting “No module named ‘streamlit.ReportThread’” error while trying to run the code.

Thanks in advance.

Hello @anshul,

Indeed, 0.65 introduced some code refactoring that broke current session state implementations. I’ll release a fixed version soon!

1 Like

I’ve updated the gist. Things should work better now!

1 Like

Thanks a lot !!!