I am new to Streamlit, and are trying to build a demo application in the HuggingFace Spaces.
The app has a text_area that contains the text that finally will be sent to the machine learning model. I want to change the content of this text_area multiple ways: a) I want to have the options of choosing pre-filled content from a dropdown list (this replaces all content in the text_area), b) I want the user to be able to modify the content directly and c) I want some «action-buttons» to modify the text already in the text box. This could for instance be to lowercase all of the text.
It is specially the «action-buttons» that are causing problems here. I can not find a way that the buttons simply modifies the content without triggering a reload (and then also a initialisation of the drop-down boxes).
What is the correct approach here?
The current version of the app is available here: DeUnCaser - a Hugging Face Space by north. It does however not have the «action buttons», since I have not gotten that part to work.
I would use a combination of session state and callbacks:
import streamlit as st
if 'options' not in st.session_state:
st.session_state['options'] = ""
def sidebar_callback():
st.session_state['options'] = st.session_state['sidebar']
def button1_callback():
st.session_state['options'] = "foo"
def button2_callback():
st.session_state['options'] = "bar"
placeholder = st.empty()
st.sidebar.selectbox(
"Examples:",
("tirsdag var travel for ukrainas president volodymyr zelenskyj på morgenen tok han imot polens statsminister mateusz morawiecki","tirsdagvartravelforukrainaspresidentvolodymyrzelenskyjpåkveldentokhanimotpolensstatsministermateuszmorawiecki","deterikkelettåholderedepåstoreogsmåbokstavermanmåforeksempelhuskestorforbokstavnårmanskriveromkrimhalvøyamenkunbrukelitenforbokstavnårmanhenvisertilenkrimroman","detteerenlitendemosomerlagetavperegilkummervoldhanerenforskersomtidligerejobbetvednasjonalbiblioteketimoirana", "sentpå60talletvardetfaktisknoensomkalteungensinperegilkummervoldidagerdetikkelengersåvanligåbrukedobbeltnavninorgehvasynesduomdet"),
key = 'sidebar', on_change = sidebar_callback)
st.button('Run DeUnCaser', on_click = button1_callback)
st.button('Run DeUnCaser but not like before', on_click = button2_callback)
with placeholder:
text = st.text_area(f"",max_chars=1000, key = 'options')
im not sure how you define “reload”, but the drop down menu wont be reinitialisied
Regards Leo
Hi Leo,
I can see that this solves one of the issues I was struggling with, the reinitiation of the selectbox. Clever solution. I like it.
However, I am still a bit lost here about the other issue I am facing. I might not have explained the issue good enough. Let me try again.
What the demo/model is doing is adding capitalisation/punctation to sentences. Lets say we have the following input: «my name is john what is your name». The model will try to understand this and hopefully output «My name is John. What is your name?».
Only the «submit button» will actually invoke the model. The other buttons are «effect buttons», like «lowercasing», «removing spaces», «removing punctation» etc.
One use scenario is this:
User copies text from the Internet, and paste it into the textbok. Lets say the text is «My name is John. What is your name?».
The user changes «John» to «Peter».
The user clicks «effect button 1- lowercase». This button should take the current text in the textbox, ie «My name is Peter. What is your name?» and then lowercase it. The output (ie both the value of the «text»-variable and the visible content in «st.text_area») should now be «my name is peter. what is your name?»
When the «submit-button» is pressed, this is the value that is passed along to the model.
Thanks a lot. Leo. I totally misunderstood the two-way link between session_state and key. It was really super easy when I got the concept. Thanks a lot for taking the time explaining it to me!
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.