Hello,
I am working on a porting a simple math app from Tkinter to a web app
and would like to use Streamlit for that.
But I can’t figure out how to get it right.
Originally the text input box is empty.
Then the user inputs a value and clicks Check.
In the next run the text input box should be empty and the number is used
somewhere. Same thing on every Check.
This is what I got so far:
import streamlit as st
if 'id_answer' not in st.session_state:
st.session_state['id_answer'] = ''
value = st.session_state['id_answer']
placeholder_text_box_answer = st.empty()
check = st.button('Check', 'id_check')
placeholder_text_box_feedback = st.empty()
if check:
value = ''
st.session_state['id_answer'] = ''
placeholder_text_box_feedback.text(f'value was {value}')
answer = placeholder_text_box_answer.text_input(f'',
value=st.session_state['id_answer'],
max_chars=10,
key='id_answer')
Any ideas how to solve that?
This seems to be a bug and is currently discussed and worked on.
The values set in st.session_state do not propagate to the frontend.
opened 12:06AM - 06 Oct 21 UTC
closed 08:39PM - 08 Oct 21 UTC
bug
state
widgets
https://discuss.streamlit.io/t/problems-with-the-new-widget-behaviour-under-stre… amlit-0-89-0/17625
I investigated using the script from the post. The value seen in session state itself is being set correctly, but `should_set_frontend_state_value` incorrectly returns False. It seems like the check for `is_new_widget_value` shouldn't be part of that method. The method implementation should have been a conservative simplification of the old one that stripped out the initial value handling and separated this check from setting the value of a new widget, so I can't see why the script would have worked in 0.88, but also the current behavior is obviously incorrect.
Removing the second part of the check would make it match the obvious notion that the frontend should have its value overridden whenever the value is set in session state, and trying out this change locally appears to fix the problem.
opened 07:05PM - 12 Oct 21 UTC
bug
### Summary
If a widget's value is set by modifying the session state the wid… get does not update.
The [docs](https://docs.streamlit.io/library/advanced-features/session-state) state
> We can set and get the value of the temperature widget by using the Session State API
### Steps to reproduce
If you take the example stated in the docs that works fine and sets the value, but only for the first run
```
import streamlit as st
if "celsius" not in st.session_state:
# set the initial default value of the slider widget
st.session_state.celsius = 50.0
st.slider(
"Temperature in Celsius",
min_value=-100.0,
max_value=100.0,
key="celsius"
)
# This will get the value of the slider widget
st.write(st.session_state.celsius)
```
If you modify the session state before the widget is "initialized" the GUI does not update,
but the actual value returned from the widget is the one set.
e.g. with the following example:
```
import streamlit as st
import datetime
def cbk_text_input():
st.write('callback text')
st.write(datetime.datetime.now())
def cbk_slider():
st.write('callback slider')
st.write(datetime.datetime.now())
if 'id_input' not in st.session_state:
st.session_state['id_input'] = ''
if 'my_slider' not in st.session_state:
st.session_state['my_slider'] = 5
# these values do not reflect in the GUI,
# but they do in the actual value returned from the widget
st.session_state['id_input'] = 'sdf'
st.session_state['my_slider'] = 50
answer = st.text_input(f'', value='',
key='id_input',
on_change=cbk_text_input)
x = st.slider('some slider', key='my_slider',
min_value=0,
max_value=100,
value=4,
step=5,
on_change=cbk_slider)
answer
st.session_state['id_input']
x
st.session_state['my_slider']
```
But I would expect the widget to also reflect a "manual" change to the session state.
I would have expected the slider to move and the text box to change its content.
### Is this a regression?
no clue
### Debug info
- Streamlit version: 1.0.0
- Python version: 3.9
- OS version: Linux Mint
- Browser version: Firefox 92.0.1
Issue did not exist in Streamlit 0.87.0
system
Closed
October 14, 2022, 5:55am
4
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.