Hi,
I have a text area but I would like to give the user an option to load a sample text instead of writing one. Ideally this should be done using a button “load sample”.
I am not sure how to change the value of the text_area once the button is clicked without returning the page. something similar to this image:
here is my code so far text = st.text_area('Enter text to classify', height=200) load_sample_button = st.button('load sample', key='btn-load-sample') submit_button = st.button('Classify', key='btn-raw-txt')
import streamlit as st
from streamlit import session_state as ss
if "txt_sample" not in ss: ss.txt_sample = "This is my sample text which I can add to, if required..."
if "wgt_contents" not in ss: ss.wgt_contents = ""
def btncb(): ss.wgt_contents = ss.txt_sample
ss.wgt_contents = st.text_area("My Text", value=ss.wgt_contents, height=200, placeholder="Enter text...")
sc1, sc2, sc3 = st.columns((2,2,5))
sc1.button('Load Sample', key='btn-load-sample', on_click=btncb)
if sc2.button('Show Raw Text', key='btn-raw-txt'): st.write(f"Raw text: {ss.wgt_contents}")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.