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.
“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:
selected = st.selectbox("Select a gene", options, format_func=first_section)
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).