Autocomplete Searchbox that dynamically updates suggestions based on a provided function.
Project description
streamlit-searchbox
A streamlit custom component providing a searchbox with autocomplete.
Installation
pip install streamlit-searchbox
Overview
Create a searchbox component and pass a search_function that accepts a str searchterm. The searchbox is triggered on user input, calls the search function for new options and redraws the page via st.experimental_rerun().
You can either pass a list of arguments, e.g.
import streamlit as st
import wikipedia
from streamlit_searchbox import st_searchbox
def search_wikipedia(searchterm: str) -> list:
# search wikipedia for the searchterm
return wikipedia.search(searchterm) if searchterm else []
# pass search function and other options as needed
selected_value = st_searchbox(
search_wikipedia,
placeholder="Search Wikipedia... ",
key="my_key",
)
st.write(f"Selected value: {selected_value}")
This example will call the Wikipedia Api to reload suggestions. The selected_value will be one of the items the search_wikipedia function returns, the suggestions shown in the UI components are a str representation. In case you want to provide custom text for suggestions or retain more information from the search, you can also return a tuple from your search function.
def search(searchterm: str) -> list[tuple[str, dict]]:
return [
(
result["title"], # display in the search box itself
result, # returned on item selection
)
for result in requests.get(f"http://some.endpoint?term={searchterm}").json()
]
selected_value = st_searchbox(search, ...)
st.json(selected_value)
You can also pass additional keyword arguments to a search function in case you need more context by adding them to st_searchbox(search, a=1, b=2).
Parameters
To customize the searchbox you can pass the following arguments:
Required
search_function: Callable[[str], List[any]]
Function that will be called on user input
key: str = "searchbox"
Streamlit key for unique component identification.
Visual
placeholder: str = "Search ..."
Placeholder for empty searches shown within the component.
label: str | None = None
Label shown above the component.
help: str | None = None
Shows a help icon with a popover. Only shown when the label is visible.
Defaults
default: any = None
Default return value in case nothing was submitted or the searchbox cleared.
default_searchterm: str = ''
Default searchterm value when the searchbox is initialized.
default_use_searchterm: bool = False
Use the current searchterm as a default return value.
default_options: list[str] | None = None
Default options that will be shown when first clicking on the searchbox.
Reruns
rerun_on_update: bool = True
Use st.experimental_rerun() to reload the app after user input and load new search suggestions. Disabling leads to delay in showing the proper search results.
rerun_scope: Literal["app", "fragment"] = "app",
If the rerun should affect the whole app or just the fragment.
debounce: int = 150
Delay executing the callback from the react component by x milliseconds to avoid too many / redundant requests, i.e. during fast typing.
min_execution_time: int = 0
DEPRECATED Delay execution after the search function finished to reach a minimum amount of x milliseconds. This can be used to avoid fast consecutive reruns, which can cause resets of the component in some streamlit versions >=1.35 and <1.39.
Transitions
clear_on_submit: bool = False
Automatically clear the input after selection.
edit_after_submit: Literal["disabled", "current", "option", "concat"] = "disabled"
Specify behavior for search query after an option is selected. By setting edit_after_submit to option you can use the searchbox similar to an autocomplete.
reset_function: Callable[[], None] | None = None
Function that will be called when the combobox is reset.
submit_function: Callable[[Any], None] | None = None
Function that will be called when a new option is selected, with the selected option as argument.
Custom Styles
style_overrides: dict | None = None
See section below for more details.
style_absolute: bool = False
Will position the searchbox as an absolute element. NOTE: this will affect all searchbox instances and should either be set for all boxes or none. See #46 for initial workaround by @JoshElgar.
Example
An example Streamlit app can be found here
Styling
To further customize the styling of the searchbox, you can override the default styling by passing style_overrides which will be directly applied in the react components. See below for an example, for more information on the available attributes, please see styling.tsx as well as the react-select documentation.
{
// change the clear icon
"clear":{
"width":20,
"height":20,
// also available: circle-unfilled, circle-filled
"icon":"cross",
// also available: never, after-submit
"clearable":"always"
},
// change the dropdown icon
"dropdown":{
"rotate":true,
"width":30,
"height":30,
"fill":"red"
},
// styling for the searchbox itself, mostly passed to react-select
"searchbox":{
"menuList":{
"backgroundColor":"transparent"
},
"singleValue":{
"color":"red"
},
"option":{
"color":"blue",
"backgroundColor":"yellow",
// highlight matching text
"highlightColor":"green"
}
}
}
Contributions
We welcome contributions from everyone. Here are a few ways you can help:
- Reporting bugs: If you find a bug, please report it by opening an issue.
- Suggesting enhancements: If you have ideas on how to improve the project, please share them by opening an issue.
- Pull requests: If you want to contribute directly to the code base, please make a pull request. Here's how you can do so:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
Contributors
- @JoshElgar absolute positioning workaround
- @dopc bugfix for #15
- @Jumitti
st.reruncompatibility - @salmanrazzaq-94
st.fragmentsupport - @hoggatt
reset_function - @bram49
submit_function - @ytausch remove
testsfolder from distributions - @hansthen
helptooltip
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_searchbox-0.1.24.tar.gz.
File metadata
- Download URL: streamlit_searchbox-0.1.24.tar.gz
- Upload date:
- Size: 844.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6009c368812feea0dd211f03c9cc5cc85b540b0d0a57165bb2885f67d6a8a3d
|
|
| MD5 |
2f6e6f16b4bd5badf8941a94088b99fc
|
|
| BLAKE2b-256 |
46c1b037f76f7d6da73af6311720df3c56ed534616572da4261c0dea1e37110e
|
File details
Details for the file streamlit_searchbox-0.1.24-py3-none-any.whl.
File metadata
- Download URL: streamlit_searchbox-0.1.24-py3-none-any.whl
- Upload date:
- Size: 847.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52ebae43acf8a5c7b6a0e681efc58170c880da308c846dd2db907da84633e468
|
|
| MD5 |
0361503c232cac578ef0ba2e859e207c
|
|
| BLAKE2b-256 |
4e4dcd7f4ca601f700b88a2ddf9b4dff9595ca15c77ab1c786b894d0668fe319
|