streambricks 0.2.14__tar.gz → 0.2.15__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 (32) hide show
  1. {streambricks-0.2.14 → streambricks-0.2.15}/PKG-INFO +1 -1
  2. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/__init__.py +1 -1
  3. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/model_widget/main.py +125 -31
  4. {streambricks-0.2.14 → streambricks-0.2.15}/.copier-answers.yml +0 -0
  5. {streambricks-0.2.14 → streambricks-0.2.15}/.github/FUNDING.yml +0 -0
  6. {streambricks-0.2.14 → streambricks-0.2.15}/.github/copilot-instructions.md +0 -0
  7. {streambricks-0.2.14 → streambricks-0.2.15}/.github/dependabot.yml +0 -0
  8. {streambricks-0.2.14 → streambricks-0.2.15}/.github/workflows/build.yml +0 -0
  9. {streambricks-0.2.14 → streambricks-0.2.15}/.github/workflows/documentation.yml +0 -0
  10. {streambricks-0.2.14 → streambricks-0.2.15}/.gitignore +0 -0
  11. {streambricks-0.2.14 → streambricks-0.2.15}/.pre-commit-config.yaml +0 -0
  12. {streambricks-0.2.14 → streambricks-0.2.15}/LICENSE +0 -0
  13. {streambricks-0.2.14 → streambricks-0.2.15}/README.md +0 -0
  14. {streambricks-0.2.14 → streambricks-0.2.15}/docs/.empty +0 -0
  15. {streambricks-0.2.14 → streambricks-0.2.15}/duties.py +0 -0
  16. {streambricks-0.2.14 → streambricks-0.2.15}/mkdocs.yml +0 -0
  17. {streambricks-0.2.14 → streambricks-0.2.15}/overrides/_dummy.txt +0 -0
  18. {streambricks-0.2.14 → streambricks-0.2.15}/pyproject.toml +0 -0
  19. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/helpers.py +0 -0
  20. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/py.typed +0 -0
  21. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/state.py +0 -0
  22. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/__init__.py +0 -0
  23. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/bind_kwargs.py +0 -0
  24. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/model_selector.py +0 -0
  25. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/model_widget/__init__.py +0 -0
  26. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
  27. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
  28. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/multi_select.py +0 -0
  29. {streambricks-0.2.14 → streambricks-0.2.15}/src/streambricks/widgets/type_helpers.py +0 -0
  30. {streambricks-0.2.14 → streambricks-0.2.15}/tests/__init__.py +0 -0
  31. {streambricks-0.2.14 → streambricks-0.2.15}/tests/conftest.py +0 -0
  32. {streambricks-0.2.14 → streambricks-0.2.15}/tests/test_widgets.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streambricks
3
- Version: 0.2.14
3
+ Version: 0.2.15
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.14"
1
+ __version__ = "0.2.15"
2
2
 
3
3
 
4
4
  from streambricks.widgets.model_widget import (
@@ -69,7 +69,7 @@ PRIMITIVE_RENDERERS = {
69
69
  }
70
70
 
71
71
 
72
- def render_union_field( # noqa: PLR0911
72
+ def render_union_field(
73
73
  *,
74
74
  key: str,
75
75
  value: Any = None,
@@ -83,6 +83,94 @@ def render_union_field( # noqa: PLR0911
83
83
 
84
84
  type_options = get_union_type_options(annotation) # list of (type, description)
85
85
 
86
+ # Check if None is one of the options
87
+ none_type_index = None
88
+ for i, (t, _) in enumerate(type_options):
89
+ if t is type(None):
90
+ none_type_index = i
91
+ break
92
+
93
+ # Handle optional fields (union with None)
94
+ if none_type_index is not None:
95
+ # Remove None from type options
96
+ type_options.pop(none_type_index)
97
+
98
+ # Add checkbox to toggle between None and non-None
99
+ enable_key = f"{key}_not_none"
100
+
101
+ # Initialize the session state for this checkbox if it doesn't exist
102
+ if enable_key not in st.session_state:
103
+ st.session_state[enable_key] = value is not None
104
+
105
+ # Render checkbox using session state
106
+ is_enabled = st.checkbox(
107
+ f"Enable {label or key}",
108
+ key=enable_key,
109
+ disabled=disabled,
110
+ help=help,
111
+ )
112
+
113
+ if not is_enabled:
114
+ # If checkbox is unchecked, return None
115
+ st.text("None")
116
+ return None
117
+
118
+ # Continue with the regular union handling for non-None types
119
+ # If there's only one non-None type left, we can skip the type selector
120
+ if len(type_options) == 1:
121
+ selected_type, selected_type_name = type_options[0]
122
+ field_key = f"{key}_value"
123
+
124
+ # Special case for literal types
125
+ if is_literal_type(selected_type):
126
+ literal_values = get_args(selected_type)
127
+ value_index = 0
128
+ if value in literal_values:
129
+ value_index = literal_values.index(value)
130
+
131
+ return st.selectbox(
132
+ label or key,
133
+ options=literal_values,
134
+ index=value_index,
135
+ key=field_key,
136
+ disabled=disabled,
137
+ help=None, # Help already shown with checkbox
138
+ )
139
+
140
+ # For regular type, use standard field renderer
141
+ modified_field_info = field_info.copy()
142
+ modified_field_info["type"] = selected_type
143
+
144
+ typed_val: Any = None
145
+ if value is not None:
146
+ try:
147
+ if selected_type is int and isinstance(value, int | float):
148
+ typed_val = int(value)
149
+ elif selected_type is float and isinstance(value, int | float):
150
+ typed_val = float(value)
151
+ elif selected_type is str:
152
+ typed_val = str(value)
153
+ elif selected_type is bool:
154
+ typed_val = bool(value)
155
+ elif isinstance(value, selected_type):
156
+ typed_val = value
157
+ except (ValueError, TypeError):
158
+ # If conversion fails, start with a blank/default value
159
+ pass
160
+
161
+ renderer = get_field_renderer(modified_field_info)
162
+ result = renderer(
163
+ key=field_key,
164
+ value=typed_val,
165
+ label=label or key,
166
+ disabled=disabled,
167
+ help=None, # Help already shown with checkbox
168
+ **modified_field_info,
169
+ )
170
+
171
+ return convert_result_type(result, selected_type)
172
+
173
+ # Original code for handling standard unions (no None or None already handled)
86
174
  default_index = 0
87
175
  if value is not None:
88
176
  for i, (t, _) in enumerate(type_options):
@@ -91,17 +179,23 @@ def render_union_field( # noqa: PLR0911
91
179
  break
92
180
 
93
181
  type_descriptions = [desc for _, desc in type_options]
94
- type_key = f"{key}_type"
95
- selected_type_name = st.selectbox(
96
- f"Type for {label or key}",
97
- options=type_descriptions,
98
- index=default_index,
99
- key=type_key,
100
- disabled=disabled,
101
- help=help,
102
- )
103
- selected_type_index = type_descriptions.index(selected_type_name)
104
- selected_type, _ = type_options[selected_type_index]
182
+
183
+ # Skip type selector if there's only one option left
184
+ if len(type_options) == 1:
185
+ selected_type, selected_type_name = type_options[0]
186
+ else:
187
+ type_key = f"{key}_type"
188
+ selected_type_name = st.selectbox(
189
+ f"Type for {label or key}",
190
+ options=type_descriptions,
191
+ index=default_index,
192
+ key=type_key,
193
+ disabled=disabled,
194
+ help=help,
195
+ )
196
+ selected_type_index = type_descriptions.index(selected_type_name)
197
+ selected_type, selected_type_name = type_options[selected_type_index]
198
+
105
199
  field_key = f"{key}_value"
106
200
 
107
201
  # Special case for literal types
@@ -112,19 +206,14 @@ def render_union_field( # noqa: PLR0911
112
206
  value_index = literal_values.index(value)
113
207
 
114
208
  return st.selectbox(
115
- f"Value ({selected_type_name})",
209
+ f"Value ({selected_type_name if len(type_options) > 1 else label or key})",
116
210
  options=literal_values,
117
211
  index=value_index,
118
212
  key=field_key,
119
213
  disabled=disabled,
120
- help=help,
214
+ help=help if len(type_options) == 1 else None,
121
215
  )
122
216
 
123
- # Special case for None type
124
- if selected_type is type(None):
125
- st.info("None selected")
126
- return None
127
-
128
217
  # For other types, use standard field renderer
129
218
  modified_field_info = field_info.copy()
130
219
  modified_field_info["type"] = selected_type
@@ -150,31 +239,36 @@ def render_union_field( # noqa: PLR0911
150
239
  result = renderer(
151
240
  key=field_key,
152
241
  value=typed_value,
153
- label=f"Value ({selected_type_name})",
242
+ label=f"Value ({selected_type_name})" if len(type_options) > 1 else label or key,
154
243
  disabled=disabled,
155
- help=help,
244
+ help=help if len(type_options) == 1 else None,
156
245
  **modified_field_info,
157
246
  )
158
247
 
248
+ return convert_result_type(result, selected_type)
249
+
250
+
251
+ def convert_result_type(result: Any, target_type: Any) -> Any: # noqa: PLR0911
252
+ """Convert result to the target type, with error handling."""
159
253
  try:
160
- if selected_type is int and not isinstance(result, int):
254
+ if target_type is int and not isinstance(result, int):
161
255
  return int(result)
162
- if selected_type is float and not isinstance(result, float):
256
+ if target_type is float and not isinstance(result, float):
163
257
  return float(result)
164
- if selected_type is str and not isinstance(result, str):
258
+ if target_type is str and not isinstance(result, str):
165
259
  return str(result)
166
- if selected_type is bool and not isinstance(result, bool):
260
+ if target_type is bool and not isinstance(result, bool):
167
261
  return bool(result)
168
262
  except (ValueError, TypeError) as e:
169
- error_msg = f"Cannot convert {result} to {selected_type_name}: {e!s}"
263
+ error_msg = f"Cannot convert {result} to {target_type.__name__}: {e!s}"
170
264
  st.error(error_msg)
171
- if selected_type is int:
265
+ if target_type is int:
172
266
  return 0
173
- if selected_type is float:
267
+ if target_type is float:
174
268
  return 0.0
175
- if selected_type is str:
269
+ if target_type is str:
176
270
  return ""
177
- if selected_type is bool:
271
+ if target_type is bool:
178
272
  return False
179
273
  return None
180
274
  else:
@@ -898,7 +992,7 @@ if __name__ == "__main__":
898
992
  boolean: bool = True
899
993
  """Optional text field."""
900
994
 
901
- long_text: str | None = "test " * 40
995
+ long_text: str = "test " * 40
902
996
  """Long text."""
903
997
 
904
998
  optional_int: int | None = None
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes