Edit text_area widget within st.form

Hi :slight_smile: 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.

Would very much appreciate your help

That’s the whole point of streamlit forms. Only when the submit button is clicked the new values become available to the application.

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

It works for me. Are you really running the same code? What you are doing with the connection doesn’t make sense to me.

OTOH your issue is probably unrelated to the connection so maybe you can produce a reproducible example without involving it.

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