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
You will want to store the actual data frame in session state, not just a TrueFalse 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 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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.