m-wrzr / streamlit-searchbox

Streamlit searchbox that dynamically updates and provides a list of suggestions based on a provided function
MIT License
240 stars 31 forks source link

No a bug just a question #25

Closed Jumitti closed 6 months ago

Jumitti commented 11 months ago

Hello i use your package in my streamlit app but i think... i ask to much for it 😅

I have a txt with more than 2 000 000 lines (~80Mo). Each line is something i can search in my searchbar. It is very heavy i know so i split my txt in different sub txt for each lines start with A, B , C ... But, when we start to write in search box, streamlit app rerun because it is how streamlit works if i dont misunderstood.

So when i use wiki api like your exemple, it is very smooth and powerfull. Did you have an idea to do the same things for me ?

Your package is the only way i found to have a really interactive searchbar. Of course i can use streamlit searchbar also but i dont really like it...

So if you have any suggestion i will read it carefully :)

m-wrzr commented 11 months ago

Hey, thanks for the kind words!

I'm not 100% what the issue is without seeing the code, but maybe you can reduce the amount of work that's done within the search function by using one of the caching decorators for intermediate results or using the session state? It sounds like the reload might lead to some recomputation that you could avoid, especially for the splitting part?

Jumitti commented 11 months ago

If you want to test its here: alpha TFinder Tools/Software -> All in One You can input any species you want (like Homo sapiens)

It seems that Streamlit version 1.28 and streamlit-searchbox version 0.1.6 fixes the speed of execution. But since we always use st.rerun(), I think that's what makes it "slow down". My application has a lot of widgets to load so the latency effect is increased tenfold. I no longer observe the problem when I use st_searchbox alone even with a large file

Code:

@st.cache_resource
def taxo():
    with open("utils/species.pkl", "rb") as file:
        species_lists = pickle.load(file)
    return species_lists

def search_taxo(searchterm: str) -> List[any]:
    st.toast('Searching species... ⏳')
    species_lists = taxo()
    st.toast('Species retrieved 😀')
    searchterm1 = searchterm[0].lower()
    species_set = species_lists.get(f"species_{searchterm1}", set())
    matches = [species for species in species_set if searchterm.lower() in species]
    st.toast('Species retrieved 😀')
    return matches

selected_value = st_searchbox(
                    search_taxo,
                    key="search_taxo")

If you want to try, species.pkl is in utils folder of alpha branch of TFinder