streambricks 0.2.9__tar.gz → 0.2.11__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {streambricks-0.2.9 → streambricks-0.2.11}/PKG-INFO +1 -1
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/__init__.py +1 -1
- streambricks-0.2.11/src/streambricks/widgets/model_widget/__init__.py +9 -0
- streambricks-0.2.11/src/streambricks/widgets/model_widget/field_metadata_renderers.py +22 -0
- streambricks-0.2.9/src/streambricks/widgets/model_widget.py → streambricks-0.2.11/src/streambricks/widgets/model_widget/main.py +50 -432
- streambricks-0.2.11/src/streambricks/widgets/model_widget/type_renderers.py +299 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/widgets/type_helpers.py +8 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.copier-answers.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.github/dependabot.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.gitignore +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/LICENSE +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/README.md +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/docs/.empty +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/duties.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/mkdocs.yml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/pyproject.toml +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/state.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/tests/__init__.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/tests/conftest.py +0 -0
- {streambricks-0.2.9 → streambricks-0.2.11}/tests/test_widgets.py +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Field metadata type renderers for Pydantic form fields."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def render_model_id_field(
|
|
9
|
+
*,
|
|
10
|
+
key: str,
|
|
11
|
+
value: str | None = None,
|
|
12
|
+
label: str | None = None,
|
|
13
|
+
disabled: bool = False,
|
|
14
|
+
help: str | None = None, # noqa: A002
|
|
15
|
+
**field_info: Any,
|
|
16
|
+
) -> str:
|
|
17
|
+
"""Render a model identifier field using model_selector widget."""
|
|
18
|
+
from streambricks.widgets.model_selector import model_selector
|
|
19
|
+
|
|
20
|
+
providers = field_info.get("providers")
|
|
21
|
+
selected_model = model_selector(value=value, providers=providers, expanded=False)
|
|
22
|
+
return selected_model.pydantic_ai_id if selected_model else value or ""
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from collections.abc import Callable, Sequence
|
|
6
|
-
import contextlib
|
|
7
6
|
from datetime import date, datetime, time
|
|
8
7
|
from decimal import Decimal
|
|
9
8
|
from enum import Enum
|
|
@@ -13,10 +12,26 @@ import fieldz
|
|
|
13
12
|
from pydantic import SecretStr
|
|
14
13
|
import streamlit as st
|
|
15
14
|
|
|
15
|
+
from streambricks.widgets.model_widget.field_metadata_renderers import (
|
|
16
|
+
render_model_id_field,
|
|
17
|
+
)
|
|
18
|
+
from streambricks.widgets.model_widget.type_renderers import (
|
|
19
|
+
render_bool_field,
|
|
20
|
+
render_date_field,
|
|
21
|
+
render_datetime_field,
|
|
22
|
+
render_enum_field,
|
|
23
|
+
render_float_field,
|
|
24
|
+
render_int_field,
|
|
25
|
+
render_literal_field,
|
|
26
|
+
render_secret_str_field,
|
|
27
|
+
render_str_field,
|
|
28
|
+
render_time_field,
|
|
29
|
+
)
|
|
16
30
|
from streambricks.widgets.type_helpers import (
|
|
17
31
|
add_new_item,
|
|
18
32
|
create_default_instance,
|
|
19
33
|
get_description,
|
|
34
|
+
get_inner_type,
|
|
20
35
|
get_with_default,
|
|
21
36
|
is_dataclass_like,
|
|
22
37
|
is_literal_type,
|
|
@@ -31,322 +46,28 @@ if TYPE_CHECKING:
|
|
|
31
46
|
|
|
32
47
|
T = TypeVar("T")
|
|
33
48
|
WidgetFunc = Callable[..., T]
|
|
49
|
+
TForm = TypeVar("TForm", bound=BaseModel)
|
|
34
50
|
|
|
35
51
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
value: str | None = None,
|
|
40
|
-
label: str | None = None,
|
|
41
|
-
disabled: bool = False,
|
|
42
|
-
help: str | None = None, # noqa: A002
|
|
43
|
-
**field_info: Any,
|
|
44
|
-
) -> str:
|
|
45
|
-
"""Render a string field using appropriate Streamlit widget."""
|
|
46
|
-
max_length = field_info.get("max_length", 0)
|
|
47
|
-
multiple_lines = field_info.get("multiple_lines", False)
|
|
48
|
-
safe_value = "" if value is None else value
|
|
49
|
-
has_meaningful_content = safe_value.strip() != ""
|
|
50
|
-
has_line_breaks = has_meaningful_content and (
|
|
51
|
-
"\n" in safe_value or "\r" in safe_value
|
|
52
|
-
)
|
|
53
|
-
is_long_val = len(safe_value) > 100 # noqa: PLR2004
|
|
54
|
-
use_text_area = max_length > 100 or is_long_val or multiple_lines or has_line_breaks # noqa: PLR2004
|
|
55
|
-
|
|
56
|
-
if use_text_area:
|
|
57
|
-
return st.text_area(
|
|
58
|
-
label=label or key,
|
|
59
|
-
value=safe_value,
|
|
60
|
-
disabled=disabled,
|
|
61
|
-
key=key,
|
|
62
|
-
help=help,
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
# For single-line inputs, strip whitespace as it's usually unintentional
|
|
66
|
-
return st.text_input(
|
|
67
|
-
label=label or key,
|
|
68
|
-
value=safe_value.strip() if has_meaningful_content else safe_value,
|
|
69
|
-
disabled=disabled,
|
|
70
|
-
key=key,
|
|
71
|
-
help=help,
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
def render_int_field(
|
|
76
|
-
*,
|
|
77
|
-
key: str,
|
|
78
|
-
value: int | None = None,
|
|
79
|
-
label: str | None = None,
|
|
80
|
-
disabled: bool = False,
|
|
81
|
-
help: str | None = None, # noqa: A002
|
|
82
|
-
**field_info: Any,
|
|
83
|
-
) -> int:
|
|
84
|
-
"""Render an integer field using Streamlit number_input."""
|
|
85
|
-
# Set default value
|
|
86
|
-
safe_value = int(value) if value is not None else 0
|
|
87
|
-
|
|
88
|
-
# Extract constraints, ensuring they're integers
|
|
89
|
-
min_value = field_info.get("ge") or field_info.get("gt")
|
|
90
|
-
min_value = int(min_value) if min_value is not None else None
|
|
91
|
-
|
|
92
|
-
max_value = field_info.get("le") or field_info.get("lt")
|
|
93
|
-
max_value = int(max_value) if max_value is not None else None
|
|
94
|
-
|
|
95
|
-
step = field_info.get("multiple_of")
|
|
96
|
-
step = int(step) if step is not None else 1
|
|
97
|
-
|
|
98
|
-
result = st.number_input(
|
|
99
|
-
label=label or key,
|
|
100
|
-
value=safe_value,
|
|
101
|
-
min_value=min_value,
|
|
102
|
-
max_value=max_value,
|
|
103
|
-
step=step,
|
|
104
|
-
disabled=disabled,
|
|
105
|
-
key=key,
|
|
106
|
-
format="%d",
|
|
107
|
-
help=help,
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
return int(result)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
def render_float_field(
|
|
114
|
-
*,
|
|
115
|
-
key: str,
|
|
116
|
-
value: float | Decimal | None = None,
|
|
117
|
-
label: str | None = None,
|
|
118
|
-
disabled: bool = False,
|
|
119
|
-
help: str | None = None, # noqa: A002
|
|
120
|
-
**field_info: Any,
|
|
121
|
-
) -> float | Decimal:
|
|
122
|
-
"""Render a float or Decimal field using Streamlit number_input."""
|
|
123
|
-
# Determine if we're dealing with a Decimal
|
|
124
|
-
field_type = field_info.get("type")
|
|
125
|
-
is_decimal = field_type is Decimal
|
|
126
|
-
|
|
127
|
-
# Convert to float for Streamlit compatibility
|
|
128
|
-
safe_value = float(value) if value is not None else 0.0
|
|
129
|
-
|
|
130
|
-
# Extract constraints, ensuring they're floats
|
|
131
|
-
min_value = field_info.get("ge") or field_info.get("gt")
|
|
132
|
-
min_value = float(min_value) if min_value is not None else None
|
|
133
|
-
|
|
134
|
-
max_value = field_info.get("le") or field_info.get("lt")
|
|
135
|
-
max_value = float(max_value) if max_value is not None else None
|
|
136
|
-
|
|
137
|
-
step = field_info.get("multiple_of")
|
|
138
|
-
step = float(step) if step is not None else 0.01
|
|
139
|
-
|
|
140
|
-
result = st.number_input(
|
|
141
|
-
label=label or key,
|
|
142
|
-
value=safe_value,
|
|
143
|
-
min_value=min_value,
|
|
144
|
-
max_value=max_value,
|
|
145
|
-
step=step,
|
|
146
|
-
disabled=disabled,
|
|
147
|
-
key=key,
|
|
148
|
-
help=help,
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
# Convert back to Decimal if needed
|
|
152
|
-
if is_decimal:
|
|
153
|
-
return Decimal(str(result))
|
|
154
|
-
|
|
155
|
-
return result
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
def render_bool_field(
|
|
159
|
-
*,
|
|
160
|
-
key: str,
|
|
161
|
-
value: bool | None = None,
|
|
162
|
-
label: str | None = None,
|
|
163
|
-
disabled: bool = False,
|
|
164
|
-
help: str | None = None, # noqa: A002
|
|
165
|
-
**field_info: Any,
|
|
166
|
-
) -> bool:
|
|
167
|
-
"""Render a boolean field using appropriate Streamlit widget."""
|
|
168
|
-
return st.checkbox(
|
|
169
|
-
label=label or key,
|
|
170
|
-
value=value if value is not None else False,
|
|
171
|
-
disabled=disabled,
|
|
172
|
-
key=key,
|
|
173
|
-
help=help,
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
def render_date_field(
|
|
178
|
-
*,
|
|
179
|
-
key: str,
|
|
180
|
-
value: date | None = None,
|
|
181
|
-
label: str | None = None,
|
|
182
|
-
disabled: bool = False,
|
|
183
|
-
help: str | None = None, # noqa: A002
|
|
184
|
-
**field_info: Any,
|
|
185
|
-
) -> date:
|
|
186
|
-
"""Render a date field using appropriate Streamlit widget."""
|
|
187
|
-
return st.date_input(
|
|
188
|
-
label=label or key,
|
|
189
|
-
value=value or date.today(),
|
|
190
|
-
disabled=disabled,
|
|
191
|
-
key=key,
|
|
192
|
-
help=help,
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
def render_time_field(
|
|
197
|
-
*,
|
|
198
|
-
key: str,
|
|
199
|
-
value: time | None = None,
|
|
200
|
-
label: str | None = None,
|
|
201
|
-
disabled: bool = False,
|
|
202
|
-
help: str | None = None, # noqa: A002
|
|
203
|
-
**field_info: Any,
|
|
204
|
-
) -> time:
|
|
205
|
-
"""Render a time field using appropriate Streamlit widget."""
|
|
206
|
-
return st.time_input(
|
|
207
|
-
label=label or key,
|
|
208
|
-
value=value or datetime.now().time(),
|
|
209
|
-
disabled=disabled,
|
|
210
|
-
key=key,
|
|
211
|
-
help=help,
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
def render_enum_field(
|
|
216
|
-
*,
|
|
217
|
-
key: str,
|
|
218
|
-
value: Enum | None = None,
|
|
219
|
-
label: str | None = None,
|
|
220
|
-
disabled: bool = False,
|
|
221
|
-
help: str | None = None, # noqa: A002
|
|
222
|
-
**field_info: Any,
|
|
223
|
-
) -> Enum:
|
|
224
|
-
"""Render an enum field using appropriate Streamlit widget."""
|
|
225
|
-
enum_class = field_info.get("enum_class") or field_info.get("type")
|
|
226
|
-
if enum_class is None or not issubclass(enum_class, Enum):
|
|
227
|
-
error_msg = f"Invalid enum class for field {key}"
|
|
228
|
-
raise TypeError(error_msg)
|
|
229
|
-
|
|
230
|
-
options = list(enum_class.__members__.values())
|
|
231
|
-
|
|
232
|
-
if not options:
|
|
233
|
-
return None # type: ignore
|
|
234
|
-
|
|
235
|
-
index = 0
|
|
236
|
-
if value is not None:
|
|
237
|
-
with contextlib.suppress(ValueError):
|
|
238
|
-
index = options.index(value)
|
|
239
|
-
|
|
240
|
-
return st.selectbox(
|
|
241
|
-
label=label or key,
|
|
242
|
-
options=options,
|
|
243
|
-
index=index,
|
|
244
|
-
disabled=disabled,
|
|
245
|
-
key=key,
|
|
246
|
-
help=help,
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
def render_secret_str_field(
|
|
251
|
-
*,
|
|
252
|
-
key: str,
|
|
253
|
-
value: SecretStr | None = None,
|
|
254
|
-
label: str | None = None,
|
|
255
|
-
disabled: bool = False,
|
|
256
|
-
help: str | None = None, # noqa: A002
|
|
257
|
-
**field_info: Any,
|
|
258
|
-
) -> SecretStr:
|
|
259
|
-
"""Render a SecretStr field using a password input."""
|
|
260
|
-
text = st.text_input(
|
|
261
|
-
label=label or key,
|
|
262
|
-
value=value.get_secret_value() if value else "",
|
|
263
|
-
disabled=disabled,
|
|
264
|
-
key=key,
|
|
265
|
-
help=help,
|
|
266
|
-
type="password",
|
|
267
|
-
)
|
|
268
|
-
return SecretStr(text)
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
def render_datetime_field(
|
|
272
|
-
*,
|
|
273
|
-
key: str,
|
|
274
|
-
value: datetime | None = None,
|
|
275
|
-
label: str | None = None,
|
|
276
|
-
disabled: bool = False,
|
|
277
|
-
help: str | None = None, # noqa: A002
|
|
278
|
-
**field_info: Any,
|
|
279
|
-
) -> datetime:
|
|
280
|
-
"""Render a datetime field using date and time inputs."""
|
|
281
|
-
# Split into date and time components
|
|
282
|
-
current_date = value.date() if value else date.today()
|
|
283
|
-
current_time = value.time() if value else datetime.now().time()
|
|
284
|
-
|
|
285
|
-
# Create columns for date and time
|
|
286
|
-
date_col, time_col = st.columns(2)
|
|
287
|
-
|
|
288
|
-
with date_col:
|
|
289
|
-
selected_date = st.date_input(
|
|
290
|
-
label=f"{label or key} (Date)",
|
|
291
|
-
value=current_date,
|
|
292
|
-
disabled=disabled,
|
|
293
|
-
key=f"{key}_date",
|
|
294
|
-
help=help,
|
|
295
|
-
)
|
|
296
|
-
|
|
297
|
-
with time_col:
|
|
298
|
-
selected_time = st.time_input(
|
|
299
|
-
label=f"{label or key} (Time)",
|
|
300
|
-
value=current_time,
|
|
301
|
-
disabled=disabled,
|
|
302
|
-
key=f"{key}_time",
|
|
303
|
-
help=help,
|
|
304
|
-
)
|
|
305
|
-
|
|
306
|
-
# Combine date and time
|
|
307
|
-
return datetime.combine(selected_date, selected_time)
|
|
308
|
-
|
|
52
|
+
FIELD_METADATA_RENDERERS: dict[str, WidgetFunc[Any]] = {
|
|
53
|
+
"model_identifier": render_model_id_field
|
|
54
|
+
}
|
|
309
55
|
|
|
310
|
-
def render_literal_field(
|
|
311
|
-
*,
|
|
312
|
-
key: str,
|
|
313
|
-
value: Any = None,
|
|
314
|
-
label: str | None = None,
|
|
315
|
-
disabled: bool = False,
|
|
316
|
-
help: str | None = None, # noqa: A002
|
|
317
|
-
**field_info: Any,
|
|
318
|
-
) -> Any:
|
|
319
|
-
"""Render a Literal field using appropriate Streamlit widget."""
|
|
320
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
321
|
-
options = get_args(annotation)
|
|
322
|
-
|
|
323
|
-
# No need for radio if only one option
|
|
324
|
-
if len(options) == 1:
|
|
325
|
-
return options[0]
|
|
326
|
-
|
|
327
|
-
# Use radio for boolean literals
|
|
328
|
-
if all(isinstance(opt, bool) for opt in options):
|
|
329
|
-
index = options.index(value) if value in options else 0
|
|
330
|
-
return st.radio(
|
|
331
|
-
label=label or key,
|
|
332
|
-
options=options,
|
|
333
|
-
index=index,
|
|
334
|
-
disabled=disabled,
|
|
335
|
-
key=key,
|
|
336
|
-
horizontal=True,
|
|
337
|
-
help=help,
|
|
338
|
-
)
|
|
339
56
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
57
|
+
# Mapping of Python types to render functions
|
|
58
|
+
PRIMITIVE_RENDERERS = {
|
|
59
|
+
str: render_str_field,
|
|
60
|
+
bool: render_bool_field,
|
|
61
|
+
int: render_int_field,
|
|
62
|
+
float: render_float_field,
|
|
63
|
+
Decimal: render_float_field,
|
|
64
|
+
date: render_date_field,
|
|
65
|
+
time: render_time_field,
|
|
66
|
+
datetime: render_datetime_field,
|
|
67
|
+
Enum: render_enum_field,
|
|
68
|
+
Literal: render_literal_field,
|
|
69
|
+
SecretStr: render_secret_str_field,
|
|
70
|
+
}
|
|
350
71
|
|
|
351
72
|
|
|
352
73
|
def render_union_field( # noqa: PLR0911
|
|
@@ -361,13 +82,10 @@ def render_union_field( # noqa: PLR0911
|
|
|
361
82
|
"""Render a field that can accept multiple types."""
|
|
362
83
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
363
84
|
possible_types = get_args(annotation)
|
|
364
|
-
|
|
365
|
-
# Create type selector
|
|
366
85
|
type_key = f"{key}_type"
|
|
367
86
|
type_names = [
|
|
368
87
|
t.__name__ if hasattr(t, "__name__") else str(t) for t in possible_types
|
|
369
88
|
]
|
|
370
|
-
|
|
371
89
|
selected_type_name = st.selectbox(
|
|
372
90
|
f"Type for {label or key}",
|
|
373
91
|
options=type_names,
|
|
@@ -376,19 +94,13 @@ def render_union_field( # noqa: PLR0911
|
|
|
376
94
|
help=help,
|
|
377
95
|
)
|
|
378
96
|
|
|
379
|
-
# Find selected type
|
|
380
97
|
selected_type_index = type_names.index(selected_type_name)
|
|
381
98
|
selected_type = possible_types[selected_type_index]
|
|
382
|
-
|
|
383
|
-
# Create field for selected type
|
|
384
99
|
field_key = f"{key}_value"
|
|
385
100
|
modified_field_info = field_info.copy()
|
|
386
101
|
modified_field_info["type"] = selected_type
|
|
387
|
-
|
|
388
|
-
# Only pass value if it matches the selected type or can be converted
|
|
389
102
|
typed_value: Any = None
|
|
390
103
|
if value is not None:
|
|
391
|
-
# Try to convert the value to the selected type
|
|
392
104
|
try:
|
|
393
105
|
if selected_type is int and isinstance(value, int | float):
|
|
394
106
|
typed_value = int(value)
|
|
@@ -408,13 +120,12 @@ def render_union_field( # noqa: PLR0911
|
|
|
408
120
|
result = renderer(
|
|
409
121
|
key=field_key,
|
|
410
122
|
value=typed_value,
|
|
411
|
-
label=f"Value ({selected_type_name})",
|
|
123
|
+
label=f"Value ({selected_type_name})",
|
|
412
124
|
disabled=disabled,
|
|
413
125
|
help=help,
|
|
414
126
|
**modified_field_info,
|
|
415
127
|
)
|
|
416
128
|
|
|
417
|
-
# Ensure the result is of the correct type
|
|
418
129
|
try:
|
|
419
130
|
if selected_type is int and not isinstance(result, int):
|
|
420
131
|
return int(result)
|
|
@@ -427,7 +138,6 @@ def render_union_field( # noqa: PLR0911
|
|
|
427
138
|
except (ValueError, TypeError) as e:
|
|
428
139
|
error_msg = f"Cannot convert {result} to {selected_type.__name__}: {e!s}"
|
|
429
140
|
st.error(error_msg)
|
|
430
|
-
# Return a default value for the selected type
|
|
431
141
|
if selected_type is int:
|
|
432
142
|
return 0
|
|
433
143
|
if selected_type is float:
|
|
@@ -461,33 +171,18 @@ def render_sequence_field(
|
|
|
461
171
|
**field_info: Any,
|
|
462
172
|
) -> list[Any]:
|
|
463
173
|
"""Render a field for sequence types (list, tuple, set)."""
|
|
464
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
465
|
-
|
|
466
|
-
# Create unique state keys for this field
|
|
467
174
|
add_item_key = f"{key}_add_item"
|
|
468
175
|
items_key = f"{key}_items"
|
|
469
|
-
|
|
470
|
-
# Initialize session state for this field
|
|
471
176
|
if items_key not in st.session_state:
|
|
472
177
|
st.session_state[items_key] = list(value) if value is not None else []
|
|
473
|
-
|
|
474
|
-
# Extract item type from sequence annotation
|
|
475
|
-
try:
|
|
476
|
-
item_type = get_args(annotation)[0] # Get type of sequence items
|
|
477
|
-
except (IndexError, TypeError):
|
|
478
|
-
item_type = Any
|
|
479
|
-
|
|
480
|
-
# Create container for sequence elements
|
|
178
|
+
item_type = get_inner_type(field_info)
|
|
481
179
|
st.markdown(f"**{label or key}**")
|
|
482
180
|
if help:
|
|
483
181
|
st.caption(help)
|
|
484
182
|
|
|
485
183
|
with st.container():
|
|
486
|
-
# Add new item button
|
|
487
184
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
488
185
|
add_new_item(st.session_state[items_key], item_type)
|
|
489
|
-
|
|
490
|
-
# Render items
|
|
491
186
|
render_sequence_items(
|
|
492
187
|
st.session_state[items_key],
|
|
493
188
|
item_type,
|
|
@@ -497,7 +192,6 @@ def render_sequence_field(
|
|
|
497
192
|
field_info,
|
|
498
193
|
)
|
|
499
194
|
|
|
500
|
-
# Return the current items
|
|
501
195
|
return st.session_state[items_key]
|
|
502
196
|
|
|
503
197
|
|
|
@@ -512,7 +206,7 @@ def render_sequence_items(
|
|
|
512
206
|
"""Render items in a sequence with delete buttons."""
|
|
513
207
|
item_info = field_info.copy()
|
|
514
208
|
item_info["type"] = item_type
|
|
515
|
-
item_info["inside_expander"] = True
|
|
209
|
+
item_info["inside_expander"] = True
|
|
516
210
|
|
|
517
211
|
try:
|
|
518
212
|
renderer = get_field_renderer(item_info)
|
|
@@ -554,19 +248,9 @@ def render_set_field(
|
|
|
554
248
|
**field_info: Any,
|
|
555
249
|
) -> set[Any]:
|
|
556
250
|
"""Render a field for set types with a multi-select interface when possible."""
|
|
557
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
558
|
-
|
|
559
|
-
# Initialize empty set if value is None
|
|
560
251
|
if value is None:
|
|
561
252
|
value = set()
|
|
562
|
-
|
|
563
|
-
# Extract item type from set annotation
|
|
564
|
-
try:
|
|
565
|
-
item_type = get_args(annotation)[0] # Get type of set items
|
|
566
|
-
except (IndexError, TypeError):
|
|
567
|
-
item_type = Any
|
|
568
|
-
|
|
569
|
-
# Check if item_type is an enum or literal - if so, we know all possible values
|
|
253
|
+
item_type = get_inner_type(field_info)
|
|
570
254
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
571
255
|
item_type
|
|
572
256
|
):
|
|
@@ -579,7 +263,6 @@ def render_set_field(
|
|
|
579
263
|
help=help,
|
|
580
264
|
)
|
|
581
265
|
|
|
582
|
-
# For open-ended sets, use a modified sequence renderer that ensures uniqueness
|
|
583
266
|
return render_open_set_field(
|
|
584
267
|
key=key,
|
|
585
268
|
value=value,
|
|
@@ -601,15 +284,12 @@ def render_set_with_known_domain(
|
|
|
601
284
|
help: str | None = None, # noqa: A002
|
|
602
285
|
) -> set[Any]:
|
|
603
286
|
"""Render a set field using a multiselect when domain is known (enum or literal)."""
|
|
604
|
-
# Get all possible values
|
|
605
287
|
if isinstance(item_type, type) and issubclass(item_type, Enum):
|
|
606
288
|
all_options = list(item_type.__members__.values())
|
|
607
289
|
format_func = lambda x: x.name # noqa: E731
|
|
608
290
|
else: # Literal type
|
|
609
291
|
all_options = list(get_args(item_type))
|
|
610
292
|
format_func = str
|
|
611
|
-
|
|
612
|
-
# Use multi-select for known domain
|
|
613
293
|
selected = st.multiselect(
|
|
614
294
|
label=label or key,
|
|
615
295
|
options=all_options,
|
|
@@ -633,24 +313,17 @@ def render_open_set_field(
|
|
|
633
313
|
**field_info: Any,
|
|
634
314
|
) -> set[Any]:
|
|
635
315
|
"""Render an open-ended set field with uniqueness enforcement."""
|
|
636
|
-
# Create unique state keys for this field
|
|
637
316
|
add_item_key = f"{key}_add_item"
|
|
638
317
|
items_key = f"{key}_items"
|
|
639
|
-
|
|
640
|
-
# Initialize session state for this field
|
|
641
318
|
if items_key not in st.session_state:
|
|
642
319
|
st.session_state[items_key] = list(value)
|
|
643
|
-
|
|
644
|
-
# Create container for set elements
|
|
645
320
|
st.markdown(f"**{label or key}**")
|
|
646
321
|
if help:
|
|
647
322
|
st.caption(help)
|
|
648
|
-
|
|
649
323
|
with st.container():
|
|
650
324
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
651
325
|
temp_list: list[Any] = []
|
|
652
326
|
add_new_item(temp_list, item_type)
|
|
653
|
-
|
|
654
327
|
# If the temp_list has an item and it's not a duplicate, add it to our items
|
|
655
328
|
if temp_list and str(temp_list[0]) not in {
|
|
656
329
|
str(i) for i in st.session_state[items_key]
|
|
@@ -683,17 +356,13 @@ def render_set_items(
|
|
|
683
356
|
"""Render items in a set with delete buttons and uniqueness enforcement."""
|
|
684
357
|
item_info = field_info.copy()
|
|
685
358
|
item_info["type"] = item_type
|
|
686
|
-
item_info["inside_expander"] = True
|
|
687
|
-
|
|
359
|
+
item_info["inside_expander"] = True
|
|
688
360
|
try:
|
|
689
361
|
renderer = get_field_renderer(item_info)
|
|
690
362
|
items_to_delete = []
|
|
691
|
-
|
|
692
363
|
for i, item in enumerate(items):
|
|
693
364
|
st.divider()
|
|
694
365
|
st.markdown(f"**Item {i + 1}**")
|
|
695
|
-
|
|
696
|
-
# Render the item
|
|
697
366
|
updated_item = renderer(
|
|
698
367
|
key=f"{key}_item_{i}",
|
|
699
368
|
value=item,
|
|
@@ -701,28 +370,23 @@ def render_set_items(
|
|
|
701
370
|
disabled=disabled,
|
|
702
371
|
**item_info,
|
|
703
372
|
)
|
|
704
|
-
|
|
705
|
-
# Check if this update would create a duplicate
|
|
706
373
|
duplicate = False
|
|
707
374
|
for j, other_item in enumerate(items):
|
|
708
375
|
if i != j and str(updated_item) == str(other_item):
|
|
709
376
|
duplicate = True
|
|
710
377
|
break
|
|
711
|
-
|
|
712
378
|
if duplicate:
|
|
713
379
|
st.error("This value would create a duplicate in the set.")
|
|
714
380
|
else:
|
|
715
381
|
items[i] = updated_item
|
|
716
|
-
|
|
717
382
|
delete_key = f"{key}_delete_{i}"
|
|
718
383
|
if st.button("Delete Item", key=delete_key, disabled=disabled):
|
|
719
384
|
items_to_delete.append(i)
|
|
720
|
-
|
|
721
385
|
if items_to_delete:
|
|
722
386
|
for idx in sorted(items_to_delete, reverse=True):
|
|
723
387
|
if 0 <= idx < len(items):
|
|
724
388
|
items.pop(idx)
|
|
725
|
-
st.rerun()
|
|
389
|
+
st.rerun()
|
|
726
390
|
|
|
727
391
|
except Exception as e: # noqa: BLE001
|
|
728
392
|
st.error(f"Error rendering set items: {e!s}")
|
|
@@ -734,10 +398,7 @@ def display_set_readonly(value, field_type, key=None):
|
|
|
734
398
|
st.text("No items")
|
|
735
399
|
return
|
|
736
400
|
|
|
737
|
-
item_type =
|
|
738
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
739
|
-
item_type = get_args(field_type)[0]
|
|
740
|
-
|
|
401
|
+
item_type = get_inner_type(field_type)
|
|
741
402
|
# For known domain sets (enum or literal), show as comma-separated list
|
|
742
403
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
743
404
|
item_type
|
|
@@ -773,13 +434,8 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
773
434
|
help: str | None = None, # noqa: A002
|
|
774
435
|
**field_info: Any,
|
|
775
436
|
) -> Any:
|
|
776
|
-
# Create a layout for the field
|
|
777
437
|
enable_key = f"{key}_enable"
|
|
778
|
-
|
|
779
|
-
# Create columns for checkbox and content
|
|
780
438
|
cols = st.columns([0.1, 0.9])
|
|
781
|
-
|
|
782
|
-
# Toggle checkbox in first column
|
|
783
439
|
with cols[0]:
|
|
784
440
|
is_enabled = st.checkbox(
|
|
785
441
|
"Enable",
|
|
@@ -788,15 +444,11 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
788
444
|
disabled=disabled,
|
|
789
445
|
label_visibility="collapsed",
|
|
790
446
|
)
|
|
791
|
-
|
|
792
|
-
# Content in second column
|
|
793
447
|
with cols[1]:
|
|
794
448
|
# Show field label
|
|
795
449
|
st.markdown(f"**{label or key}**")
|
|
796
450
|
if help:
|
|
797
451
|
st.caption(help)
|
|
798
|
-
|
|
799
|
-
# If enabled, render the field; otherwise show "None"
|
|
800
452
|
if is_enabled:
|
|
801
453
|
return renderer(
|
|
802
454
|
key=key,
|
|
@@ -837,12 +489,10 @@ def render_model_instance_field(
|
|
|
837
489
|
st.error(error_msg)
|
|
838
490
|
return None
|
|
839
491
|
|
|
840
|
-
# Show a header for the nested model with help text if available
|
|
841
492
|
st.markdown(f"**{label or key}**")
|
|
842
493
|
if help:
|
|
843
494
|
st.caption(help)
|
|
844
495
|
|
|
845
|
-
# Use an expander for the nested model fields
|
|
846
496
|
with st.expander("Edit", expanded=True):
|
|
847
497
|
updated_value = {}
|
|
848
498
|
try:
|
|
@@ -853,10 +503,8 @@ def render_model_instance_field(
|
|
|
853
503
|
nested_field_info = {"name": field_name, "type": field.type}
|
|
854
504
|
if field_help:
|
|
855
505
|
nested_field_info["help"] = field_help
|
|
856
|
-
|
|
857
506
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
858
507
|
nested_field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
859
|
-
|
|
860
508
|
renderer = get_field_renderer(nested_field_info)
|
|
861
509
|
updated_value[field_name] = renderer(
|
|
862
510
|
key=f"{key}_{field_name}",
|
|
@@ -872,42 +520,28 @@ def render_model_instance_field(
|
|
|
872
520
|
return value
|
|
873
521
|
|
|
874
522
|
|
|
875
|
-
# Mapping of Python types to render functions
|
|
876
|
-
PRIMITIVE_RENDERERS = {
|
|
877
|
-
str: render_str_field,
|
|
878
|
-
bool: render_bool_field,
|
|
879
|
-
int: render_int_field,
|
|
880
|
-
float: render_float_field,
|
|
881
|
-
Decimal: render_float_field,
|
|
882
|
-
date: render_date_field,
|
|
883
|
-
time: render_time_field,
|
|
884
|
-
datetime: render_datetime_field,
|
|
885
|
-
Enum: render_enum_field,
|
|
886
|
-
Literal: render_literal_field,
|
|
887
|
-
SecretStr: render_secret_str_field,
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
|
|
891
523
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
892
524
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
893
525
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
526
|
+
json_schema_extra = field_info.get("json_schema_extra", {})
|
|
527
|
+
if (
|
|
528
|
+
json_schema_extra
|
|
529
|
+
and (field_type := json_schema_extra.get("field_type"))
|
|
530
|
+
in FIELD_METADATA_RENDERERS
|
|
531
|
+
):
|
|
532
|
+
return FIELD_METADATA_RENDERERS[field_type]
|
|
894
533
|
if annotation is SecretStr or (
|
|
895
534
|
isinstance(annotation, type) and issubclass(annotation, SecretStr)
|
|
896
535
|
):
|
|
897
536
|
return render_secret_str_field
|
|
898
|
-
|
|
899
537
|
if is_literal_type(annotation):
|
|
900
538
|
return render_literal_field
|
|
901
|
-
|
|
902
539
|
if is_union_type(annotation):
|
|
903
540
|
return render_union_field
|
|
904
|
-
|
|
905
541
|
if is_set_type(annotation):
|
|
906
542
|
return render_set_field
|
|
907
|
-
|
|
908
543
|
if is_sequence_type(annotation):
|
|
909
544
|
return render_sequence_field
|
|
910
|
-
|
|
911
545
|
origin = get_origin(annotation)
|
|
912
546
|
if origin is not None:
|
|
913
547
|
args = get_args(annotation)
|
|
@@ -972,7 +606,6 @@ def render_model_readonly[T](
|
|
|
972
606
|
|
|
973
607
|
def render_field_readonly(label, value, field_type, description=None, key=None):
|
|
974
608
|
"""Render a single field in read-only mode."""
|
|
975
|
-
# Create a container with two columns: label and value
|
|
976
609
|
cols = st.columns([0.3, 0.7])
|
|
977
610
|
with cols[0]:
|
|
978
611
|
st.markdown(f"**{label}:**")
|
|
@@ -1031,9 +664,7 @@ def display_sequence_readonly(value, field_type, key=None):
|
|
|
1031
664
|
if not value: # Empty sequence
|
|
1032
665
|
st.text("No items")
|
|
1033
666
|
return
|
|
1034
|
-
item_type =
|
|
1035
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
1036
|
-
item_type = get_args(field_type)[0]
|
|
667
|
+
item_type = get_inner_type(field_type)
|
|
1037
668
|
for i, item in enumerate(value):
|
|
1038
669
|
with st.expander(f"Item {i + 1}", expanded=False):
|
|
1039
670
|
display_value_readonly(item, item_type, key=f"{key}_{i}" if key else None)
|
|
@@ -1061,27 +692,15 @@ def render_model_field(model_class, field_name, value=None, container=st):
|
|
|
1061
692
|
if field is None:
|
|
1062
693
|
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
1063
694
|
raise ValueError(error_msg)
|
|
1064
|
-
|
|
1065
|
-
# Extract field metadata
|
|
1066
695
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
1067
696
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
1068
697
|
field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
1069
|
-
|
|
1070
698
|
label = field_name.replace("_", " ").title()
|
|
1071
699
|
help_text = get_description(field)
|
|
1072
700
|
if help_text:
|
|
1073
701
|
field_info["help"] = help_text
|
|
1074
|
-
|
|
1075
702
|
renderer = get_field_renderer(field_info)
|
|
1076
|
-
return renderer(
|
|
1077
|
-
key=field_name,
|
|
1078
|
-
value=value,
|
|
1079
|
-
label=label,
|
|
1080
|
-
**field_info,
|
|
1081
|
-
)
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
TForm = TypeVar("TForm", bound="BaseModel")
|
|
703
|
+
return renderer(key=field_name, value=value, label=label, **field_info)
|
|
1085
704
|
|
|
1086
705
|
|
|
1087
706
|
@overload
|
|
@@ -1186,7 +805,6 @@ if __name__ == "__main__":
|
|
|
1186
805
|
long_text: str | None = "test " * 40
|
|
1187
806
|
"""Long text."""
|
|
1188
807
|
|
|
1189
|
-
# Fields that demonstrate the optional toggle
|
|
1190
808
|
optional_int: int | None = None
|
|
1191
809
|
"""An optional integer that can be None."""
|
|
1192
810
|
optional_string: str | None = None
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"""Primitive type renderers for Pydantic form fields."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
from datetime import date, datetime, time
|
|
7
|
+
from decimal import Decimal
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from typing import Any, get_args
|
|
10
|
+
|
|
11
|
+
from pydantic import SecretStr
|
|
12
|
+
import streamlit as st
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def render_str_field(
|
|
16
|
+
*,
|
|
17
|
+
key: str,
|
|
18
|
+
value: str | None = None,
|
|
19
|
+
label: str | None = None,
|
|
20
|
+
disabled: bool = False,
|
|
21
|
+
help: str | None = None, # noqa: A002
|
|
22
|
+
**field_info: Any,
|
|
23
|
+
) -> str:
|
|
24
|
+
"""Render a string field using appropriate Streamlit widget."""
|
|
25
|
+
max_length = field_info.get("max_length", 0)
|
|
26
|
+
multiple_lines = field_info.get("multiple_lines", False)
|
|
27
|
+
safe_value = "" if value is None else value
|
|
28
|
+
has_meaningful_content = safe_value.strip() != ""
|
|
29
|
+
has_line_breaks = has_meaningful_content and (
|
|
30
|
+
"\n" in safe_value or "\r" in safe_value
|
|
31
|
+
)
|
|
32
|
+
is_long_val = len(safe_value) > 100 # noqa: PLR2004
|
|
33
|
+
use_text_area = max_length > 100 or is_long_val or multiple_lines or has_line_breaks # noqa: PLR2004
|
|
34
|
+
|
|
35
|
+
if use_text_area:
|
|
36
|
+
return st.text_area(
|
|
37
|
+
label=label or key,
|
|
38
|
+
value=safe_value,
|
|
39
|
+
disabled=disabled,
|
|
40
|
+
key=key,
|
|
41
|
+
help=help,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return st.text_input(
|
|
45
|
+
label=label or key,
|
|
46
|
+
value=safe_value.strip() if has_meaningful_content else safe_value,
|
|
47
|
+
disabled=disabled,
|
|
48
|
+
key=key,
|
|
49
|
+
help=help,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def render_int_field(
|
|
54
|
+
*,
|
|
55
|
+
key: str,
|
|
56
|
+
value: int | None = None,
|
|
57
|
+
label: str | None = None,
|
|
58
|
+
disabled: bool = False,
|
|
59
|
+
help: str | None = None, # noqa: A002
|
|
60
|
+
**field_info: Any,
|
|
61
|
+
) -> int:
|
|
62
|
+
"""Render an integer field using Streamlit number_input."""
|
|
63
|
+
# Set default value
|
|
64
|
+
safe_value = int(value) if value is not None else 0
|
|
65
|
+
min_value = field_info.get("ge") or field_info.get("gt")
|
|
66
|
+
min_value = int(min_value) if min_value is not None else None
|
|
67
|
+
max_value = field_info.get("le") or field_info.get("lt")
|
|
68
|
+
max_value = int(max_value) if max_value is not None else None
|
|
69
|
+
step = field_info.get("multiple_of")
|
|
70
|
+
step = int(step) if step is not None else 1
|
|
71
|
+
result = st.number_input(
|
|
72
|
+
label=label or key,
|
|
73
|
+
value=safe_value,
|
|
74
|
+
min_value=min_value,
|
|
75
|
+
max_value=max_value,
|
|
76
|
+
step=step,
|
|
77
|
+
disabled=disabled,
|
|
78
|
+
key=key,
|
|
79
|
+
format="%d",
|
|
80
|
+
help=help,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return int(result)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def render_float_field(
|
|
87
|
+
*,
|
|
88
|
+
key: str,
|
|
89
|
+
value: float | Decimal | None = None,
|
|
90
|
+
label: str | None = None,
|
|
91
|
+
disabled: bool = False,
|
|
92
|
+
help: str | None = None, # noqa: A002
|
|
93
|
+
**field_info: Any,
|
|
94
|
+
) -> float | Decimal:
|
|
95
|
+
"""Render a float or Decimal field using Streamlit number_input."""
|
|
96
|
+
field_type = field_info.get("type")
|
|
97
|
+
is_decimal = field_type is Decimal
|
|
98
|
+
safe_value = float(value) if value is not None else 0.0
|
|
99
|
+
min_value = field_info.get("ge") or field_info.get("gt")
|
|
100
|
+
min_value = float(min_value) if min_value is not None else None
|
|
101
|
+
max_value = field_info.get("le") or field_info.get("lt")
|
|
102
|
+
max_value = float(max_value) if max_value is not None else None
|
|
103
|
+
step = field_info.get("multiple_of")
|
|
104
|
+
step = float(step) if step is not None else 0.01
|
|
105
|
+
result = st.number_input(
|
|
106
|
+
label=label or key,
|
|
107
|
+
value=safe_value,
|
|
108
|
+
min_value=min_value,
|
|
109
|
+
max_value=max_value,
|
|
110
|
+
step=step,
|
|
111
|
+
disabled=disabled,
|
|
112
|
+
key=key,
|
|
113
|
+
help=help,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if is_decimal:
|
|
117
|
+
return Decimal(str(result))
|
|
118
|
+
|
|
119
|
+
return result
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def render_bool_field(
|
|
123
|
+
*,
|
|
124
|
+
key: str,
|
|
125
|
+
value: bool | None = None,
|
|
126
|
+
label: str | None = None,
|
|
127
|
+
disabled: bool = False,
|
|
128
|
+
help: str | None = None, # noqa: A002
|
|
129
|
+
**field_info: Any,
|
|
130
|
+
) -> bool:
|
|
131
|
+
"""Render a boolean field using appropriate Streamlit widget."""
|
|
132
|
+
return st.checkbox(
|
|
133
|
+
label=label or key,
|
|
134
|
+
value=value if value is not None else False,
|
|
135
|
+
disabled=disabled,
|
|
136
|
+
key=key,
|
|
137
|
+
help=help,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def render_date_field(
|
|
142
|
+
*,
|
|
143
|
+
key: str,
|
|
144
|
+
value: date | None = None,
|
|
145
|
+
label: str | None = None,
|
|
146
|
+
disabled: bool = False,
|
|
147
|
+
help: str | None = None, # noqa: A002
|
|
148
|
+
**field_info: Any,
|
|
149
|
+
) -> date:
|
|
150
|
+
"""Render a date field using appropriate Streamlit widget."""
|
|
151
|
+
return st.date_input(
|
|
152
|
+
label=label or key,
|
|
153
|
+
value=value or date.today(),
|
|
154
|
+
disabled=disabled,
|
|
155
|
+
key=key,
|
|
156
|
+
help=help,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def render_time_field(
|
|
161
|
+
*,
|
|
162
|
+
key: str,
|
|
163
|
+
value: time | None = None,
|
|
164
|
+
label: str | None = None,
|
|
165
|
+
disabled: bool = False,
|
|
166
|
+
help: str | None = None, # noqa: A002
|
|
167
|
+
**field_info: Any,
|
|
168
|
+
) -> time:
|
|
169
|
+
"""Render a time field using appropriate Streamlit widget."""
|
|
170
|
+
return st.time_input(
|
|
171
|
+
label=label or key,
|
|
172
|
+
value=value or datetime.now().time(),
|
|
173
|
+
disabled=disabled,
|
|
174
|
+
key=key,
|
|
175
|
+
help=help,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def render_enum_field(
|
|
180
|
+
*,
|
|
181
|
+
key: str,
|
|
182
|
+
value: Enum | None = None,
|
|
183
|
+
label: str | None = None,
|
|
184
|
+
disabled: bool = False,
|
|
185
|
+
help: str | None = None, # noqa: A002
|
|
186
|
+
**field_info: Any,
|
|
187
|
+
) -> Enum:
|
|
188
|
+
"""Render an enum field using appropriate Streamlit widget."""
|
|
189
|
+
enum_class = field_info.get("enum_class") or field_info.get("type")
|
|
190
|
+
if enum_class is None or not issubclass(enum_class, Enum):
|
|
191
|
+
error_msg = f"Invalid enum class for field {key}"
|
|
192
|
+
raise TypeError(error_msg)
|
|
193
|
+
options = list(enum_class.__members__.values())
|
|
194
|
+
if not options:
|
|
195
|
+
return None # type: ignore
|
|
196
|
+
index = 0
|
|
197
|
+
if value is not None:
|
|
198
|
+
with contextlib.suppress(ValueError):
|
|
199
|
+
index = options.index(value)
|
|
200
|
+
return st.selectbox(
|
|
201
|
+
label=label or key,
|
|
202
|
+
options=options,
|
|
203
|
+
index=index,
|
|
204
|
+
disabled=disabled,
|
|
205
|
+
key=key,
|
|
206
|
+
help=help,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def render_secret_str_field(
|
|
211
|
+
*,
|
|
212
|
+
key: str,
|
|
213
|
+
value: SecretStr | None = None,
|
|
214
|
+
label: str | None = None,
|
|
215
|
+
disabled: bool = False,
|
|
216
|
+
help: str | None = None, # noqa: A002
|
|
217
|
+
**field_info: Any,
|
|
218
|
+
) -> SecretStr:
|
|
219
|
+
"""Render a SecretStr field using a password input."""
|
|
220
|
+
text = st.text_input(
|
|
221
|
+
label=label or key,
|
|
222
|
+
value=value.get_secret_value() if value else "",
|
|
223
|
+
disabled=disabled,
|
|
224
|
+
key=key,
|
|
225
|
+
help=help,
|
|
226
|
+
type="password",
|
|
227
|
+
)
|
|
228
|
+
return SecretStr(text)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def render_datetime_field(
|
|
232
|
+
*,
|
|
233
|
+
key: str,
|
|
234
|
+
value: datetime | None = None,
|
|
235
|
+
label: str | None = None,
|
|
236
|
+
disabled: bool = False,
|
|
237
|
+
help: str | None = None, # noqa: A002
|
|
238
|
+
**field_info: Any,
|
|
239
|
+
) -> datetime:
|
|
240
|
+
"""Render a datetime field using date and time inputs."""
|
|
241
|
+
current_date = value.date() if value else date.today()
|
|
242
|
+
current_time = value.time() if value else datetime.now().time()
|
|
243
|
+
date_col, time_col = st.columns(2)
|
|
244
|
+
with date_col:
|
|
245
|
+
selected_date = st.date_input(
|
|
246
|
+
label=f"{label or key} (Date)",
|
|
247
|
+
value=current_date,
|
|
248
|
+
disabled=disabled,
|
|
249
|
+
key=f"{key}_date",
|
|
250
|
+
help=help,
|
|
251
|
+
)
|
|
252
|
+
with time_col:
|
|
253
|
+
selected_time = st.time_input(
|
|
254
|
+
label=f"{label or key} (Time)",
|
|
255
|
+
value=current_time,
|
|
256
|
+
disabled=disabled,
|
|
257
|
+
key=f"{key}_time",
|
|
258
|
+
help=help,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
return datetime.combine(selected_date, selected_time)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def render_literal_field(
|
|
265
|
+
*,
|
|
266
|
+
key: str,
|
|
267
|
+
value: Any = None,
|
|
268
|
+
label: str | None = None,
|
|
269
|
+
disabled: bool = False,
|
|
270
|
+
help: str | None = None, # noqa: A002
|
|
271
|
+
**field_info: Any,
|
|
272
|
+
) -> Any:
|
|
273
|
+
"""Render a Literal field using appropriate Streamlit widget."""
|
|
274
|
+
annotation = field_info.get("type") or field_info.get("annotation")
|
|
275
|
+
options = get_args(annotation)
|
|
276
|
+
if len(options) == 1:
|
|
277
|
+
return options[0]
|
|
278
|
+
if all(isinstance(opt, bool) for opt in options):
|
|
279
|
+
index = options.index(value) if value in options else 0
|
|
280
|
+
return st.radio(
|
|
281
|
+
label=label or key,
|
|
282
|
+
options=options,
|
|
283
|
+
index=index,
|
|
284
|
+
disabled=disabled,
|
|
285
|
+
key=key,
|
|
286
|
+
horizontal=True,
|
|
287
|
+
help=help,
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
# Use selectbox for other literals
|
|
291
|
+
index = options.index(value) if value in options else 0
|
|
292
|
+
return st.selectbox(
|
|
293
|
+
label=label or key,
|
|
294
|
+
options=options,
|
|
295
|
+
index=index,
|
|
296
|
+
disabled=disabled,
|
|
297
|
+
key=key,
|
|
298
|
+
help=help,
|
|
299
|
+
)
|
|
@@ -225,3 +225,11 @@ def add_new_item(items_list: list, item_type: Any) -> None:
|
|
|
225
225
|
else:
|
|
226
226
|
# For unknown types, add None
|
|
227
227
|
items_list.append(None)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def get_inner_type(field_info: Any) -> Any:
|
|
231
|
+
annotation = field_info.get("type") or field_info.get("annotation")
|
|
232
|
+
try:
|
|
233
|
+
return get_args(annotation)[0] # Get type of set items
|
|
234
|
+
except (IndexError, TypeError):
|
|
235
|
+
return Any
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|