Skip to content

Commit

Permalink
Merge pull request #55 from salmanrazzaq-94/seachbox_compaitibility_e…
Browse files Browse the repository at this point in the history
…nhancement

Enhance Searchbox Component for Streamlit 1.37.0 Compatibility and Fragment Support (Issue #54)
  • Loading branch information
m-wrzr authored Aug 23, 2024
2 parents a32a06a + ddab167 commit 9de468c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
49 changes: 45 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

import requests
import streamlit as st

from streamlit_searchbox import st_searchbox

logging.getLogger("streamlit_searchbox").setLevel(logging.DEBUG)

st.set_page_config(layout="centered", page_title="Searchbox Demo")


def search_wikipedia_ids(searchterm: str) -> List[tuple[str, Any]]:
"""
function with list of tuples (label:str, value:any)
Expand Down Expand Up @@ -203,8 +201,8 @@ def search_kwargs(searchterm: str, **kwargs) -> List[str]:
]


searchboxes, visual_ref, form_example, manual_example = st.tabs(
["Searchboxes", "Visual Reference", "Form Example", "Manual Example"]
searchboxes, visual_ref, form_example, manual_example, fragment_example = st.tabs(
["Searchboxes", "Visual Reference", "Form Example", "Manual Example", "Fragment Example"]
)

with searchboxes:
Expand Down Expand Up @@ -292,3 +290,46 @@ def search_kwargs(searchterm: str, **kwargs) -> List[str]:
)

st.write(manual)

with fragment_example:
# Only pass scope if the version is >= 1.37
if st.__version__ >= "1.37":


if "app_runs" not in st.session_state:
st.session_state.app_runs = 0
st.session_state.fragment_runs = 0

@st.fragment
def _fragment():
st.session_state.fragment_runs += 1
# pass search function to searchbox
st.button("Run Fragment")
selected_value_fragment= st_searchbox(
search_wikipedia_ids,
key="wiki_searchbox_fragment",
rerun_on_update=True,
rerun_scope="fragment"
)
if selected_value_fragment:
st.write(selected_value_fragment)
st.write(f"Fragment says it ran {st.session_state.fragment_runs} times.")



st.session_state.app_runs += 1
_fragment()
st.button("Rerun full app")
selected_value_app = st_searchbox(
search_wikipedia_ids,
key="wiki_searchbox_full_app",
rerun_on_update=True,
rerun_scope="app"
)
if selected_value_app:
st.write(selected_value_app)
st.write(f"Full app says it ran {st.session_state.app_runs} times.")
st.write(f"Full app sees that fragment ran {st.session_state.fragment_runs} times.")

else:
st.write(f"You need streamlit version >=1.37 to run this example. Current version is {version}")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="streamlit-searchbox",
version="0.1.14",
version="0.1.15",
author="m-wrzr",
description="Autocomplete Searchbox",
long_description="Streamlit searchbox that dynamically updates "
Expand All @@ -16,7 +16,7 @@
install_requires=[
# version 1.37 reruns lead to constant iFrame resets
# version 1.35/1.36 also have reset issues but less frequent
"streamlit >= 1.0, != 1.37.0",
"streamlit >= 1.0",
],
extras_require={
"tests": [
Expand Down
21 changes: 18 additions & 3 deletions streamlit_searchbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import streamlit as st
import streamlit.components.v1 as components


try:
from streamlit import rerun as rerun # type: ignore
except ImportError:
Expand Down Expand Up @@ -78,6 +79,7 @@ def _process_search(
key: str,
searchterm: str,
rerun_on_update: bool,
rerun_scope: Literal["app", "fragment"] = "app",
**kwargs,
) -> None:
# nothing changed, avoid new search
Expand All @@ -94,7 +96,12 @@ def _process_search(
st.session_state[key]["options_py"] = _list_to_options_py(search_results)

if rerun_on_update:
rerun()
# Only pass scope if the version is >= 1.37

if st.__version__ >= "1.37":
rerun(scope=rerun_scope) # Pass scope if present
else:
rerun()


def _set_defaults(
Expand Down Expand Up @@ -177,6 +184,7 @@ def st_searchbox(
style_absolute: bool = False,
style_overrides: StyleOverrides | None = None,
key: str = "searchbox",
rerun_scope: Literal["app", "fragment"] = "app",
**kwargs,
) -> Any:
"""
Expand Down Expand Up @@ -209,6 +217,9 @@ def st_searchbox(
CSS styling passed directly to the react components. Defaults to None.
key (str, optional):
Streamlit session key. Defaults to "searchbox".
rerun_scope ("app", "fragment", optional):
(Introduced in Streamlit 1.37) The scope in which to rerun the Streamlit app.
Only applicable if Streamlit version >= 1.37. Defaults to "app".
Returns:
any: based on user selection
Expand Down Expand Up @@ -252,7 +263,7 @@ def st_searchbox(
st.session_state[key]["result"] = value

# triggers rerun, no ops afterwards executed
_process_search(search_function, key, value, rerun_on_update, **kwargs)
_process_search(search_function, key, value, rerun_on_update, rerun_scope, **kwargs)

if interaction == "submit":
st.session_state[key]["result"] = (
Expand All @@ -266,7 +277,11 @@ def st_searchbox(
_set_defaults(key, default, default_options)

if rerun_on_update:
rerun()
# Only pass scope if the version is >= 1.37
if st.__version__ >= "1.37":
rerun(scope=rerun_scope) # Pass scope if present
else:
rerun()

return default

Expand Down

0 comments on commit 9de468c

Please sign in to comment.