Skip to content

Commit

Permalink
fix pre-commit;
Browse files Browse the repository at this point in the history
  • Loading branch information
m-wrzr committed Aug 23, 2024
1 parent 9de468c commit 22dfe79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
34 changes: 20 additions & 14 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

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 @@ -202,7 +204,13 @@ def search_kwargs(searchterm: str, **kwargs) -> List[str]:


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

with searchboxes:
Expand Down Expand Up @@ -294,8 +302,6 @@ def search_kwargs(searchterm: str, **kwargs) -> List[str]:
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
Expand All @@ -305,31 +311,31 @@ def _fragment():
st.session_state.fragment_runs += 1
# pass search function to searchbox
st.button("Run Fragment")
selected_value_fragment= st_searchbox(
selected_value_fragment = st_searchbox(
search_wikipedia_ids,
key="wiki_searchbox_fragment",
rerun_on_update=True,
rerun_scope="fragment"
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"
)
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.")
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}")
st.write(f"streamlit >=1.37 needed for this example. version={st.__version__}")
12 changes: 7 additions & 5 deletions streamlit_searchbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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 @@ -215,11 +214,12 @@ def st_searchbox(
searchboxes and should be passed to every element. Defaults to False.
style_overrides (StyleOverrides, optional):
CSS styling passed directly to the react components. Defaults to None.
rerun_scope ("app", "fragment", optional):
The scope in which to rerun the Streamlit app. Only applicable if Streamlit
version >= 1.37. Defaults to "app".
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 @@ -263,7 +263,9 @@ def st_searchbox(
st.session_state[key]["result"] = value

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

if interaction == "submit":
st.session_state[key]["result"] = (
Expand Down

0 comments on commit 22dfe79

Please sign in to comment.