streambricks 0.2.10__tar.gz → 0.2.12__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.12}/PKG-INFO +1 -1
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/__init__.py +1 -1
- streambricks-0.2.12/src/streambricks/widgets/model_widget/__init__.py +9 -0
- streambricks-0.2.12/src/streambricks/widgets/model_widget/field_metadata_renderers.py +22 -0
- streambricks-0.2.10/src/streambricks/widgets/model_widget.py → streambricks-0.2.12/src/streambricks/widgets/model_widget/main.py +44 -465
- streambricks-0.2.12/src/streambricks/widgets/model_widget/type_renderers.py +299 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/widgets/type_helpers.py +8 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.copier-answers.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.github/dependabot.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.gitignore +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/LICENSE +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/README.md +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/docs/.empty +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/duties.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/mkdocs.yml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/pyproject.toml +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/state.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/tests/__init__.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/tests/conftest.py +0 -0
- {streambricks-0.2.10 → streambricks-0.2.12}/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,20 +3,35 @@
|
|
|
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
|
|
10
|
-
from typing import
|
|
9
|
+
from typing import Any, Literal, TypeVar, get_args, get_origin, overload
|
|
11
10
|
|
|
12
11
|
import fieldz
|
|
13
|
-
from pydantic import SecretStr
|
|
12
|
+
from pydantic import BaseModel, 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,
|
|
@@ -26,354 +41,29 @@ from streambricks.widgets.type_helpers import (
|
|
|
26
41
|
)
|
|
27
42
|
|
|
28
43
|
|
|
29
|
-
if TYPE_CHECKING:
|
|
30
|
-
from pydantic import BaseModel
|
|
31
|
-
|
|
32
44
|
T = TypeVar("T")
|
|
33
45
|
WidgetFunc = Callable[..., T]
|
|
34
|
-
|
|
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
|
-
)
|
|
46
|
+
TForm = TypeVar("TForm", bound=BaseModel)
|
|
275
47
|
|
|
276
48
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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)
|
|
49
|
+
FIELD_METADATA_RENDERERS: dict[str, WidgetFunc[Any]] = {
|
|
50
|
+
"model_identifier": render_model_id_field
|
|
51
|
+
}
|
|
335
52
|
|
|
336
53
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
# No need for radio if only one option
|
|
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
|
-
)
|
|
54
|
+
PRIMITIVE_RENDERERS = {
|
|
55
|
+
str: render_str_field,
|
|
56
|
+
bool: render_bool_field,
|
|
57
|
+
int: render_int_field,
|
|
58
|
+
float: render_float_field,
|
|
59
|
+
Decimal: render_float_field,
|
|
60
|
+
date: render_date_field,
|
|
61
|
+
time: render_time_field,
|
|
62
|
+
datetime: render_datetime_field,
|
|
63
|
+
Enum: render_enum_field,
|
|
64
|
+
Literal: render_literal_field,
|
|
65
|
+
SecretStr: render_secret_str_field,
|
|
66
|
+
}
|
|
377
67
|
|
|
378
68
|
|
|
379
69
|
def render_union_field( # noqa: PLR0911
|
|
@@ -388,13 +78,10 @@ def render_union_field( # noqa: PLR0911
|
|
|
388
78
|
"""Render a field that can accept multiple types."""
|
|
389
79
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
390
80
|
possible_types = get_args(annotation)
|
|
391
|
-
|
|
392
|
-
# Create type selector
|
|
393
81
|
type_key = f"{key}_type"
|
|
394
82
|
type_names = [
|
|
395
83
|
t.__name__ if hasattr(t, "__name__") else str(t) for t in possible_types
|
|
396
84
|
]
|
|
397
|
-
|
|
398
85
|
selected_type_name = st.selectbox(
|
|
399
86
|
f"Type for {label or key}",
|
|
400
87
|
options=type_names,
|
|
@@ -403,19 +90,13 @@ def render_union_field( # noqa: PLR0911
|
|
|
403
90
|
help=help,
|
|
404
91
|
)
|
|
405
92
|
|
|
406
|
-
# Find selected type
|
|
407
93
|
selected_type_index = type_names.index(selected_type_name)
|
|
408
94
|
selected_type = possible_types[selected_type_index]
|
|
409
|
-
|
|
410
|
-
# Create field for selected type
|
|
411
95
|
field_key = f"{key}_value"
|
|
412
96
|
modified_field_info = field_info.copy()
|
|
413
97
|
modified_field_info["type"] = selected_type
|
|
414
|
-
|
|
415
|
-
# Only pass value if it matches the selected type or can be converted
|
|
416
98
|
typed_value: Any = None
|
|
417
99
|
if value is not None:
|
|
418
|
-
# Try to convert the value to the selected type
|
|
419
100
|
try:
|
|
420
101
|
if selected_type is int and isinstance(value, int | float):
|
|
421
102
|
typed_value = int(value)
|
|
@@ -435,13 +116,12 @@ def render_union_field( # noqa: PLR0911
|
|
|
435
116
|
result = renderer(
|
|
436
117
|
key=field_key,
|
|
437
118
|
value=typed_value,
|
|
438
|
-
label=f"Value ({selected_type_name})",
|
|
119
|
+
label=f"Value ({selected_type_name})",
|
|
439
120
|
disabled=disabled,
|
|
440
121
|
help=help,
|
|
441
122
|
**modified_field_info,
|
|
442
123
|
)
|
|
443
124
|
|
|
444
|
-
# Ensure the result is of the correct type
|
|
445
125
|
try:
|
|
446
126
|
if selected_type is int and not isinstance(result, int):
|
|
447
127
|
return int(result)
|
|
@@ -454,7 +134,6 @@ def render_union_field( # noqa: PLR0911
|
|
|
454
134
|
except (ValueError, TypeError) as e:
|
|
455
135
|
error_msg = f"Cannot convert {result} to {selected_type.__name__}: {e!s}"
|
|
456
136
|
st.error(error_msg)
|
|
457
|
-
# Return a default value for the selected type
|
|
458
137
|
if selected_type is int:
|
|
459
138
|
return 0
|
|
460
139
|
if selected_type is float:
|
|
@@ -488,33 +167,18 @@ def render_sequence_field(
|
|
|
488
167
|
**field_info: Any,
|
|
489
168
|
) -> list[Any]:
|
|
490
169
|
"""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
170
|
add_item_key = f"{key}_add_item"
|
|
495
171
|
items_key = f"{key}_items"
|
|
496
|
-
|
|
497
|
-
# Initialize session state for this field
|
|
498
172
|
if items_key not in st.session_state:
|
|
499
173
|
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
|
|
174
|
+
item_type = get_inner_type(field_info)
|
|
508
175
|
st.markdown(f"**{label or key}**")
|
|
509
176
|
if help:
|
|
510
177
|
st.caption(help)
|
|
511
178
|
|
|
512
179
|
with st.container():
|
|
513
|
-
# Add new item button
|
|
514
180
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
515
181
|
add_new_item(st.session_state[items_key], item_type)
|
|
516
|
-
|
|
517
|
-
# Render items
|
|
518
182
|
render_sequence_items(
|
|
519
183
|
st.session_state[items_key],
|
|
520
184
|
item_type,
|
|
@@ -524,7 +188,6 @@ def render_sequence_field(
|
|
|
524
188
|
field_info,
|
|
525
189
|
)
|
|
526
190
|
|
|
527
|
-
# Return the current items
|
|
528
191
|
return st.session_state[items_key]
|
|
529
192
|
|
|
530
193
|
|
|
@@ -539,7 +202,7 @@ def render_sequence_items(
|
|
|
539
202
|
"""Render items in a sequence with delete buttons."""
|
|
540
203
|
item_info = field_info.copy()
|
|
541
204
|
item_info["type"] = item_type
|
|
542
|
-
item_info["inside_expander"] = True
|
|
205
|
+
item_info["inside_expander"] = True
|
|
543
206
|
|
|
544
207
|
try:
|
|
545
208
|
renderer = get_field_renderer(item_info)
|
|
@@ -581,19 +244,9 @@ def render_set_field(
|
|
|
581
244
|
**field_info: Any,
|
|
582
245
|
) -> set[Any]:
|
|
583
246
|
"""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
247
|
if value is None:
|
|
588
248
|
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
|
|
249
|
+
item_type = get_inner_type(field_info)
|
|
597
250
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
598
251
|
item_type
|
|
599
252
|
):
|
|
@@ -606,7 +259,6 @@ def render_set_field(
|
|
|
606
259
|
help=help,
|
|
607
260
|
)
|
|
608
261
|
|
|
609
|
-
# For open-ended sets, use a modified sequence renderer that ensures uniqueness
|
|
610
262
|
return render_open_set_field(
|
|
611
263
|
key=key,
|
|
612
264
|
value=value,
|
|
@@ -628,15 +280,12 @@ def render_set_with_known_domain(
|
|
|
628
280
|
help: str | None = None, # noqa: A002
|
|
629
281
|
) -> set[Any]:
|
|
630
282
|
"""Render a set field using a multiselect when domain is known (enum or literal)."""
|
|
631
|
-
# Get all possible values
|
|
632
283
|
if isinstance(item_type, type) and issubclass(item_type, Enum):
|
|
633
284
|
all_options = list(item_type.__members__.values())
|
|
634
285
|
format_func = lambda x: x.name # noqa: E731
|
|
635
286
|
else: # Literal type
|
|
636
287
|
all_options = list(get_args(item_type))
|
|
637
288
|
format_func = str
|
|
638
|
-
|
|
639
|
-
# Use multi-select for known domain
|
|
640
289
|
selected = st.multiselect(
|
|
641
290
|
label=label or key,
|
|
642
291
|
options=all_options,
|
|
@@ -660,24 +309,17 @@ def render_open_set_field(
|
|
|
660
309
|
**field_info: Any,
|
|
661
310
|
) -> set[Any]:
|
|
662
311
|
"""Render an open-ended set field with uniqueness enforcement."""
|
|
663
|
-
# Create unique state keys for this field
|
|
664
312
|
add_item_key = f"{key}_add_item"
|
|
665
313
|
items_key = f"{key}_items"
|
|
666
|
-
|
|
667
|
-
# Initialize session state for this field
|
|
668
314
|
if items_key not in st.session_state:
|
|
669
315
|
st.session_state[items_key] = list(value)
|
|
670
|
-
|
|
671
|
-
# Create container for set elements
|
|
672
316
|
st.markdown(f"**{label or key}**")
|
|
673
317
|
if help:
|
|
674
318
|
st.caption(help)
|
|
675
|
-
|
|
676
319
|
with st.container():
|
|
677
320
|
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
678
321
|
temp_list: list[Any] = []
|
|
679
322
|
add_new_item(temp_list, item_type)
|
|
680
|
-
|
|
681
323
|
# If the temp_list has an item and it's not a duplicate, add it to our items
|
|
682
324
|
if temp_list and str(temp_list[0]) not in {
|
|
683
325
|
str(i) for i in st.session_state[items_key]
|
|
@@ -710,17 +352,13 @@ def render_set_items(
|
|
|
710
352
|
"""Render items in a set with delete buttons and uniqueness enforcement."""
|
|
711
353
|
item_info = field_info.copy()
|
|
712
354
|
item_info["type"] = item_type
|
|
713
|
-
item_info["inside_expander"] = True
|
|
714
|
-
|
|
355
|
+
item_info["inside_expander"] = True
|
|
715
356
|
try:
|
|
716
357
|
renderer = get_field_renderer(item_info)
|
|
717
358
|
items_to_delete = []
|
|
718
|
-
|
|
719
359
|
for i, item in enumerate(items):
|
|
720
360
|
st.divider()
|
|
721
361
|
st.markdown(f"**Item {i + 1}**")
|
|
722
|
-
|
|
723
|
-
# Render the item
|
|
724
362
|
updated_item = renderer(
|
|
725
363
|
key=f"{key}_item_{i}",
|
|
726
364
|
value=item,
|
|
@@ -728,28 +366,23 @@ def render_set_items(
|
|
|
728
366
|
disabled=disabled,
|
|
729
367
|
**item_info,
|
|
730
368
|
)
|
|
731
|
-
|
|
732
|
-
# Check if this update would create a duplicate
|
|
733
369
|
duplicate = False
|
|
734
370
|
for j, other_item in enumerate(items):
|
|
735
371
|
if i != j and str(updated_item) == str(other_item):
|
|
736
372
|
duplicate = True
|
|
737
373
|
break
|
|
738
|
-
|
|
739
374
|
if duplicate:
|
|
740
375
|
st.error("This value would create a duplicate in the set.")
|
|
741
376
|
else:
|
|
742
377
|
items[i] = updated_item
|
|
743
|
-
|
|
744
378
|
delete_key = f"{key}_delete_{i}"
|
|
745
379
|
if st.button("Delete Item", key=delete_key, disabled=disabled):
|
|
746
380
|
items_to_delete.append(i)
|
|
747
|
-
|
|
748
381
|
if items_to_delete:
|
|
749
382
|
for idx in sorted(items_to_delete, reverse=True):
|
|
750
383
|
if 0 <= idx < len(items):
|
|
751
384
|
items.pop(idx)
|
|
752
|
-
st.rerun()
|
|
385
|
+
st.rerun()
|
|
753
386
|
|
|
754
387
|
except Exception as e: # noqa: BLE001
|
|
755
388
|
st.error(f"Error rendering set items: {e!s}")
|
|
@@ -761,10 +394,7 @@ def display_set_readonly(value, field_type, key=None):
|
|
|
761
394
|
st.text("No items")
|
|
762
395
|
return
|
|
763
396
|
|
|
764
|
-
item_type =
|
|
765
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
766
|
-
item_type = get_args(field_type)[0]
|
|
767
|
-
|
|
397
|
+
item_type = get_inner_type(field_type)
|
|
768
398
|
# For known domain sets (enum or literal), show as comma-separated list
|
|
769
399
|
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
770
400
|
item_type
|
|
@@ -800,13 +430,8 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
800
430
|
help: str | None = None, # noqa: A002
|
|
801
431
|
**field_info: Any,
|
|
802
432
|
) -> Any:
|
|
803
|
-
# Create a layout for the field
|
|
804
433
|
enable_key = f"{key}_enable"
|
|
805
|
-
|
|
806
|
-
# Create columns for checkbox and content
|
|
807
434
|
cols = st.columns([0.1, 0.9])
|
|
808
|
-
|
|
809
|
-
# Toggle checkbox in first column
|
|
810
435
|
with cols[0]:
|
|
811
436
|
is_enabled = st.checkbox(
|
|
812
437
|
"Enable",
|
|
@@ -815,15 +440,11 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
815
440
|
disabled=disabled,
|
|
816
441
|
label_visibility="collapsed",
|
|
817
442
|
)
|
|
818
|
-
|
|
819
|
-
# Content in second column
|
|
820
443
|
with cols[1]:
|
|
821
444
|
# Show field label
|
|
822
445
|
st.markdown(f"**{label or key}**")
|
|
823
446
|
if help:
|
|
824
447
|
st.caption(help)
|
|
825
|
-
|
|
826
|
-
# If enabled, render the field; otherwise show "None"
|
|
827
448
|
if is_enabled:
|
|
828
449
|
return renderer(
|
|
829
450
|
key=key,
|
|
@@ -864,12 +485,10 @@ def render_model_instance_field(
|
|
|
864
485
|
st.error(error_msg)
|
|
865
486
|
return None
|
|
866
487
|
|
|
867
|
-
# Show a header for the nested model with help text if available
|
|
868
488
|
st.markdown(f"**{label or key}**")
|
|
869
489
|
if help:
|
|
870
490
|
st.caption(help)
|
|
871
491
|
|
|
872
|
-
# Use an expander for the nested model fields
|
|
873
492
|
with st.expander("Edit", expanded=True):
|
|
874
493
|
updated_value = {}
|
|
875
494
|
try:
|
|
@@ -880,10 +499,8 @@ def render_model_instance_field(
|
|
|
880
499
|
nested_field_info = {"name": field_name, "type": field.type}
|
|
881
500
|
if field_help:
|
|
882
501
|
nested_field_info["help"] = field_help
|
|
883
|
-
|
|
884
502
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
885
503
|
nested_field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
886
|
-
|
|
887
504
|
renderer = get_field_renderer(nested_field_info)
|
|
888
505
|
updated_value[field_name] = renderer(
|
|
889
506
|
key=f"{key}_{field_name}",
|
|
@@ -899,26 +516,9 @@ def render_model_instance_field(
|
|
|
899
516
|
return value
|
|
900
517
|
|
|
901
518
|
|
|
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
519
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
919
520
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
920
521
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
921
|
-
# Check for custom field type specification in json_schema_extra
|
|
922
522
|
json_schema_extra = field_info.get("json_schema_extra", {})
|
|
923
523
|
if (
|
|
924
524
|
json_schema_extra
|
|
@@ -930,19 +530,14 @@ def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa:
|
|
|
930
530
|
isinstance(annotation, type) and issubclass(annotation, SecretStr)
|
|
931
531
|
):
|
|
932
532
|
return render_secret_str_field
|
|
933
|
-
|
|
934
533
|
if is_literal_type(annotation):
|
|
935
534
|
return render_literal_field
|
|
936
|
-
|
|
937
535
|
if is_union_type(annotation):
|
|
938
536
|
return render_union_field
|
|
939
|
-
|
|
940
537
|
if is_set_type(annotation):
|
|
941
538
|
return render_set_field
|
|
942
|
-
|
|
943
539
|
if is_sequence_type(annotation):
|
|
944
540
|
return render_sequence_field
|
|
945
|
-
|
|
946
541
|
origin = get_origin(annotation)
|
|
947
542
|
if origin is not None:
|
|
948
543
|
args = get_args(annotation)
|
|
@@ -1007,7 +602,6 @@ def render_model_readonly[T](
|
|
|
1007
602
|
|
|
1008
603
|
def render_field_readonly(label, value, field_type, description=None, key=None):
|
|
1009
604
|
"""Render a single field in read-only mode."""
|
|
1010
|
-
# Create a container with two columns: label and value
|
|
1011
605
|
cols = st.columns([0.3, 0.7])
|
|
1012
606
|
with cols[0]:
|
|
1013
607
|
st.markdown(f"**{label}:**")
|
|
@@ -1066,9 +660,7 @@ def display_sequence_readonly(value, field_type, key=None):
|
|
|
1066
660
|
if not value: # Empty sequence
|
|
1067
661
|
st.text("No items")
|
|
1068
662
|
return
|
|
1069
|
-
item_type =
|
|
1070
|
-
with contextlib.suppress(IndexError, TypeError):
|
|
1071
|
-
item_type = get_args(field_type)[0]
|
|
663
|
+
item_type = get_inner_type(field_type)
|
|
1072
664
|
for i, item in enumerate(value):
|
|
1073
665
|
with st.expander(f"Item {i + 1}", expanded=False):
|
|
1074
666
|
display_value_readonly(item, item_type, key=f"{key}_{i}" if key else None)
|
|
@@ -1096,27 +688,15 @@ def render_model_field(model_class, field_name, value=None, container=st):
|
|
|
1096
688
|
if field is None:
|
|
1097
689
|
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
1098
690
|
raise ValueError(error_msg)
|
|
1099
|
-
|
|
1100
|
-
# Extract field metadata
|
|
1101
691
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
1102
692
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
1103
693
|
field_info.update(field.native_field.json_schema_extra or {}) # type: ignore
|
|
1104
|
-
|
|
1105
694
|
label = field_name.replace("_", " ").title()
|
|
1106
695
|
help_text = get_description(field)
|
|
1107
696
|
if help_text:
|
|
1108
697
|
field_info["help"] = help_text
|
|
1109
|
-
|
|
1110
698
|
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")
|
|
699
|
+
return renderer(key=field_name, value=value, label=label, **field_info)
|
|
1120
700
|
|
|
1121
701
|
|
|
1122
702
|
@overload
|
|
@@ -1221,7 +801,6 @@ if __name__ == "__main__":
|
|
|
1221
801
|
long_text: str | None = "test " * 40
|
|
1222
802
|
"""Long text."""
|
|
1223
803
|
|
|
1224
|
-
# Fields that demonstrate the optional toggle
|
|
1225
804
|
optional_int: int | None = None
|
|
1226
805
|
"""An optional integer that can be None."""
|
|
1227
806
|
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
|