Want to maintain variable value for repeated runs

Summary

I want to create a simple app where the user selects a file and then provides text input. The app edits and displays the file based on the input. The user may enter some text input again and the app works on the previously edited file. This continues till the user quits. Somehow not able to figure this out using session state.

Steps to reproduce

Code snippet:

if "df" not in st.session_state:
    st.session_state["df"] = False
  
file = st.file_uploader("Choose a file:")
if file is not None:
    if not st.session_state["df"]:
        data = read_df(file)
        st.session_state["df"] = True
       
    form = st.form(key="input")
    txt = form.text_input("What do you want to do?")
    submit = form.form_submit_button("Submit")
    if submit:
         process(data)
         st.write(data)

Expected behavior:
Want the app to edit the same dataframe.

Actual behavior:
UnboundLocalError: local variable ‘data’ referenced before assignment

Would appreciate some help!!

You will want to store the actual data frame in session state, not just a True False flag.

import streamlit as st
import pandas as pd

if 'data' not in st.session_state:
    st.session_state.data = None

def load_data():
    st.session_state.data = pd.read_csv(st.session_state.loader)

file = st.file_uploader('Choose a file', type='csv', key='loader', on_change=load_data)

df = st.session_state.data

if df is not None:
    # Run program
    st.write('Doing a thing')
    st.write(df)

Thanks Debbie @mathcatsand ! Have edited the code as below but somehow the app is still not preserving the dataframe when I hit “Submit” in the form :frowning: Would appreciate if you can spot the error.

def main():           
    if "df" not in st.session_state:
        st.session_state.df = None
    
    def load_data():
        st.session_state.df = pd.read_excel(st.session_state.loader, header=3)

    file = st.file_uploader("Choose a file:", key="loader", on_change=load_data)
    
    data = st.session_state.df
    
    if data is not None:
        st.write("Here")
        form = st.form(key="input")
        txt = form.text_input("What do you want to do?")
        submit = form.form_submit_button("Submit")
        if submit:
            data = do_something(data, txt)
            st.write(data)
            st.session_state.df = data #assign latest value of dataframe to the session state variable

I went ahead and made a line to actually do something and moved the printing of the dataframe display out of the conditional that only runs when updating the data for clarity. This works for me. It’s your same code with those additions (and taken out of a main function for simplicity).

import streamlit as st
import pandas as pd

if "df" not in st.session_state:
    st.session_state.df = None

def load_data():
    st.session_state.df = pd.read_excel(st.session_state.loader, header=3)

file = st.file_uploader("Choose a file:", key="loader", on_change=load_data)

data = st.session_state.df

if data is not None:
    st.write("Here")
    form = st.form(key="input")
    txt = form.text_input("What do you want to do?")
    submit = form.form_submit_button("Submit")
    if submit:
        columns = data.columns
        data[columns[0]] = [txt]*len(data)
        st.session_state.df = data #assign latest value of dataframe to the session state variable
    st.write(data)

Hi Debbie: the issue was an older version of streamlit (0.84). Updating seems to solve it for now.
Thanks for your help with this.

1 Like

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