I am using st.session_state wrong and I can't figure out how to fix it

I am getting the error “NameError: name ‘data’ is not defined” I read that I need to use session state to fix this. This is my code and i can’t figure out how to do it right.

I have this above my form

if ‘data’ not in st.session_state:
st.session_state[‘data’] = ’ ’

This is the part where I convert my form into JSON so supabase will submit it, I hope.

if submitted:

   data = {
   "Ticket" : Ticket,
   "Schedule_Notes" : "Schedule Notes",
   "Pulled_Week" : "Pulled Week",

this is the bottom of with the submit.

if data:
st.session_state[‘data’] = data

try:
execute_query(
st_supabase_client.table(“DIRP”).insert([data])
).execute()

except Exception as e:
st.error(f"Failed to submit data: {e}")

Can you share the complete traceback and relevant code snippet?

Possible reason for this behaviour - You may be processing the data out of refresh loop.

Let me explain - The submit button triggers the whole streamlit application to reload. Therefore, if just had assigned a value on submit button - it will not actually each the downstream code as the Streamlit will now reload from top to bottom due to submit button trigger. Hence you might see this error that data variable is undefined aka NameError.

Streamlit forms have this peculiar quirk that if you have a form that does something post submitting processing - you’ll have a hard time getting it running or get it running consistently. This is because of above-mentioned reason - the application reloads on submit button press.
I have submitted a work around this on my profile, feel free to refer. I Hope that helps.

Plausible solution -

  • Directly save the information from form to session variable instead of first passing to a intermediatory variable.
  • You cannot do the form submit and elegant fetch from the database in single run. You’ll have to plan for a stagging (loading) page. So basically you’ll have to divide this into two steps - get the query data into session variable, and then do a query fetch and display.

My comment is from limited available information. As mentioned by @SiddhantSadangi more information will be amazing.

I finally figured it out!

1 Like

Can you share what was the issue?

I fixed that also… so now my only problem is st.date_input being the wrong syntax for supabase.

Cool!
Btw, I am the creator of st_supabase_connection… so if you’ve any feedback, I’d love to hear :slight_smile:

2 Likes

I do have a piece of feedback. This part of my code I couldn’t find an explicit example of and had to piece it together. I was using st.form and then had to convert that to JSON before executing the query. None of the example I found really addressed “this is how you send your form data to supabase.”

if submitted:

try:
execute_query(
st_supabase_client.table(“DIRP”).insert([data])
).execute()

except Exception as e:
st.error(f"Failed to submit data: {e}")

Can you share the widgets in your form and the payload you are sending to Supabase? I’ll add an example in the README.