Hello,
I’m having unexpected results when filtering my selectbox , and I don’t see the best matches listed on top of the list. For example , I have a huge list in my selectbox, and when I filter with the string “WRN”, I expect to see the options starting with this string (or at list options containing it) on top, however its not the case and I find unrelated strings coming first. Here’s a screen capture of what I see:
In some older discussions I s’ve seen the idea of creating a custom filteroption for filtering the selectbox in our own way, but is there any way to pass this function from the python code ?
Thank you in advance
This behavior is because the default way that the selectbox does filtering is with fuzzy-search, meaning as long as the entry has the characters you type in that order, it will be a shown.
For example,
“WWOX-AS1 | rp 11 190d 6.2 | wwox antisense rna 1 | wwox as 1” contains w...r...n in order.
It depends on your use-case, but if for example you only care about the part before the first | for filtering purposes, you could use a custom format_func to only display that part, and then the filter will only look at that part.
import streamlit as st
options = [
"WWOX-AS1 | rp 11 190d 6.2 | wwox antisense rna 1 | wwox as 1",
"WRN | wrn recq like helicase | werner syndrome recq helicase like | exonuclease wrn | werner ",
]
def first_section(whole_string: str) -> str:
return whole_string.split("|")[0]
selected = st.selectbox("Select a gene", options, format_func=first_section)
st.write(selected)
Note that the output will have the full string, not just the part before the |. Hope that’s helpful.
Thank you for clarifications,
Having seen the react code of streamlit for selectbox filtering , I’m actually aware how its supposed to work. So I know the result is correct , but I guess the selectbox is supposed to list the best scoring strings first, for example the one that starts with : WRN
In addition, I know about the format_fonc and I’m actually using it. But for the filtering, I’d like to it on the entire line , not just on the first section (before the pipe).
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.