Access to widget state

I wanted to know if it is possible to access the widget state of a text_input.
The idea is to check which text_input (out of many) is currently selected.

Hi @Felipe-Hub, welcome to the forum!

You can do this most easily by setting a key on the text_input, and then accessing that in the st.session_state object, like this:

val = st.text_input("Input 1", key="input_1")

st.session_state["input_1"] # returns whatever is in that textbox, as soon as you hit enter

Hmm, I’m not sure it’s that simple… I want to have several different text_inputs with a fixed value, and replace the fixed value depending on a selection by the user from another widget (a canvas with an image and bounding boxes around elements of the image).

So, basically, the text_inputs are loaded in the page, the user would select the text_input they wish to fill in, and then select the bounding box from the canvas containing the value they want to fill the text_input with.

I was thinking about accessing all the relevant text_inputs and verify through the widget state which text_input is currently selected by the user, to get the key from that widget and then use session_state - as you suggested - to change the value in that particular key.

But if there is no easy way to access the widget state, I have to spend some more time to figure out a workaround using only session_state. Thanks.

If by “widget state” you mean “which widget is currently in focus by the user” than you are correct, there isn’t an easy way that I know of to access that. I think the best thing would be to accomplish it through session_state as you’ve suggested.