Is there a simple solution to change the default text (“Choose an option”) from the multiselect widget? I would like to translate the default text to German.
Thanks!
Is there a simple solution to change the default text (“Choose an option”) from the multiselect widget? I would like to translate the default text to German.
Thanks!
Hi @NiklausBal, welcome to the Streamlit community!!
Streamlit does not expose the option to change the placeholder text for st.multiselect
. As such, there is no native option to change the placeholder because it is hard-coded in the frontend.
However, here is an unofficial CSS hack. I used a CSS selector to first make the default placeholder invisible. Next, I use another CSS selector to set the value of the placeholder. This CSS is injected into the app with st.markdown
.
1 Insert the following snippet into your app:
import streamlit as st
change_text = """
<style>
div.st-cs.st-c5.st-bc.st-ct.st-cu {visibility: hidden;}
div.st-cs.st-c5.st-bc.st-ct.st-cu:before {content: "Wähle eine Option"; visibility: visible;}
</style>
"""
st.markdown(change_text, unsafe_allow_html=True)
2 Replace the German text Wähle eine Option
with the text of your choice.
Here’s an example app and the corresponding output:
import streamlit as st
change_text = """
<style>
div.st-cs.st-c5.st-bc.st-ct.st-cu {visibility: hidden;}
div.st-cs.st-c5.st-bc.st-ct.st-cu:before {content: "Wähle eine Option"; visibility: visible;}
</style>
"""
st.markdown(change_text, unsafe_allow_html=True)
options = st.multiselect(
'What are your favorite colors',
['Green', 'Yellow', 'Red', 'Blue'],)
st.write('You selected:', options)
Are you able to reproduce this behavior?
Happy Streamlit-ing!
Snehan
Inspiration: How can I replace text with CSS?
Hi @snehankekre
Thank you for your solution! It works really well.
Anything like this for selectbox?
Also, how did u get those tags like div.st-cs.st-c5.st-bc.st-ct.st-cu
? If you can explain that, it will be great help
It’s not working for the lastest version streamlit…
On your browser, open up “developer tools” (e.g. F12 on Chrome). Then, inspect the element that corresponds to the text “Choose an option”. The HTML class is what you are looking for. Classes are separated by spaces in HTML. Join them with dots (.
) in change_text
.
Below is what I have based on Streamlit v1.15.2 (@YoungXu06):
This is really a hard-coded hack. I would try and see if it’s possible to contribute a new feature in the near future.
Hello. I am not a front end expert but my HTML class keep changing it’s tag name every time I refresh the page. Is there a work around for that ?
I tested but my div name keeps changing on every refresh. Any way to make it more dynamic ?
If you are okay with changing the default text of every multiselect on the page you can do it as in this example. You can also add a few extra conditions to isolate it to a particular multiselect if the rest of your layout is stable enough.
(Check out the second tab “multiselect” on the page. The code is embedded in the page.)
@SergioAntonio I saw you just liked this. FYI there is a PR on GitHub to add placeholder
as an optional keyword parameter for multiselect. So you will be able to do this natively very soon.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
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.
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.
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.
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.