m-wrzr / streamlit-searchbox

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

Hitting enter in the search box forces one of the defaults to be used #74

Open dividor opened 3 weeks ago

dividor commented 3 weeks ago

I would like users to be able to enter their own search string and not take the default. Ideally, the flow would be like google ...

  1. User enters string
  2. They start seeing default options
  3. They decide they want their query to do the search
  4. They hit enter
  5. The suggestion should go away and their search term should stay in the search box

However, I can't get this to work. Here is the code ...


def search(query):
    results = agent_client.search(query, data_sources=["GAO"])
    titles = []
    for result in results:
        titles.append(result['title'])
    titles = titles[0:4]
    return titles

# Capture the suggestion or direct input
query = st_searchbox(
    search_function=search,
    default_use_searchterm=True,
    placeholder="Type to search or press Enter to search for your input...",
    min_execution_time=2,
    label="search",
)

# Handling the free text search
if query:
    # Check if query was typed without selecting suggestion
    # Here you process the custom input
    st.write(f"Searching for: {query}")
    results = search(query)  # If you need to search again with the entered text
    st.write("Results:", results)

When the user hits enter, it automatically takes the first suggestion.

Is there a way to allow the user to use their own search query without defaulting to the first default when they hit enter?

Thanks!