Hi I am running my streamlit app locally. I want to build a survey where users can edit a pre-generated text. I have this within an st.form because otherwise I cannot easily resample the pre-generated text from a SQL database.
My problem is that when the user edits the text, I cannot access the edited text. It always stores the original pre-generated text. Here my code:
with st.form(key = "form_editing", clear_on_submit=True):
with pool.connect() as db_conn:
query = text("SELECT * FROM df_prompts ORDER BY RAND() LIMIT 1")
result = db_conn.execute(query)
sample_row = result.fetchone()
st.subheader("Prompt")
prompt_id = sample_row["prompt_id"]
st.write("{} [Source]({})".format(sample_row["question"],sample_row["dataset_source"]))
st.subheader("Edit pre-generated answer")
answer_edited = st.text_area("Answer:", value = sample_row["answer"], key = "text_area", max_chars = 1000, help = "You can write your own response to your liking.")
if st.form_submit_button("Submit and View Next"):
insert_editing(
sample_row["question_id"], # question_id
sample_row["prompt_id"], # prompt_id
answer_edited
)
I already tried accessing the updated text via the key in session_state, but nothing worked so far.
Yes of course - that’s not my question. It’s about accessing the value after submitting the form. It works completely fine if I have the text area empty. The problem only occurs if I already put a text inside the widget
with st.form(key = "form_editing", clear_on_submit=True):
with pool.connect() as db_conn:
query = text("SELECT * FROM df_prompts ORDER BY RAND() LIMIT 1")
result = db_conn.execute(query)
sample_row = result.fetchone()
st.subheader("Prompt")
prompt_id = sample_row["prompt_id"]
st.write("{} [Source]
({})".format(sample_row["question"],sample_row["dataset_source"]))
st.subheader("Edit pre-generated answer")
answer_edited = st.text_area("Answer:", value = sample_row["answer"], k
st.form_submit_button("Submit and View Next")
Here is my reproducible code. When I now access the variable “answer_edited” outside the form it is not updated when the user edits it
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.