Summary
Question: Why are these three code versions not equivalent? The working version does not use the args
kwarg for the form submit button. The two failing versions do.
Steps to reproduce
Type text into the text area produced by the following code, then click ‘Submit’. The code properly prints the entered text to the console:
Code snippet (works):
import streamlit as st
def on_submit(typed_txt):
print(f"Typed txt: '{typed_txt}'")
with st.form('my_form', clear_on_submit=True):
typed_txt = st.text_area(
'TxtEntry',
key='typed_txt',
height=10,
label_visibility='collapsed'
)
st.form_submit_button(
label="Submit",
**on_click=lambda : on_submit(st.session_state['typed_txt'])**
)
The following version unexpectedly prints an empty string to the console:
Code snippet (failing (variation 1):
import streamlit as st
def on_submit(typed_txt):
print(f"Typed txt: '{typed_txt}'")
with st.form('my_form', clear_on_submit=True):
typed_txt = st.text_area(
'TxtEntry',
key='typed_txt',
height=10,
label_visibility='collapsed'
)
st.form_submit_button(
label="Submit",
args=(st.session_state['typed_txt'],),
on_click=lambda txt: on_submit(txt)
)
The following version also prints an empty string to the console:
Code snippet (failing (variation 2):
import streamlit as st
def on_submit(typed_txt):
print(f"Typed txt: '{typed_txt}'")
with st.form('my_form', clear_on_submit=True):
typed_txt = st.text_area(
'TxtEntry',
key='typed_txt',
height=10,
label_visibility='collapsed'
)
st.form_submit_button(
label="Submit",
args=(st.session_state['typed_txt'],),
on_click=on_submit
)
Expected behavior:
I would expect all three variants to print the text entered into the text area.
Actual behavior:
Instead, only the first version works as I would expect.
Debug info
- Streamlit version: 1.17.0
- Python version: 3.10.9
- Using Conda
- OS version: Ubuntu 20.04
- Browser version: 109.0.5414.74 (Official Build) (64-bit)