streambricks 0.2.4__tar.gz → 0.2.6__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.
Files changed (29) hide show
  1. {streambricks-0.2.4 → streambricks-0.2.6}/PKG-INFO +1 -1
  2. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/__init__.py +1 -1
  3. streambricks-0.2.6/src/streambricks/state.py +75 -0
  4. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/widgets/model_widget.py +34 -10
  5. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/widgets/multi_select.py +0 -7
  6. streambricks-0.2.4/src/streambricks/state.py +0 -32
  7. {streambricks-0.2.4 → streambricks-0.2.6}/.copier-answers.yml +0 -0
  8. {streambricks-0.2.4 → streambricks-0.2.6}/.github/FUNDING.yml +0 -0
  9. {streambricks-0.2.4 → streambricks-0.2.6}/.github/copilot-instructions.md +0 -0
  10. {streambricks-0.2.4 → streambricks-0.2.6}/.github/dependabot.yml +0 -0
  11. {streambricks-0.2.4 → streambricks-0.2.6}/.github/workflows/build.yml +0 -0
  12. {streambricks-0.2.4 → streambricks-0.2.6}/.github/workflows/documentation.yml +0 -0
  13. {streambricks-0.2.4 → streambricks-0.2.6}/.gitignore +0 -0
  14. {streambricks-0.2.4 → streambricks-0.2.6}/.pre-commit-config.yaml +0 -0
  15. {streambricks-0.2.4 → streambricks-0.2.6}/LICENSE +0 -0
  16. {streambricks-0.2.4 → streambricks-0.2.6}/README.md +0 -0
  17. {streambricks-0.2.4 → streambricks-0.2.6}/docs/.empty +0 -0
  18. {streambricks-0.2.4 → streambricks-0.2.6}/duties.py +0 -0
  19. {streambricks-0.2.4 → streambricks-0.2.6}/mkdocs.yml +0 -0
  20. {streambricks-0.2.4 → streambricks-0.2.6}/overrides/_dummy.txt +0 -0
  21. {streambricks-0.2.4 → streambricks-0.2.6}/pyproject.toml +0 -0
  22. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/helpers.py +0 -0
  23. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/py.typed +0 -0
  24. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/widgets/__init__.py +0 -0
  25. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/widgets/model_selector.py +0 -0
  26. {streambricks-0.2.4 → streambricks-0.2.6}/src/streambricks/widgets/type_helpers.py +0 -0
  27. {streambricks-0.2.4 → streambricks-0.2.6}/tests/__init__.py +0 -0
  28. {streambricks-0.2.4 → streambricks-0.2.6}/tests/conftest.py +0 -0
  29. {streambricks-0.2.4 → streambricks-0.2.6}/tests/test_widgets.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streambricks
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: Streamlit widgets and helpers
5
5
  Project-URL: Documentation, https://phil65.github.io/streambricks/
6
6
  Project-URL: Source, https://github.com/phil65/streambricks
@@ -1,4 +1,4 @@
1
- __version__ = "0.2.4"
1
+ __version__ = "0.2.6"
2
2
 
3
3
 
4
4
  from streambricks.widgets.model_widget import (
@@ -0,0 +1,75 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Self, TypeVar
4
+
5
+ from pydantic import BaseModel
6
+ import streamlit as st
7
+
8
+
9
+ T = TypeVar("T", bound=BaseModel)
10
+
11
+
12
+ class State(BaseModel):
13
+ @classmethod
14
+ def get(cls, *, key: str | None = None) -> Self:
15
+ """Get or initialize typed state for Streamlit.
16
+
17
+ Args:
18
+ model_class: Pydantic model class defining the state structure
19
+ key: Optional key to use in session_state (defaults to model class name)
20
+
21
+ Returns:
22
+ An instance of the model representing current state
23
+ """
24
+ state_key = key or cls.__name__
25
+ if state_key not in st.session_state:
26
+ st.session_state[state_key] = cls()
27
+
28
+ current_state = st.session_state[state_key]
29
+ if not isinstance(current_state, cls):
30
+ st.session_state[state_key] = cls()
31
+
32
+ return st.session_state[state_key]
33
+
34
+ @classmethod
35
+ def set_state(cls, *, key: str | None = None, value: Self):
36
+ """Set state to given model.
37
+
38
+ Args:
39
+ key: Optional key to use in session_state (defaults to model class name)
40
+ value: The value to set in session_state
41
+ """
42
+ state_key = key or cls.__name__
43
+ st.session_state[state_key] = value
44
+
45
+ @classmethod
46
+ def reset(cls, *, key: str | None = None):
47
+ """Reset state.
48
+
49
+ Args:
50
+ key: Optional key to use in session_state (defaults to model class name)
51
+ """
52
+ state_key = key or cls.__name__
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,27 @@ 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
+
241
263
  def render_literal_field(
242
264
  *,
243
265
  key: str,
@@ -498,7 +520,7 @@ def render_set_field(
498
520
  item_type = Any
499
521
 
500
522
  # 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(
523
+ if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
502
524
  item_type
503
525
  ):
504
526
  return render_set_with_known_domain(
@@ -661,7 +683,7 @@ def render_set_items(
661
683
 
662
684
  def display_set_readonly(value, field_type, key=None):
663
685
  """Display a set in read-only mode."""
664
- if not value: # Empty set
686
+ if not value:
665
687
  st.text("No items")
666
688
  return
667
689
 
@@ -670,10 +692,10 @@ def display_set_readonly(value, field_type, key=None):
670
692
  item_type = get_args(field_type)[0]
671
693
 
672
694
  # 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(
695
+ if (isinstance(item_type, type) and issubclass(item_type, Enum)) or is_literal_type( # pyright: ignore
674
696
  item_type
675
697
  ):
676
- if isinstance(item_type, type) and issubclass(item_type, Enum):
698
+ if isinstance(item_type, type) and issubclass(item_type, Enum): # pyright: ignore
677
699
  display_text = ", ".join(item.name for item in value)
678
700
  else:
679
701
  display_text = ", ".join(str(item) for item in value)
@@ -718,14 +740,10 @@ def render_model_instance_field(
718
740
 
719
741
  # Use an expander for the nested model fields
720
742
  with st.expander("Edit", expanded=True):
721
- # Render each field of the nested model
722
743
  updated_value = {}
723
-
724
744
  try:
725
745
  for field in fieldz.fields(model_class):
726
746
  field_name = field.name
727
-
728
- # Get field value and handle 'MISSING' with type-appropriate defaults
729
747
  field_value = get_with_default(value, field_name, field)
730
748
  field_help = get_description(field)
731
749
  nested_field_info = {"name": field_name, "type": field.type}
@@ -761,12 +779,17 @@ PRIMITIVE_RENDERERS = {
761
779
  time: render_time_field,
762
780
  Enum: render_enum_field,
763
781
  Literal: render_literal_field,
782
+ SecretStr: render_secret_str_field,
764
783
  }
765
784
 
766
785
 
767
786
  def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
768
787
  """Get the appropriate renderer for a field based on its type and constraints."""
769
788
  annotation = field_info.get("type") or field_info.get("annotation")
789
+ if annotation is SecretStr or (
790
+ isinstance(annotation, type) and issubclass(annotation, SecretStr)
791
+ ):
792
+ return render_secret_str_field
770
793
 
771
794
  if is_literal_type(annotation):
772
795
  return render_literal_field
@@ -890,8 +913,9 @@ def display_value_readonly(value, field_type, key=None):
890
913
  st.text_area("", value=str_value, disabled=True, height=100, key=key)
891
914
  else:
892
915
  st.text(str_value)
916
+ case SecretStr():
917
+ st.text("********")
893
918
  case _:
894
- # Default fallback for other types
895
919
  st.text(str(value))
896
920
 
897
921
 
@@ -1051,7 +1075,7 @@ if __name__ == "__main__":
1051
1075
  status: int | str | bool = 2
1052
1076
  """A field that can be either int, str, or bool."""
1053
1077
 
1054
- optional_text: str | None = None
1078
+ optional_text: SecretStr | None = None
1055
1079
  """Optional text field."""
1056
1080
 
1057
1081
  tags: list[str] = Field(default_factory=list)
@@ -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
 
@@ -1,32 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from typing import Self, TypeVar
4
-
5
- from pydantic import BaseModel
6
- import streamlit as st
7
-
8
-
9
- T = TypeVar("T", bound=BaseModel)
10
-
11
-
12
- class State(BaseModel):
13
- @classmethod
14
- def get(cls, *, key: str | None = None) -> Self:
15
- """Get or initialize typed state for Streamlit.
16
-
17
- Args:
18
- model_class: Pydantic model class defining the state structure
19
- key: Optional key to use in session_state (defaults to model class name)
20
-
21
- Returns:
22
- An instance of the model representing current state
23
- """
24
- state_key = key or cls.__name__
25
- if state_key not in st.session_state:
26
- st.session_state[state_key] = cls()
27
-
28
- current_state = st.session_state[state_key]
29
- if not isinstance(current_state, cls):
30
- st.session_state[state_key] = cls()
31
-
32
- return st.session_state[state_key]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes