-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackend.py
147 lines (81 loc) · 3.71 KB
/
backend.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
import streamlit as st
from typing import Union
def add_state_to_session(state: Union[str, dict], state_value = None) -> None:
"""
Adds some data to the current session state if it is not already present.
Args:
`Union[str, dict] state`: the state to add. If it is a string, it is interpreted as
a key and is assigned state_value's value. If it is a dictionary, all key value pairs
are inserted as state into the session.
`Any state_value`: the state value to assign to the key, if the state parameter is a string.
Raises:
`ValueError` in the following cases:
- `state_value` is not `None` when `state` is a dictionary
- `state_value` is `None` when `state` is a string
- a type for `state` other than a string or dictionary is given
- if `state` is a dictionary and any of the keys in `state` are not strings
"""
if isinstance(state, dict):
if state_value is not None:
raise ValueError("expected state_value to be None when state is a dictionary, got" + str(state_value))
add_multi_state_to_session(state)
elif isinstance(state, str):
add_single_state_to_session(state, state_value)
else:
raise ValueError("Passed in state key must be a string or dict, is of type " + str(type(state)) + " with value " + str(state))
def add_single_state_to_session(state: str, state_value) -> None:
"""
Adds a single state value to the session state.
Args:
`str state`: the state to add.
`Any state_value`: the value to give the state.
Raises:
`ValueError` in the following cases:
- `state` is not a string
- `state_value` is None
"""
if isinstance(state, str):
if state_value is None:
raise ValueError("state value must be given when the given state is a string key")
if state not in st.session_state:
st.session_state[state] = state_value
else:
raise ValueError("Passed in state key must be a string, is of type " + str(type(state)) + " with value " + str(state))
def add_multi_state_to_session(state: dict) -> None:
"""
Adds multiple state values to the session state.
Args:
`dict state`: the state to add. Keys must all be strings.
Raises:
`ValueError` in the following cases:
- any of `state`'s keys are not strings
"""
for state_element, state_element_value in state.items():
if type(state_element) != str:
raise ValueError("All keys in state dictionary must be strings, found one of type " + str(type(state_element)) + " with value " + str(state_element))
if state_element not in st.session_state:
st.session_state[state_element] = state_element_value
def get_selected_value(selector: list) -> str:
"""
Obtains the value selected in a selector object.
Args:
`list selector`: the selector to get the selected item from.
Returns:
the selected item.
"""
return selector[0] if len(selector) == 1 else ""
def single_selector_page(title: str, description: str, selector_list: list[str]) -> str:
"""
Given a set of options, creates a selector for a user interface
and continuously returns the currently selected value.
Args:
`str title`: the title to give the selector page.
`str description`: the description to give the selector.
`list[str] selector_list`: the list of options for the selector.
Returns:
the currently selected value.
"""
st.subheader(title)
st.write(description)
selector: list[str] = st.multiselect("", selector_list, max_selections=1)
return get_selected_value(selector)