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.
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 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.”