forked from m-wrzr/streamlit-searchbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
154 lines (120 loc) · 3.4 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import enum
import logging
import random
import time
from typing import List, Tuple
import requests
import streamlit as st
from streamlit_searchbox import st_searchbox
logging.getLogger("streamlit_searchbox").setLevel(logging.DEBUG)
def search_wikipedia_ids(searchterm: str) -> List[Tuple[str, any]]:
"""
function with list of tuples (label:str, value:any)
"""
# you can use a nice default here
if not searchterm:
return []
# search that returns a list of wiki articles in dict form
# with information on title, id, etc
response = requests.get(
"http://en.wikipedia.org/w/api.php",
params={
"list": "search",
"format": "json",
"action": "query",
"srlimit": 10,
"limit": 10,
"srsearch": searchterm,
},
timeout=5,
).json()["query"]["search"]
# first element will be shown in search, second is returned from component
return [
(
str(article["title"]),
article["pageid"],
)
for article in response
]
def search_sth_fast(searchterm: str) -> List[str]:
"""
function with list of strings
"""
return [f"{searchterm}_{i}" for i in range(10)]
def search_rnd_delay(searchterm: str) -> List[str]:
print(f"searching... {searchterm}")
time.sleep(random.randint(1, 5))
return [f"{searchterm}_{i}" for i in range(10)]
def search_fancy_return(_: str):
e = enum.Enum("FancyEnum", {"a": 1, "b": 2, "c": 3})
return [e.a, e.b, e.c]
def search_empty_list(_: str):
if not st.session_state.get("search_empty_list_n", None):
st.session_state["search_empty_list_n"] = 1
return ["a", "b", "c"]
return []
#################################
#### application starts here ####
#################################
c1, c2, c3 = st.columns(3)
with st.sidebar:
selected_value = st_searchbox(
search_function=search_wikipedia_ids,
placeholder="Search Wikipedia",
label="search_wikipedia_ids",
default="SOME DEFAULT",
clear_on_submit=False,
clearable=True,
key="search_wikipedia_ids",
)
st.info(f"{selected_value}")
c1, c2, c3 = st.columns(3)
with c1:
selected_value2 = st_searchbox(
search_sth_fast,
default=None,
label="search_sth_fast",
clear_on_submit=True,
key="search_sth_fast",
)
st.info(f"{selected_value2}")
with c2:
selected_value3 = st_searchbox(
search_rnd_delay,
default=None,
clear_on_submit=False,
clearable=True,
label="search_rnd_delay",
key="search_rnd_delay",
)
st.info(f"{selected_value3}")
with c3:
st.multiselect("For visual reference", [1, 2, 3], default=[1, 2])
st.markdown("---")
st.write("search_fancy_return (no label)")
selected_value4 = st_searchbox(
search_fancy_return,
clear_on_submit=True,
key="search_fancy_return",
)
st.info(f"{selected_value4} {type(selected_value4)}")
selected_value5 = st_searchbox(
search_empty_list,
clear_on_submit=True,
key="search_empty_list",
)
st.info(f"{selected_value5} {type(selected_value5)}")
st.markdown("---")
st.header("Other components for reference:")
st.multiselect(
"Multiselect",
[1, 2, 3, 4, 5],
default=[1, 2],
key="multiselect",
)
st.selectbox(
"Selectbox",
[1, 2, 3],
index=1,
key="selectbox",
)