Update text_area

Summary

Hi,
I want to update my text inside a text area.

Steps to reproduce

Code snippet:

my_area =  st.text_area(":blue[My text here :]",height=2000)
new_text = My_function(my_area)
my_area = st.text_area(":blue[My text here :]",new_text,height=2000)

2 Likes

I’m not entirely clear on the flow you want, but one way to accomplish this is to set the default value of your textbox to be based on a value in st.session_state, and then update that value in the session state and rerun the app

import streamlit as st

if "default" not in st.session_state:
    st.session_state["default"] = "Default text" * 100

my_area = st.text_area(
    ":blue[My text here :]", value=st.session_state["default"], height=2000
)

if st.button("Update default example"):
    st.session_state["default"] = "Updated text" * 100
    st.experimental_rerun()
1 Like

Thanks for the answer. Thats my code :

text = st.text_area(“:blue[Enter text :]”,height=100)

def Upper_Text(text):
upper_text = text.upper()
return upper_text

if st.button(“Upper Text”):
text = st.text_area(“:blue[Enter text :]”,Upper_Text(text),height=2000)

And this is the result :

I want to display the result in the same text area and not create another one.

It’s good.

Hi @ttuz! Did you achieve this?

It can be done in three easy steps:

  1. Assign a key to the text_area so that manipulating its value becomes easier.
  2. Use a callback to change the text_area value when the button is clicked.
  3. Profit!
def on_upper_clicked():
    st.session_state.text = st.session_state.text.upper()

st.text_area("Enter text", key="text")
st.button("Upper Text", on_click=on_upper_clicked)
2 Likes

You could probably do it with a single line (without a callback)

rtnvar = st.text_area(“Enter text”).upper()

Cheers

For OP’s use case, the solution is to use a callback function which can be invoked on a button click. This is because a button click re-runs the entire script again, so that on the re-run, the modified text is rendered in the text area. So in addition to @Goyo’s solution here, the following also works:

import streamlit as st
st.session_state.text = st.session_state.get("text", "").upper()
st.text_area("Enter text", key="text")
st.button("Upper Text")

See this discussion for more info and examples.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.