Urgent help on getting value of input box

good day guys. i have just started learning and practicing streamlit and i must say it is cool to use and simple. i have been making use of tkinter for basic desktop apps but now i intend learning streamlit to convert most of my tkinter apps to web app but the issue i am facing is that, some of the simple functions to get the values from input boxes on tkinter are not present on streamlit. stuffs like

insert to insert value into a text_area or text_input,
delete to clear the text_area or text_input box. i was thinking streamlit has such function maybe
box1 = st.text_area(“enter text”)
box1.get() to get the value that is inside the box
box1.insert(“string”) to insert anything inside the text_area or text_input box. stuffs like this would be a great features to add to streamlit because it will make things easy to do with functions. i am currently having issues implementing such flow with streamlit. with tkinter, i have no issue doing this, but with streamlit, how do i get the value of a text_area, text_input when it is inserted after a function is being run. for example, check code below

import streamlit as st

import clipboard

import time

box2 = st.text_area("enter your age")

labels = [1, 2, 3, 4, 5]

box1 = st.text_area('Generated From function', value='type here')

box4 = st.empty()

def MAINFUNCTION(tojoin):

    JOINLIST = "\n".join(str(i) for i in tojoin)

    return JOINLIST

if st.button("Submit"):

    runapp = MAINFUNCTION(labels)

    with box4.beta_container():

        st.text_area("Generated", runapp)

    box1 = st.text_area("Generated From function", runapp)

    box3 = st.text_area('enter your name', runapp)

    #st.success(box1)

if st.button('copy'):

    st.success(f'box1 to be copied: {box1}')

    clipboard.copy(str(box1))

    clipboard.paste()

    st.success("copied successfully")

    st.success(f'box4 to be copied: {box4}')

    st.success(f'box1 to be copied: {box3}')

from the code above, i inserted the value from running the mainfunction after clicking the button. this works well and inserted the value. now i tried to copy the value from the box which was added when i run after clicking the button. but i got an error as nothing is being printed out and copied except this

box1 to be copied: type here

box4 to be copied: <streamlit.delta_generator.DeltaGenerator object at 0x0000021A28263CC8>

why is it not copying the actual content from the box instead of the above. how do i actually get the value inside a box regardless because if i remove the value and manually type anything in box1. it works but why doesn’t it get the value that is generated from the function when the submit button is clicked since it was inserted automatically. also, even after clicking the copy button, the other text_area boxes disappears.

so my question is, how do i get the value regardless .

1 Like

Hi @carty,

Welcome to the Streamlit community. If you look at the API documentation, the return value from the st.text_area()is the current value inside the widget. So you don’t need to convert it into a string. You need to capture the result of text_area in box4 by saving the return from the call into a variable, the way you’re doing with box1, for example.

The way streamlit works is that reruns the entire code from start to the end (or hits st.stop()).

Does this help?

Dinesh

1 Like

hi, good day. i still don’t get it. how do i capture the return value when it is not inserted manually. the code inserted the value automatically after clicking the button to run the Python Function MAINFUNCTION() which return JOINLIST.
now the returned JOINLIST is now a variable of runapp which i inserted in the st textarea of box4.

now i intend to copy that value from box4 by clicking the st.button(‘copy’) which doesn’t copy the value but instead copies something else. so if you can remodify the code for me to see how this would work, i will appreciate it i am still stuck at this

Hi @carty,

So, the program you posted has a bunch of issues. Lets look at them one by one.

When the user interacts with the screen in any way, streamlit runs the entire program from the top. So, when the user clicks on"Submit", the code within the if st.button(“submit”) runs and the variables box1 and box3 are initialized to the contents of whatever’s in the st.text_area widget associated with those boxes. However, when the user clicks on the copy button, the code runs from scratch again, and this time, the code associated with submit is not run, and so box1 and box3 are not populated at all. You’ll need to either save those values of box1 and box3 using SessionState semantics described for example in this post: Multi-page app with session state or the one described in https://gist.github.com/FranzDiebold/898396a6be785d9b5ca6f3706ef9b0bc.

The second problem is that you need to collect the input from text_area which has “Generated” as the label and then use that, not use box4.

So, the code would look like this:

import streamlit as st
import clipboard
import session_state
import time

def MAINFUNCTION(tojoin):
    JOINLIST = "\n".join(str(i) for i in tojoin)
    return JOINLIST

state = session_state._get_state()
box2 = st.text_area("enter your age")
labels = [1, 2, 3, 4, 5]
box1 = st.text_area('Generated From function', value='type here')
box4 = st.empty()

if st.button("Submit"):
    runapp = MAINFUNCTION(labels)
    with box4.beta_container():
        state.box4 = st.text_area("Generated", runapp)

    state.box1 = st.text_area("Generated From function", runapp)
    state.box3 = st.text_area('enter your name', runapp)
    # st.success(box1)

if st.button('copy'):
    st.success(f'box1 to be copied: {state.box1}')

  clipboard.copy(state.box1)
  clipboard_paste()
   st.success("copied successfully")

    st.success(f'box4 to be copied: {state.box4}')
    st.success(f'box1 to be copied: {state.box3}')

Let me know if this works for you. The session_state code that I’m importing from is this one: https://gist.github.com/okld/0aba4869ba6fdc8d49132e6974e2e662. I copied the code associated with the class _SessionStateand the routines _get_state and _get_session and saved it as session_state.py.

Dinesh

1 Like

this is what i am getting when i tried to run the code
No module named ‘session_state’
even when i tried to install session state, i get this error
ERROR: Could not find a version that satisfies the requirement SesstionState
ERROR: No matching distribution found for SesstionState

i am using python3.7.8. how do i fix this issue

If anyone wanted to implement this today, there has been a few updates since in regards to session_state and containers.

The current code would be this:

import streamlit as st
import clipboard
import time

def MAINFUNCTION(tojoin):
    JOINLIST = "\n".join(str(i) for i in tojoin)
    return JOINLIST

state = st.session_state
box2 = st.text_area("enter your age")
labels = [1, 2, 3, 4, 5]
box1 = st.text_area('Generated From function', value='type here')
box4 = st.empty()

if st.button("Submit"):
    runapp = MAINFUNCTION(labels)
    with box4.container():
        state.box4 = st.text_area("Generated", runapp)

    state.box1 = st.text_area("Generated From function", runapp)
    state.box3 = st.text_area('enter your name', runapp)
    # st.success(box1)

if st.button('copy'):
    st.success(f'box1 to be copied: {state.box1}')

    clipboard.copy(state.box1)
    clipboard.paste()
    st.success("copied successfully")

    st.success(f'box4 to be copied: {state.box4}')
    st.success(f'box1 to be copied: {state.box3}')
1 Like