Hi all, I recently built a simple app that takes user text as input, generates a response, and then asks for user feedback. There are two options for rendering the user feedback form:
Render feedback form simultaneously with everything else.
Pro: The text can be captured in a variable when the form is submitted.
Con: It doesn’t look as slick.
Render feedback form only if the user submits something.
Pro: Looks and feels great.
Con: The form renders fine but its contents are lost.
I’d really like to go with Option 2 but it’s impossible to access the contents of the form on the backend. Here is a code snippet that illustrates the problem:
import streamlit as st
import time
with st.form('input_form'):
query = st.text_area('Ask a question')
submitted_input = st.form_submit_button('Submit question')
print(query) # works fine
if submitted_input:
with st.spinner(text='This may take a moment...'):
time.sleep(2) # simulate generating a response
st.text("That's a good question! I think that...") # simulated response
with st.form('feedback_form'):
feedback = st.text_input('What did you think of this answer?')
submitted_feedback = st.form_submit_button('Submit feedback')
print(feedback) # empty string
To turn this into Option 1 simply remove the if submitted_input guard.
Does anyone have a workaround or at least some insight into why this doesn’t work? Thanks.
Hi @intalentive i think what you can do is to add a callback to your submit button and you can create a container to store the input_form and feedback_form eg.
container_1 = st.container()
container_1.empty()
container_2 = st.container()
container_2.empty()
def create_feedback():
with container_2:
with st.form('feedback_form'):
feedback = st.text_input('What did you think of this answer?')
submitted_feedback = st.form_submit_button('Submit feedback')
with container_1:
with st.form('input_form'):
query = st.text_area('Ask a question')
submitted_input = st.form_submit_button('Submit question', on_click = create_feedback)
print(query) # works fine
if submitted_input:
with st.spinner(text='This may take a moment...'):
time.sleep(2) # simulate generating a response
st.text("That's a good question! I think that...") # simulated response
Thanks for your response! I was really hoping that this would work, but when I add a print statement as follows, I still get the empty string.
def create_feedback():
with container_2:
with st.form('feedback_form'):
feedback = st.text_input('What did you think of this answer?')
submitted_feedback = st.form_submit_button('Submit feedback')
print(feedback) # nothing!
import streamlit as st
import time
container_1 = st.container()
container_1.empty()
container_2 = st.container()
container_2.empty()
def print_fdbk():
print(st.session_state.fbk)
st.session_state.qry = ""
def create_feedback():
with container_2:
with st.form('feedback_form'):
feedback = st.text_input('What did you think of this answer?', key='fbk')
submitted_feedback = st.form_submit_button('Submit feedback', on_click=print_fdbk)
with container_1:
with st.form('input_form'):
query = st.text_area('Ask a question', key='qry')
submitted_input = st.form_submit_button('Submit question', on_click = create_feedback)
print(query)
if submitted_input:
with st.spinner(text='This may take a moment...'):
time.sleep(2) # simulate generating a response
st.text("That's a good question! I think that...") # simulated response
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.