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