streambricks 0.2.10__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.10 → streambricks-0.2.11}/PKG-INFO +1 -1
- {streambricks-0.2.10 → 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.10/src/streambricks/widgets/model_widget.py → streambricks-0.2.11/src/streambricks/widgets/model_widget/main.py +43 -460
- streambricks-0.2.11/src/streambricks/widgets/model_widget/type_renderers.py +299 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/widgets/type_helpers.py +8 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.copier-answers.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.github/dependabot.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.gitignore +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/LICENSE +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/README.md +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/docs/.empty +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/duties.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/mkdocs.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/pyproject.toml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/state.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/tests/__init__.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.11}/tests/conftest.py +0 -0
- {streambricks-0.2.10 → 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,349 +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
|
-
FIELD_METADATA_RENDERERS: dict[str, WidgetFunc[Any]] = {}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def register_field_renderer(field_type: str, renderer: WidgetFunc[Any]) -> None:
|
|
39
|
-
"""Register a custom field renderer for a specific field_type."""
|
|
40
|
-
FIELD_METADATA_RENDERERS[field_type] = renderer
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def render_model_identifier_field(
|
|
44
|
-
*,
|
|
45
|
-
key: str,
|
|
46
|
-
value: str | None = None,
|
|
47
|
-
label: str | None = None,
|
|
48
|
-
disabled: bool = False,
|
|
49
|
-
help: str | None = None, # noqa: A002
|
|
50
|
-
**field_info: Any,
|
|
51
|
-
) -> str:
|
|
52
|
-
"""Render a model identifier field using model_selector widget."""
|
|
53
|
-
from streambricks.widgets.model_selector import model_selector
|
|
54
|
-
|
|
55
|
-
providers = field_info.get("providers")
|
|
56
|
-
selected_model = model_selector(value=value, providers=providers, expanded=False)
|
|
57
|
-
return selected_model.pydantic_ai_id if selected_model else value or ""
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
register_field_renderer("model_identifier", render_model_identifier_field)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def render_str_field(
|
|
64
|
-
*,
|
|
65
|
-
key: str,
|
|
66
|
-
value: str | None = None,
|
|
67
|
-
label: str | None = None,
|
|
68
|
-
disabled: bool = False,
|
|
69
|
-
help: str | None = None, # noqa: A002
|
|
70
|
-
**field_info: Any,
|
|
71
|
-
) -> str:
|
|
72
|
-
"""Render a string field using appropriate Streamlit widget."""
|
|
73
|
-
max_length = field_info.get("max_length", 0)
|
|
74
|
-
multiple_lines = field_info.get("multiple_lines", False)
|
|
75
|
-
safe_value = "" if value is None else value
|
|
76
|
-
has_meaningful_content = safe_value.strip() != ""
|
|
77
|
-
has_line_breaks = has_meaningful_content and (
|
|
78
|
-
"\n" in safe_value or "\r" in safe_value
|
|
79
|
-
)
|
|
80
|
-
is_long_val = len(safe_value) > 100 # noqa: PLR2004
|
|
81
|
-
use_text_area = max_length > 100 or is_long_val or multiple_lines or has_line_breaks # noqa: PLR2004
|
|
82
|
-
|
|
83
|
-
if use_text_area:
|
|
84
|
-
return st.text_area(
|
|
85
|
-
label=label or key,
|
|
86
|
-
value=safe_value,
|
|
87
|
-
disabled=disabled,
|
|
88
|
-
key=key,
|
|
89
|
-
help=help,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
# For single-line inputs, strip whitespace as it's usually unintentional
|
|
93
|
-
return st.text_input(
|
|
94
|
-
label=label or key,
|
|
95
|
-
value=safe_value.strip() if has_meaningful_content else safe_value,
|
|
96
|
-
disabled=disabled,
|
|
97
|
-
key=key,
|
|
98
|
-
help=help,
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def render_int_field(
|
|
103
|
-
*,
|
|
104
|
-
key: str,
|
|
105
|
-
value: int | None = None,
|
|
106
|
-
label: str | None = None,
|
|
107
|
-
disabled: bool = False,
|
|
108
|
-
help: str | None = None, # noqa: A002
|
|
109
|
-
**field_info: Any,
|
|
110
|
-
) -> int:
|
|
111
|
-
"""Render an integer field using Streamlit number_input."""
|
|
112
|
-
# Set default value
|
|
113
|
-
safe_value = int(value) if value is not None else 0
|
|
114
|
-
|
|
115
|
-
# Extract constraints, ensuring they're integers
|
|
116
|
-
min_value = field_info.get("ge") or field_info.get("gt")
|
|
117
|
-
min_value = int(min_value) if min_value is not None else None
|
|
118
|
-
|
|
119
|
-
max_value = field_info.get("le") or field_info.get("lt")
|
|
120
|
-
max_value = int(max_value) if max_value is not None else None
|
|
121
|
-
|
|
122
|
-
step = field_info.get("multiple_of")
|
|
123
|
-
step = int(step) if step is not None else 1
|
|
124
|
-
|
|
125
|
-
result = st.number_input(
|
|
126
|
-
label=label or key,
|
|
127
|
-
value=safe_value,
|
|
128
|
-
min_value=min_value,
|
|
129
|
-
max_value=max_value,
|
|
130
|
-
step=step,
|
|
131
|
-
disabled=disabled,
|
|
132
|
-
key=key,
|
|
133
|
-
format="%d",
|
|
134
|
-
help=help,
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
return int(result)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
def render_float_field(
|
|
141
|
-
*,
|
|
142
|
-
key: str,
|
|
143
|
-
value: float | Decimal | None = None,
|
|
144
|
-
label: str | None = None,
|
|
145
|
-
disabled: bool = False,
|
|
146
|
-
help: str | None = None, # noqa: A002
|
|
147
|
-
**field_info: Any,
|
|
148
|
-
) -> float | Decimal:
|
|
149
|
-
"""Render a float or Decimal field using Streamlit number_input."""
|
|
150
|
-
# Determine if we're dealing with a Decimal
|
|
151
|
-
field_type = field_info.get("type")
|
|
152
|
-
is_decimal = field_type is Decimal
|
|
153
|
-
|
|
154
|
-
# Convert to float for Streamlit compatibility
|
|
155
|
-
safe_value = float(value) if value is not None else 0.0
|
|
156
|
-
|
|
157
|
-
# Extract constraints, ensuring they're floats
|
|
158
|
-
min_value = field_info.get("ge") or field_info.get("gt")
|
|
159
|
-
min_value = float(min_value) if min_value is not None else None
|
|
160
|
-
|
|
161
|
-
max_value = field_info.get("le") or field_info.get("lt")
|
|
162
|
-
max_value = float(max_value) if max_value is not None else None
|
|
163
|
-
|
|
164
|
-
step = field_info.get("multiple_of")
|
|
165
|
-
step = float(step) if step is not None else 0.01
|
|
166
|
-
|
|
167
|
-
result = st.number_input(
|
|
168
|
-
label=label or key,
|
|
169
|
-
value=safe_value,
|
|
170
|
-
min_value=min_value,
|
|
171
|
-
max_value=max_value,
|
|
172
|
-
step=step,
|
|
173
|
-
disabled=disabled,
|
|
174
|
-
key=key,
|
|
175
|
-
help=help,
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
# Convert back to Decimal if needed
|
|
179
|
-
if is_decimal:
|
|
180
|
-
return Decimal(str(result))
|
|
181
|
-
|
|
182
|
-
return result
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
def render_bool_field(
|
|
186
|
-
*,
|
|
187
|
-
key: str,
|
|
188
|
-
value: bool | None = None,
|
|
189
|
-
label: str | None = None,
|
|
190
|
-
disabled: bool = False,
|
|
191
|
-
help: str | None = None, # noqa: A002
|
|
192
|
-
**field_info: Any,
|
|
193
|
-
) -> bool:
|
|
194
|
-
"""Render a boolean field using appropriate Streamlit widget."""
|
|
195
|
-
return st.checkbox(
|
|
196
|
-
label=label or key,
|
|
197
|
-
value=value if value is not None else False,
|
|
198
|
-
disabled=disabled,
|
|
199
|
-
key=key,
|
|
200
|
-
help=help,
|
|
201
|
-
)
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
def render_date_field(
|
|
205
|
-
*,
|
|
206
|
-
key: str,
|
|
207
|
-
value: date | None = None,
|
|
208
|
-
label: str | None = None,
|
|
209
|
-
disabled: bool = False,
|
|
210
|
-
help: str | None = None, # noqa: A002
|
|
211
|
-
**field_info: Any,
|
|
212
|
-
) -> date:
|
|
213
|
-
"""Render a date field using appropriate Streamlit widget."""
|
|
214
|
-
return st.date_input(
|
|
215
|
-
label=label or key,
|
|
216
|
-
value=value or date.today(),
|
|
217
|
-
disabled=disabled,
|
|
218
|
-
key=key,
|
|
219
|
-
help=help,
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
def render_time_field(
|
|
224
|
-
*,
|
|
225
|
-
key: str,
|
|
226
|
-
value: time | None = None,
|
|
227
|
-
label: str | None = None,
|
|
228
|
-
disabled: bool = False,
|
|
229
|
-
help: str | None = None, # noqa: A002
|
|
230
|
-
**field_info: Any,
|
|
231
|
-
) -> time:
|
|
232
|
-
"""Render a time field using appropriate Streamlit widget."""
|
|
233
|
-
return st.time_input(
|
|
234
|
-
label=label or key,
|
|
235
|
-
value=value or datetime.now().time(),
|
|
236
|
-
disabled=disabled,
|
|
237
|
-
key=key,
|
|
238
|
-
help=help,
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
def render_enum_field(
|
|
243
|
-
*,
|
|
244
|
-
key: str,
|
|
245
|
-
value: Enum | None = None,
|
|
246
|
-
label: str | None = None,
|
|
247
|
-
disabled: bool = False,
|
|
248
|
-
help: str | None = None, # noqa: A002
|
|
249
|
-
**field_info: Any,
|
|
250
|
-
) -> Enum:
|
|
251
|
-
"""Render an enum field using appropriate Streamlit widget."""
|
|
252
|
-
enum_class = field_info.get("enum_class") or field_info.get("type")
|
|
253
|
-
if enum_class is None or not issubclass(enum_class, Enum):
|
|
254
|
-
error_msg = f"Invalid enum class for field {key}"
|
|
255
|
-
raise TypeError(error_msg)
|
|
256
|
-
|
|
257
|
-
options = list(enum_class.__members__.values())
|
|
258
|
-
|
|
259
|
-
if not options:
|
|
260
|
-
return None # type: ignore
|
|
261
|
-
|
|
262
|
-
index = 0
|
|
263
|
-
if value is not None:
|
|
264
|
-
with contextlib.suppress(ValueError):
|
|
265
|
-
index = options.index(value)
|
|
266
|
-
|
|
267
|
-
return st.selectbox(
|
|
268
|
-
label=label or key,
|
|
269
|
-
options=options,
|
|
270
|
-
index=index,
|
|
271
|
-
disabled=disabled,
|
|
272
|
-
key=key,
|
|
273
|
-
help=help,
|
|
274
|
-
)
|
|
275
51
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
key: str,
|
|
280
|
-
value: SecretStr | None = None,
|
|
281
|
-
label: str | None = None,
|
|
282
|
-
disabled: bool = False,
|
|
283
|
-
help: str | None = None, # noqa: A002
|
|
284
|
-
**field_info: Any,
|
|
285
|
-
) -> SecretStr:
|
|
286
|
-
"""Render a SecretStr field using a password input."""
|
|
287
|
-
text = st.text_input(
|
|
288
|
-
label=label or key,
|
|
289
|
-
value=value.get_secret_value() if value else "",
|
|
290
|
-
disabled=disabled,
|
|
291
|
-
key=key,
|
|
292
|
-
help=help,
|
|
293
|
-
type="password",
|
|
294
|
-
)
|
|
295
|
-
return SecretStr(text)
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
def render_datetime_field(
|
|
299
|
-
*,
|
|
300
|
-
key: str,
|
|
301
|
-
value: datetime | None = None,
|
|
302
|
-
label: str | None = None,
|
|
303
|
-
disabled: bool = False,
|
|
304
|
-
help: str | None = None, # noqa: A002
|
|
305
|
-
**field_info: Any,
|
|
306
|
-
) -> datetime:
|
|
307
|
-
"""Render a datetime field using date and time inputs."""
|
|
308
|
-
# Split into date and time components
|
|
309
|
-
current_date = value.date() if value else date.today()
|
|
310
|
-
current_time = value.time() if value else datetime.now().time()
|
|
311
|
-
|
|
312
|
-
# Create columns for date and time
|
|
313
|
-
date_col, time_col = st.columns(2)
|
|
314
|
-
|
|
315
|
-
with date_col:
|
|
316
|
-
selected_date = st.date_input(
|
|
317
|
-
label=f"{label or key} (Date)",
|
|
318
|
-
value=current_date,
|
|
319
|
-
disabled=disabled,
|
|
320
|
-
key=f"{key}_date",
|
|
321
|
-
help=help,
|
|
322
|
-
)
|
|
323
|
-
|
|
324
|
-
with time_col:
|
|
325
|
-
selected_time = st.time_input(
|
|
326
|
-
label=f"{label or key} (Time)",
|
|
327
|
-
value=current_time,
|
|
328
|
-
disabled=disabled,
|
|
329
|
-
key=f"{key}_time",
|
|
330
|
-
help=help,
|
|
331
|
-
)
|
|
332
|
-
|
|
333
|
-
# Combine date and time
|
|
334
|
-
return datetime.combine(selected_date, selected_time)
|
|
52
|
+
FIELD_METADATA_RENDERERS: dict[str, WidgetFunc[Any]] = {
|
|
53
|
+
"model_identifier": render_model_id_field
|
|
54
|
+
}
|
|
335
55
|
|
|
336
56
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
if len(options) == 1:
|
|
352
|
-
return options[0]
|
|
353
|
-
|
|
354
|
-
# Use radio for boolean literals
|
|
355
|
-
if all(isinstance(opt, bool) for opt in options):
|
|
356
|
-
index = options.index(value) if value in options else 0
|
|
357
|
-
return st.radio(
|
|
358
|
-
label=label or key,
|
|
359
|
-
options=options,
|
|
360
|
-
index=index,
|
|
361
|
-
disabled=disabled,
|
|
362
|
-
key=key,
|
|
363
|
-
horizontal=True,
|
|
364
|
-
help=help,
|
|
365
|
-
)
|
|
366
|
-
|
|
367
|
-
# Use selectbox for other literals
|
|
368
|
-
index = options.index(value) if value in options else 0
|
|
369
|
-
return st.selectbox(
|
|
370
|
-
label=label or key,
|
|
371
|
-
options=options,
|
|
372
|
-
index=index,
|
|
373
|
-
disabled=disabled,
|
|
374
|
-
key=key,
|
|
375
|
-
help=help,
|
|
376
|
-
)
|
|
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
|
+
}
|
|
377
71
|
|
|
378
72
|
|
|
379
73
|
def render_union_field( # noqa: PLR0911
|
|
@@ -388,13 +82,10 @@ def render_union_field( # noqa: PLR0911
|
|
|
388
82
|
"""Render a field that can accept multiple types."""
|
|
389
83
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
390
84
|
possible_types = get_args(annotation)
|
|
391
|
-
|
|
392
|
-
# Create type selector
|
|
393
85
|
type_key = f"{key}_type"
|
|
394
86
|
type_names = [
|
|
395
87
|
t.__name__ if hasattr(t, "__name__") else str(t) for t in possible_types
|
|
396
88
|
]
|
|
397
|
-
|
|
398
89
|
selected_type_name = st.selectbox(
|
|
399
90
|
f"Type for {label or key}",
|
|
400
91
|
options=type_names,
|
|
@@ -403,19 +94,13 @@ def render_union_field( # noqa: PLR0911
|
|
|
403
94
|
help=help,
|
|
404
95
|
)
|
|
405
96
|
|
|
406
|
-
# Find selected type
|
|
407
97
|
selected_type_index = type_names.index(selected_type_name)
|
|
408
98
|
selected_type = possible_types[selected_type_index]
|
|
409
|
-
|
|
410
|
-
# Create field for selected type
|
|
411
99
|
field_key = f"{key}_value"
|
|
412
100
|
modified_field_info = field_info.copy()
|
|
413
101
|
modified_field_info["type"] = selected_type
|
|
414
|
-
|
|
415
|
-
# Only pass value if it matches the selected type or can be converted
|
|
416
102
|
typed_value: Any = None
|
|
417
103
|
if value is not None:
|
|
418
|
-
# Try to convert the value to the selected type
|
|
419
104
|
try:
|
|
420
105
|
if selected_type is int and isinstance(value, int | float):
|
|
421
106
|
typed_value = int(value)
|
|
@@ -435,13 +120,12 @@ def render_union_field( # noqa: PLR0911
|
|
|
435
120
|
result = renderer(
|
|
436
121
|
key=field_key,
|
|
437
122
|
value=typed_value,
|
|
438
|
-
label=f"Value ({selected_type_name})",
|
|
123
|
+
label=f"Value ({selected_type_name})",
|
|
439
124
|
disabled=disabled,
|
|
440
125
|
help=help,
|
|
441
126
|
**modified_field_info,
|
|
442
127
|
)
|
|
443
128
|
|
|
444
|
-
# Ensure the result is of the correct type
|
|
445
129
|
try:
|
|
446
130
|
if selected_type is int and not isinstance(result, int):
|
|
447
131
|
return int(result)
|
|
@@ -454,7 +138,6 @@ def render_union_field( # noqa: PLR0911
|
|
|
454
138
|
except (ValueError, TypeError) as e:
|
|
455
139
|
error_msg = f"Cannot convert {result} to {selected_type.__name__}: {e!s}"
|
|
456
140
|
st.error(error_msg)
|
|
457
|
-
# Return a default value for the selected type
|
|
458
141
|
if selected_type is int:
|
|
459
142
|
return 0
|
|
460
143
|
if selected_type is float:
|
|
@@ -488,33 +171,18 @@ def render_sequence_field(
|
|
|
488
171
|
**field_info: Any,
|
|
489
172
|
) -> list[Any]:
|
|
490
173
|
"""Render a field for sequence types (list, tuple, set)."""
|
|
491
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
492
|
-
|
|
493
|
-
# Create unique state keys for this field
|
|
494
174
|
add_item_key = f"{key}_add_item"
|
|
495
175
|
items_key = f"{key}_items"
|
|
496
|
-
|
|
497
|
-
# Initialize session state for this field
|
|
498
176
|
if items_key not in st.session_state:
|
|
499
177
|
st.session_state[items_key] = list(value) if value is not None else []
|
|
500
|
-
|
|
501
|
-
# Extract item type from sequence annotation
|
|
502
|
-
try:
|
|
503
|
-
item_type = get_args(annotation)[0] # Get type of sequence items
|
|
504
|
-
except (IndexError, TypeError):
|
|
505
|
-
item_type = Any
|
|
506
|
-
|
|
507
|
-
# Create container for sequence elements
|
|
178
|
+
item_type = get_inner_type(field_info)
|
|
508
179
|
st.markdown(f"**{label or key}**")
|
|
509
180
|
if help:
|
|
510
181
|
st.caption(help)
|
|
511
182
|
|
|
512
183
|
with st.container():
|
|
513
|
-
# Add new item button
|
|
514
184
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
515
185
|
add_new_item(st.session_state[items_key], item_type)
|
|
516
|
-
|
|
517
|
-
# Render items
|
|
518
186
|
render_sequence_items(
|
|
519
187
|
st.session_state[items_key],
|
|
520
188
|
item_type,
|
|
@@ -524,7 +192,6 @@ def render_sequence_field(
|
|
|
524
192
|
field_info,
|
|
525
193
|
)
|
|
526
194
|
|
|
527
|
-
# Return the current items
|
|
528
195
|
return st.session_state[items_key]
|
|
529
196
|
|
|
530
197
|
|
|
@@ -539,7 +206,7 @@ def render_sequence_items(
|
|
|
539
206
|
"""Render items in a sequence with delete buttons."""
|
|
540
207
|
item_info = field_info.copy()
|
|
541
208
|
item_info["type"] = item_type
|
|
542
|
-
item_info["inside_expander"] = True
|
|
209
|
+
item_info["inside_expander"] = True
|
|
543
210
|
|
|
544
211
|
try:
|
|
545
212
|
renderer = get_field_renderer(item_info)
|
|
@@ -581,19 +248,9 @@ def render_set_field(
|
|
|
581
248
|
**field_info: Any,
|
|
582
249
|
) -> set[Any]:
|
|
583
250
|
"""Render a field for set types with a multi-select interface when possible."""
|
|
584
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
585
|
-
|
|
586
|
-
# Initialize empty set if value is None
|
|
587
251
|
if value is None:
|
|
588
252
|
value = set()
|
|
589
|
-
|
|
590
|
-
# Extract item type from set annotation
|
|
591
|
-
try:
|
|
592
|
-
item_type = get_args(annotation)[0] # Get type of set items
|
|
593
|
-
except (IndexError, TypeError):
|
|
594
|
-
item_type = Any
|
|
595
|
-
|
|
596
|
-
# Check if item_type is an enum or literal - if so, we know all possible values
|
|
253
|
+
item_type = get_inner_type(field_info)
|
|
597
254
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
598
255
|
item_type
|
|
599
256
|
):
|
|
@@ -606,7 +263,6 @@ def render_set_field(
|
|
|
606
263
|
help=help,
|
|
607
264
|
)
|
|
608
265
|
|
|
609
|
-
# For open-ended sets, use a modified sequence renderer that ensures uniqueness
|
|
610
266
|
return render_open_set_field(
|
|
611
267
|
key=key,
|
|
612
268
|
value=value,
|
|
@@ -628,15 +284,12 @@ def render_set_with_known_domain(
|
|
|
628
284
|
help: str | None = None, # noqa: A002
|
|
629
285
|
) -> set[Any]:
|
|
630
286
|
"""Render a set field using a multiselect when domain is known (enum or literal)."""
|
|
631
|
-
# Get all possible values
|
|
632
287
|
if isinstance(item_type, type) and issubclass(item_type, Enum):
|
|
633
288
|
all_options = list(item_type.__members__.values())
|
|
634
289
|
format_func = lambda x: x.name # noqa: E731
|
|
635
290
|
else: # Literal type
|
|
636
291
|
all_options = list(get_args(item_type))
|
|
637
292
|
format_func = str
|
|
638
|
-
|
|
639
|
-
# Use multi-select for known domain
|
|
640
293
|
selected = st.multiselect(
|
|
641
294
|
label=label or key,
|
|
642
295
|
options=all_options,
|
|
@@ -660,24 +313,17 @@ def render_open_set_field(
|
|
|
660
313
|
**field_info: Any,
|
|
661
314
|
) -> set[Any]:
|
|
662
315
|
"""Render an open-ended set field with uniqueness enforcement."""
|
|
663
|
-
# Create unique state keys for this field
|
|
664
316
|
add_item_key = f"{key}_add_item"
|
|
665
317
|
items_key = f"{key}_items"
|
|
666
|
-
|
|
667
|
-
# Initialize session state for this field
|
|
668
318
|
if items_key not in st.session_state:
|
|
669
319
|
st.session_state[items_key] = list(value)
|
|
670
|
-
|
|
671
|
-
# Create container for set elements
|
|
672
320
|
st.markdown(f"**{label or key}**")
|
|
673
321
|
if help:
|
|
674
322
|
st.caption(help)
|
|
675
|
-
|
|
676
323
|
with st.container():
|
|
677
324
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
678
325
|
temp_list: list[Any] = []
|
|
679
326
|
add_new_item(temp_list, item_type)
|
|
680
|
-
|
|
681
327
|
# If the temp_list has an item and it's not a duplicate, add it to our items
|
|
682
328
|
if temp_list and str(temp_list[0]) not in {
|
|
683
329
|
str(i) for i in st.session_state[items_key]
|
|
@@ -710,17 +356,13 @@ def render_set_items(
|
|
|
710
356
|
"""Render items in a set with delete buttons and uniqueness enforcement."""
|
|
711
357
|
item_info = field_info.copy()
|
|
712
358
|
item_info["type"] = item_type
|
|
713
|
-
item_info["inside_expander"] = True
|
|
714
|
-
|
|
359
|
+
item_info["inside_expander"] = True
|
|
715
360
|
try:
|
|
716
361
|
renderer = get_field_renderer(item_info)
|
|
717
362
|
items_to_delete = []
|
|
718
|
-
|
|
719
363
|
for i, item in enumerate(items):
|
|
720
364
|
st.divider()
|
|
721
365
|
st.markdown(f"**Item {i + 1}**")
|
|
722
|
-
|
|
723
|
-
# Render the item
|
|
724
366
|
updated_item = renderer(
|
|
725
367
|
key=f"{key}_item_{i}",
|
|
726
368
|
value=item,
|
|
@@ -728,28 +370,23 @@ def render_set_items(
|
|
|
728
370
|
disabled=disabled,
|
|
729
371
|
**item_info,
|
|
730
372
|
)
|
|
731
|
-
|
|
732
|
-
# Check if this update would create a duplicate
|
|
733
373
|
duplicate = False
|
|
734
374
|
for j, other_item in enumerate(items):
|
|
735
375
|
if i != j and str(updated_item) == str(other_item):
|
|
736
376
|
duplicate = True
|
|
737
377
|
break
|
|
738
|
-
|
|
739
378
|
if duplicate:
|
|
740
379
|
st.error("This value would create a duplicate in the set.")
|
|
741
380
|
else:
|
|
742
381
|
items[i] = updated_item
|
|
743
|
-
|
|
744
382
|
delete_key = f"{key}_delete_{i}"
|
|
745
383
|
if st.button("Delete Item", key=delete_key, disabled=disabled):
|
|
746
384
|
items_to_delete.append(i)
|
|
747
|
-
|
|
748
385
|
if items_to_delete:
|
|
749
386
|
for idx in sorted(items_to_delete, reverse=True):
|
|
750
387
|
if 0 <= idx < len(items):
|
|
751
388
|
items.pop(idx)
|
|
752
|
-
st.rerun()
|
|
389
|
+
st.rerun()
|
|
753
390
|
|
|
754
391
|
except Exception as e: # noqa: BLE001
|
|
755
392
|
st.error(f"Error rendering set items: {e!s}")
|
|
@@ -761,10 +398,7 @@ def display_set_readonly(value, field_type, key=None):
|
|
|
761
398
|
st.text("No items")
|
|
762
399
|
return
|
|
763
400
|
|
|
764
|
-
item_type =
|
|
765
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
766
|
-
item_type = get_args(field_type)[0]
|
|
767
|
-
|
|
401
|
+
item_type = get_inner_type(field_type)
|
|
768
402
|
# For known domain sets (enum or literal), show as comma-separated list
|
|
769
403
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
770
404
|
item_type
|
|
@@ -800,13 +434,8 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
800
434
|
help: str | None = None, # noqa: A002
|
|
801
435
|
**field_info: Any,
|
|
802
436
|
) -> Any:
|
|
803
|
-
# Create a layout for the field
|
|
804
437
|
enable_key = f"{key}_enable"
|
|
805
|
-
|
|
806
|
-
# Create columns for checkbox and content
|
|
807
438
|
cols = st.columns([0.1, 0.9])
|
|
808
|
-
|
|
809
|
-
# Toggle checkbox in first column
|
|
810
439
|
with cols[0]:
|
|
811
440
|
is_enabled = st.checkbox(
|
|
812
441
|
"Enable",
|
|
@@ -815,15 +444,11 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
815
444
|
disabled=disabled,
|
|
816
445
|
label_visibility="collapsed",
|
|
817
446
|
)
|
|
818
|
-
|
|
819
|
-
# Content in second column
|
|
820
447
|
with cols[1]:
|
|
821
448
|
# Show field label
|
|
822
449
|
st.markdown(f"**{label or key}**")
|
|
823
450
|
if help:
|
|
824
451
|
st.caption(help)
|
|
825
|
-
|
|
826
|
-
# If enabled, render the field; otherwise show "None"
|
|
827
452
|
if is_enabled:
|
|
828
453
|
return renderer(
|
|
829
454
|
key=key,
|
|
@@ -864,12 +489,10 @@ def render_model_instance_field(
|
|
|
864
489
|
st.error(error_msg)
|
|
865
490
|
return None
|
|
866
491
|
|
|
867
|
-
# Show a header for the nested model with help text if available
|
|
868
492
|
st.markdown(f"**{label or key}**")
|
|
869
493
|
if help:
|
|
870
494
|
st.caption(help)
|
|
871
495
|
|
|
872
|
-
# Use an expander for the nested model fields
|
|
873
496
|
with st.expander("Edit", expanded=True):
|
|
874
497
|
updated_value = {}
|
|
875
498
|
try:
|
|
@@ -880,10 +503,8 @@ def render_model_instance_field(
|
|
|
880
503
|
nested_field_info = {"name": field_name, "type": field.type}
|
|
881
504
|
if field_help:
|
|
882
505
|
nested_field_info["help"] = field_help
|
|
883
|
-
|
|
884
506
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
885
507
|
nested_field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
886
|
-
|
|
887
508
|
renderer = get_field_renderer(nested_field_info)
|
|
888
509
|
updated_value[field_name] = renderer(
|
|
889
510
|
key=f"{key}_{field_name}",
|
|
@@ -899,26 +520,9 @@ def render_model_instance_field(
|
|
|
899
520
|
return value
|
|
900
521
|
|
|
901
522
|
|
|
902
|
-
# Mapping of Python types to render functions
|
|
903
|
-
PRIMITIVE_RENDERERS = {
|
|
904
|
-
str: render_str_field,
|
|
905
|
-
bool: render_bool_field,
|
|
906
|
-
int: render_int_field,
|
|
907
|
-
float: render_float_field,
|
|
908
|
-
Decimal: render_float_field,
|
|
909
|
-
date: render_date_field,
|
|
910
|
-
time: render_time_field,
|
|
911
|
-
datetime: render_datetime_field,
|
|
912
|
-
Enum: render_enum_field,
|
|
913
|
-
Literal: render_literal_field,
|
|
914
|
-
SecretStr: render_secret_str_field,
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
|
|
918
523
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
919
524
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
920
525
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
921
|
-
# Check for custom field type specification in json_schema_extra
|
|
922
526
|
json_schema_extra = field_info.get("json_schema_extra", {})
|
|
923
527
|
if (
|
|
924
528
|
json_schema_extra
|
|
@@ -930,19 +534,14 @@ def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa:
|
|
|
930
534
|
isinstance(annotation, type) and issubclass(annotation, SecretStr)
|
|
931
535
|
):
|
|
932
536
|
return render_secret_str_field
|
|
933
|
-
|
|
934
537
|
if is_literal_type(annotation):
|
|
935
538
|
return render_literal_field
|
|
936
|
-
|
|
937
539
|
if is_union_type(annotation):
|
|
938
540
|
return render_union_field
|
|
939
|
-
|
|
940
541
|
if is_set_type(annotation):
|
|
941
542
|
return render_set_field
|
|
942
|
-
|
|
943
543
|
if is_sequence_type(annotation):
|
|
944
544
|
return render_sequence_field
|
|
945
|
-
|
|
946
545
|
origin = get_origin(annotation)
|
|
947
546
|
if origin is not None:
|
|
948
547
|
args = get_args(annotation)
|
|
@@ -1007,7 +606,6 @@ def render_model_readonly[T](
|
|
|
1007
606
|
|
|
1008
607
|
def render_field_readonly(label, value, field_type, description=None, key=None):
|
|
1009
608
|
"""Render a single field in read-only mode."""
|
|
1010
|
-
# Create a container with two columns: label and value
|
|
1011
609
|
cols = st.columns([0.3, 0.7])
|
|
1012
610
|
with cols[0]:
|
|
1013
611
|
st.markdown(f"**{label}:**")
|
|
@@ -1066,9 +664,7 @@ def display_sequence_readonly(value, field_type, key=None):
|
|
|
1066
664
|
if not value: # Empty sequence
|
|
1067
665
|
st.text("No items")
|
|
1068
666
|
return
|
|
1069
|
-
item_type =
|
|
1070
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
1071
|
-
item_type = get_args(field_type)[0]
|
|
667
|
+
item_type = get_inner_type(field_type)
|
|
1072
668
|
for i, item in enumerate(value):
|
|
1073
669
|
with st.expander(f"Item {i + 1}", expanded=False):
|
|
1074
670
|
display_value_readonly(item, item_type, key=f"{key}_{i}" if key else None)
|
|
@@ -1096,27 +692,15 @@ def render_model_field(model_class, field_name, value=None, container=st):
|
|
|
1096
692
|
if field is None:
|
|
1097
693
|
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
1098
694
|
raise ValueError(error_msg)
|
|
1099
|
-
|
|
1100
|
-
# Extract field metadata
|
|
1101
695
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
1102
696
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
1103
697
|
field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
1104
|
-
|
|
1105
698
|
label = field_name.replace("_", " ").title()
|
|
1106
699
|
help_text = get_description(field)
|
|
1107
700
|
if help_text:
|
|
1108
701
|
field_info["help"] = help_text
|
|
1109
|
-
|
|
1110
702
|
renderer = get_field_renderer(field_info)
|
|
1111
|
-
return renderer(
|
|
1112
|
-
key=field_name,
|
|
1113
|
-
value=value,
|
|
1114
|
-
label=label,
|
|
1115
|
-
**field_info,
|
|
1116
|
-
)
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
TForm = TypeVar("TForm", bound="BaseModel")
|
|
703
|
+
return renderer(key=field_name, value=value, label=label, **field_info)
|
|
1120
704
|
|
|
1121
705
|
|
|
1122
706
|
@overload
|
|
@@ -1221,7 +805,6 @@ if __name__ == "__main__":
|
|
|
1221
805
|
long_text: str | None = "test " * 40
|
|
1222
806
|
"""Long text."""
|
|
1223
807
|
|
|
1224
|
-
# Fields that demonstrate the optional toggle
|
|
1225
808
|
optional_int: int | None = None
|
|
1226
809
|
"""An optional integer that can be None."""
|
|
1227
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
|