streambricks 0.2.5__tar.gz → 0.2.7__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.5 → streambricks-0.2.7}/PKG-INFO +1 -1
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/__init__.py +1 -1
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/state.py +23 -1
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/widgets/model_widget.py +78 -12
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/widgets/multi_select.py +0 -7
- {streambricks-0.2.5 → streambricks-0.2.7}/.copier-answers.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.github/dependabot.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.gitignore +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/LICENSE +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/README.md +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/docs/.empty +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/duties.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/mkdocs.yml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/pyproject.toml +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/src/streambricks/widgets/type_helpers.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/tests/__init__.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/tests/conftest.py +0 -0
- {streambricks-0.2.5 → streambricks-0.2.7}/tests/test_widgets.py +0 -0
|
@@ -32,7 +32,7 @@ class State(BaseModel):
|
|
|
32
32
|
return st.session_state[state_key]
|
|
33
33
|
|
|
34
34
|
@classmethod
|
|
35
|
-
def
|
|
35
|
+
def set_state(cls, *, key: str | None = None, value: Self):
|
|
36
36
|
"""Set state to given model.
|
|
37
37
|
|
|
38
38
|
Args:
|
|
@@ -51,3 +51,25 @@ class State(BaseModel):
|
|
|
51
51
|
"""
|
|
52
52
|
state_key = key or cls.__name__
|
|
53
53
|
st.session_state[state_key] = cls()
|
|
54
|
+
|
|
55
|
+
def form(self, read_only: bool = False, exclude: set[str] | None = None) -> Self:
|
|
56
|
+
"""Display (editable) state as a model form.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
read_only: Whether to display the form in read-only mode.
|
|
60
|
+
exclude: Optional set of field names to exclude from the form.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
The updated state instance.
|
|
64
|
+
"""
|
|
65
|
+
from streambricks.widgets import model_widget
|
|
66
|
+
|
|
67
|
+
return model_widget.render_model_form(self, readonly=read_only, exclude=exclude)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__":
|
|
71
|
+
|
|
72
|
+
class TestState(State):
|
|
73
|
+
test: int = 1
|
|
74
|
+
|
|
75
|
+
state = TestState.get()
|
|
@@ -10,6 +10,7 @@ from enum import Enum
|
|
|
10
10
|
from typing import TYPE_CHECKING, Any, Literal, TypeVar, get_args, get_origin, overload
|
|
11
11
|
|
|
12
12
|
import fieldz
|
|
13
|
+
from pydantic import SecretStr
|
|
13
14
|
import streamlit as st
|
|
14
15
|
|
|
15
16
|
from streambricks.widgets.type_helpers import (
|
|
@@ -238,6 +239,66 @@ def render_enum_field(
|
|
|
238
239
|
)
|
|
239
240
|
|
|
240
241
|
|
|
242
|
+
def render_secret_str_field(
|
|
243
|
+
*,
|
|
244
|
+
key: str,
|
|
245
|
+
value: str | None = None,
|
|
246
|
+
label: str | None = None,
|
|
247
|
+
disabled: bool = False,
|
|
248
|
+
help: str | None = None, # noqa: A002
|
|
249
|
+
**field_info: Any,
|
|
250
|
+
) -> SecretStr:
|
|
251
|
+
"""Render a SecretStr field using a password input."""
|
|
252
|
+
text = st.text_input(
|
|
253
|
+
label=label or key,
|
|
254
|
+
value=value or "",
|
|
255
|
+
disabled=disabled,
|
|
256
|
+
key=key,
|
|
257
|
+
help=help,
|
|
258
|
+
type="password",
|
|
259
|
+
)
|
|
260
|
+
return SecretStr(text)
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def render_datetime_field(
|
|
264
|
+
*,
|
|
265
|
+
key: str,
|
|
266
|
+
value: datetime | None = None,
|
|
267
|
+
label: str | None = None,
|
|
268
|
+
disabled: bool = False,
|
|
269
|
+
help: str | None = None, # noqa: A002
|
|
270
|
+
**field_info: Any,
|
|
271
|
+
) -> datetime:
|
|
272
|
+
"""Render a datetime field using date and time inputs."""
|
|
273
|
+
# Split into date and time components
|
|
274
|
+
current_date = value.date() if value else date.today()
|
|
275
|
+
current_time = value.time() if value else datetime.now().time()
|
|
276
|
+
|
|
277
|
+
# Create columns for date and time
|
|
278
|
+
date_col, time_col = st.columns(2)
|
|
279
|
+
|
|
280
|
+
with date_col:
|
|
281
|
+
selected_date = st.date_input(
|
|
282
|
+
label=f"{label or key} (Date)",
|
|
283
|
+
value=current_date,
|
|
284
|
+
disabled=disabled,
|
|
285
|
+
key=f"{key}_date",
|
|
286
|
+
help=help,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
with time_col:
|
|
290
|
+
selected_time = st.time_input(
|
|
291
|
+
label=f"{label or key} (Time)",
|
|
292
|
+
value=current_time,
|
|
293
|
+
disabled=disabled,
|
|
294
|
+
key=f"{key}_time",
|
|
295
|
+
help=help,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
# Combine date and time
|
|
299
|
+
return datetime.combine(selected_date, selected_time)
|
|
300
|
+
|
|
301
|
+
|
|
241
302
|
def render_literal_field(
|
|
242
303
|
*,
|
|
243
304
|
key: str,
|
|
@@ -498,7 +559,7 @@ def render_set_field(
|
|
|
498
559
|
item_type = Any
|
|
499
560
|
|
|
500
561
|
# Check if item_type is an enum or literal - if so, we know all possible values
|
|
501
|
-
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type(
|
|
562
|
+
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
502
563
|
item_type
|
|
503
564
|
):
|
|
504
565
|
return render_set_with_known_domain(
|
|
@@ -661,7 +722,7 @@ def render_set_items(
|
|
|
661
722
|
|
|
662
723
|
def display_set_readonly(value, field_type, key=None):
|
|
663
724
|
"""Display a set in read-only mode."""
|
|
664
|
-
if not value:
|
|
725
|
+
if not value:
|
|
665
726
|
st.text("No items")
|
|
666
727
|
return
|
|
667
728
|
|
|
@@ -670,10 +731,10 @@ def display_set_readonly(value, field_type, key=None):
|
|
|
670
731
|
item_type = get_args(field_type)[0]
|
|
671
732
|
|
|
672
733
|
# For known domain sets (enum or literal), show as comma-separated list
|
|
673
|
-
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type(
|
|
734
|
+
if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
|
|
674
735
|
item_type
|
|
675
736
|
):
|
|
676
|
-
if isinstance(item_type, type) and issubclass(item_type, Enum):
|
|
737
|
+
if isinstance(item_type, type) and issubclass(item_type, Enum): # pyright: ignore
|
|
677
738
|
display_text = ", ".join(item.name for item in value)
|
|
678
739
|
else:
|
|
679
740
|
display_text = ", ".join(str(item) for item in value)
|
|
@@ -718,14 +779,10 @@ def render_model_instance_field(
|
|
|
718
779
|
|
|
719
780
|
# Use an expander for the nested model fields
|
|
720
781
|
with st.expander("Edit", expanded=True):
|
|
721
|
-
# Render each field of the nested model
|
|
722
782
|
updated_value = {}
|
|
723
|
-
|
|
724
783
|
try:
|
|
725
784
|
for field in fieldz.fields(model_class):
|
|
726
785
|
field_name = field.name
|
|
727
|
-
|
|
728
|
-
# Get field value and handle 'MISSING' with type-appropriate defaults
|
|
729
786
|
field_value = get_with_default(value, field_name, field)
|
|
730
787
|
field_help = get_description(field)
|
|
731
788
|
nested_field_info = {"name": field_name, "type": field.type}
|
|
@@ -753,20 +810,26 @@ def render_model_instance_field(
|
|
|
753
810
|
# Mapping of Python types to render functions
|
|
754
811
|
PRIMITIVE_RENDERERS = {
|
|
755
812
|
str: render_str_field,
|
|
813
|
+
bool: render_bool_field,
|
|
756
814
|
int: render_int_field,
|
|
757
815
|
float: render_float_field,
|
|
758
816
|
Decimal: render_float_field,
|
|
759
|
-
bool: render_bool_field,
|
|
760
817
|
date: render_date_field,
|
|
761
818
|
time: render_time_field,
|
|
819
|
+
datetime: render_datetime_field,
|
|
762
820
|
Enum: render_enum_field,
|
|
763
821
|
Literal: render_literal_field,
|
|
822
|
+
SecretStr: render_secret_str_field,
|
|
764
823
|
}
|
|
765
824
|
|
|
766
825
|
|
|
767
826
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
768
827
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
769
828
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
829
|
+
if annotation is SecretStr or (
|
|
830
|
+
isinstance(annotation, type) and issubclass(annotation, SecretStr)
|
|
831
|
+
):
|
|
832
|
+
return render_secret_str_field
|
|
770
833
|
|
|
771
834
|
if is_literal_type(annotation):
|
|
772
835
|
return render_literal_field
|
|
@@ -883,6 +946,8 @@ def display_value_readonly(value, field_type, key=None):
|
|
|
883
946
|
st.text(str(enum_value.name))
|
|
884
947
|
case bool() as bool_value:
|
|
885
948
|
st.checkbox("", value=bool_value, disabled=True, key=key)
|
|
949
|
+
case datetime():
|
|
950
|
+
st.text(value.strftime("%Y-%m-%d %H:%M:%S"))
|
|
886
951
|
case int() | float() | Decimal() | date() | time() | datetime():
|
|
887
952
|
st.text(str(value))
|
|
888
953
|
case str() as str_value:
|
|
@@ -890,8 +955,9 @@ def display_value_readonly(value, field_type, key=None):
|
|
|
890
955
|
st.text_area("", value=str_value, disabled=True, height=100, key=key)
|
|
891
956
|
else:
|
|
892
957
|
st.text(str_value)
|
|
958
|
+
case SecretStr():
|
|
959
|
+
st.text("********")
|
|
893
960
|
case _:
|
|
894
|
-
# Default fallback for other types
|
|
895
961
|
st.text(str(value))
|
|
896
962
|
|
|
897
963
|
|
|
@@ -1051,7 +1117,7 @@ if __name__ == "__main__":
|
|
|
1051
1117
|
status: int | str | bool = 2
|
|
1052
1118
|
"""A field that can be either int, str, or bool."""
|
|
1053
1119
|
|
|
1054
|
-
optional_text:
|
|
1120
|
+
optional_text: bool = True
|
|
1055
1121
|
"""Optional text field."""
|
|
1056
1122
|
|
|
1057
1123
|
tags: list[str] = Field(default_factory=list)
|
|
@@ -1069,7 +1135,7 @@ if __name__ == "__main__":
|
|
|
1069
1135
|
def demo():
|
|
1070
1136
|
st.title("Pydantic Form Demo")
|
|
1071
1137
|
if "model" not in st.session_state:
|
|
1072
|
-
st.session_state.model = TestModel(status=2
|
|
1138
|
+
st.session_state.model = TestModel(status=2)
|
|
1073
1139
|
st.session_state.model = render_model_form(TestModel)
|
|
1074
1140
|
with st.expander("Current Model State", expanded=True):
|
|
1075
1141
|
st.json(st.session_state.model.model_dump_json(indent=2))
|
|
@@ -38,12 +38,8 @@ def multiselect[T](
|
|
|
38
38
|
Returns:
|
|
39
39
|
List of selected MultiSelectItem objects
|
|
40
40
|
"""
|
|
41
|
-
# Create full state key
|
|
42
41
|
full_key = f"multiselect_{state_key}"
|
|
43
|
-
|
|
44
|
-
# Map for looking up items by label
|
|
45
42
|
item_map = {str(item): item for item in items}
|
|
46
|
-
|
|
47
43
|
# Initialize state if needed
|
|
48
44
|
if full_key not in st.session_state:
|
|
49
45
|
default_selection = list(item_map.keys()) if default_all else []
|
|
@@ -64,10 +60,7 @@ def multiselect[T](
|
|
|
64
60
|
key=f"{full_key}_widget",
|
|
65
61
|
)
|
|
66
62
|
|
|
67
|
-
# Update session state
|
|
68
63
|
st.session_state[full_key] = selected_labels
|
|
69
|
-
|
|
70
|
-
# Return the actual selected items
|
|
71
64
|
return [item_map[label] for label in selected_labels]
|
|
72
65
|
|
|
73
66
|
|
|
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
|