streambricks 0.2.14__tar.gz → 0.2.16__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.14 → streambricks-0.2.16}/PKG-INFO +1 -1
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/__init__.py +3 -1
- streambricks-0.2.16/src/streambricks/helpers.py +149 -0
- streambricks-0.2.16/src/streambricks/widgets/image_capture.py +45 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/main.py +142 -94
- streambricks-0.2.16/src/streambricks/widgets/sequence.py +112 -0
- streambricks-0.2.14/src/streambricks/helpers.py +0 -48
- {streambricks-0.2.14 → streambricks-0.2.16}/.copier-answers.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.github/dependabot.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.gitignore +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/LICENSE +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/README.md +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/docs/.empty +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/duties.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/mkdocs.yml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/pyproject.toml +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/state.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/__init__.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/type_helpers.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/tests/__init__.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/tests/conftest.py +0 -0
- {streambricks-0.2.14 → streambricks-0.2.16}/tests/test_widgets.py +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
__version__ = "0.2.
|
|
1
|
+
__version__ = "0.2.16"
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
from streambricks.widgets.model_widget import (
|
|
@@ -6,6 +6,7 @@ from streambricks.widgets.model_widget import (
|
|
|
6
6
|
render_model_readonly as model_display,
|
|
7
7
|
)
|
|
8
8
|
from streambricks.widgets.multi_select import multiselect, MultiSelectItem
|
|
9
|
+
from streambricks.widgets.image_capture import image_capture
|
|
9
10
|
from streambricks.widgets.model_selector import (
|
|
10
11
|
model_selector,
|
|
11
12
|
model_selector as llm_model_selector,
|
|
@@ -18,6 +19,7 @@ __all__ = [
|
|
|
18
19
|
"MultiSelectItem",
|
|
19
20
|
"State",
|
|
20
21
|
"bind_kwargs_as_widget",
|
|
22
|
+
"image_capture",
|
|
21
23
|
"llm_model_selector",
|
|
22
24
|
"model_display",
|
|
23
25
|
"model_edit",
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Utility functions for Streamlit applications."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from datetime import date, datetime, time
|
|
7
|
+
from decimal import Decimal
|
|
8
|
+
from enum import Enum
|
|
9
|
+
import inspect
|
|
10
|
+
import sys
|
|
11
|
+
from typing import TYPE_CHECKING, Any, get_args
|
|
12
|
+
|
|
13
|
+
import streamlit as st
|
|
14
|
+
from streamlit import runtime
|
|
15
|
+
from streamlit.web.cli import main
|
|
16
|
+
|
|
17
|
+
from streambricks.widgets.type_helpers import is_literal_type
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from collections.abc import Callable, Coroutine
|
|
22
|
+
|
|
23
|
+
from streamlit.elements.lib.column_types import ColumnConfig
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def run(fn: Callable[..., Any] | Coroutine[Any, Any, Any], *args: Any, **kwargs: Any):
|
|
27
|
+
"""Run a function or coroutine with Streamlit.
|
|
28
|
+
|
|
29
|
+
If Streamlit runtime exists, execute the function directly. Otherwise,
|
|
30
|
+
start Streamlit with the current script.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
fn: The function or coroutine to run
|
|
34
|
+
args: Positional arguments to pass to the function
|
|
35
|
+
kwargs: Keyword arguments to pass to the function
|
|
36
|
+
"""
|
|
37
|
+
if runtime.exists():
|
|
38
|
+
if inspect.iscoroutine(fn):
|
|
39
|
+
asyncio.run(fn)
|
|
40
|
+
# Handle coroutine function
|
|
41
|
+
elif inspect.iscoroutinefunction(fn):
|
|
42
|
+
coro = fn(*args, **kwargs)
|
|
43
|
+
asyncio.run(coro)
|
|
44
|
+
# Handle regular function
|
|
45
|
+
else:
|
|
46
|
+
fn(*args, **kwargs) # type: ignore
|
|
47
|
+
else:
|
|
48
|
+
sys.argv = ["streamlit", "run", sys.argv[0]]
|
|
49
|
+
sys.exit(main())
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def get_column_for_type( # noqa: PLR0911
|
|
53
|
+
typ: type,
|
|
54
|
+
title: str,
|
|
55
|
+
*,
|
|
56
|
+
required: bool = True,
|
|
57
|
+
help: str | None = None, # noqa: A002
|
|
58
|
+
**kwargs: Any,
|
|
59
|
+
) -> ColumnConfig:
|
|
60
|
+
"""Get appropriate column config for a given type.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
typ: The type to create a column for
|
|
64
|
+
title: Column title/label
|
|
65
|
+
required: Whether the field is required
|
|
66
|
+
help: Help text for the column
|
|
67
|
+
**kwargs: Additional arguments passed to column constructor
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
Configured column for the type
|
|
71
|
+
"""
|
|
72
|
+
# Handle literal types
|
|
73
|
+
if is_literal_type(typ):
|
|
74
|
+
options = get_args(typ)
|
|
75
|
+
return st.column_config.SelectboxColumn(
|
|
76
|
+
title,
|
|
77
|
+
help=help,
|
|
78
|
+
required=required,
|
|
79
|
+
options=options,
|
|
80
|
+
**kwargs,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# Handle enums
|
|
84
|
+
if isinstance(typ, type) and issubclass(typ, Enum):
|
|
85
|
+
return st.column_config.SelectboxColumn(
|
|
86
|
+
title,
|
|
87
|
+
help=help,
|
|
88
|
+
required=required,
|
|
89
|
+
options=list(typ.__members__.values()), # type: ignore
|
|
90
|
+
**kwargs,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Match on type
|
|
94
|
+
match typ:
|
|
95
|
+
case type() as t if t is bool:
|
|
96
|
+
return st.column_config.CheckboxColumn(
|
|
97
|
+
title,
|
|
98
|
+
help=help,
|
|
99
|
+
**kwargs,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
case type() as t if t in (int, float, Decimal):
|
|
103
|
+
return st.column_config.NumberColumn(
|
|
104
|
+
title,
|
|
105
|
+
help=help,
|
|
106
|
+
required=required,
|
|
107
|
+
**kwargs,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
case type() as t if t is str:
|
|
111
|
+
return st.column_config.TextColumn(
|
|
112
|
+
title,
|
|
113
|
+
help=help,
|
|
114
|
+
required=required,
|
|
115
|
+
**kwargs,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
case type() as t if t is datetime:
|
|
119
|
+
return st.column_config.DatetimeColumn(
|
|
120
|
+
title,
|
|
121
|
+
help=help,
|
|
122
|
+
required=required,
|
|
123
|
+
**kwargs,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
case type() as t if t is date:
|
|
127
|
+
return st.column_config.DateColumn(
|
|
128
|
+
title,
|
|
129
|
+
help=help,
|
|
130
|
+
required=required,
|
|
131
|
+
**kwargs,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
case type() as t if t is time:
|
|
135
|
+
return st.column_config.TimeColumn(
|
|
136
|
+
title,
|
|
137
|
+
help=help,
|
|
138
|
+
required=required,
|
|
139
|
+
**kwargs,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
case _:
|
|
143
|
+
# Default to generic column for unsupported types
|
|
144
|
+
return st.column_config.Column(
|
|
145
|
+
title,
|
|
146
|
+
help=help,
|
|
147
|
+
required=required,
|
|
148
|
+
**kwargs,
|
|
149
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Model selector component for streamlit applications."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
import streamlit as st
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def image_capture(
|
|
15
|
+
*,
|
|
16
|
+
image_label: str | None = None,
|
|
17
|
+
camera_label: str | None = None,
|
|
18
|
+
supported_types: list[str] | None = None,
|
|
19
|
+
) -> UploadedFile | None:
|
|
20
|
+
"""Capture an image from a file upload or camera.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
image_label: Label for the file upload widget.
|
|
24
|
+
camera_label: Label for the camera input widget.
|
|
25
|
+
supported_types: List of supported file types for image upload.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
Uploaded file or None if not selected.
|
|
29
|
+
"""
|
|
30
|
+
tab1, tab2 = st.tabs(["Upload file", "Use camera"])
|
|
31
|
+
with tab1:
|
|
32
|
+
types_ = supported_types or ["pdf", "png", "jpg", "jpeg"]
|
|
33
|
+
supported_str = f"Supported file types: {', '.join(types_)}"
|
|
34
|
+
uploaded_file = st.file_uploader(
|
|
35
|
+
image_label or "Upload picture", type=types_, help=supported_str
|
|
36
|
+
)
|
|
37
|
+
with tab2:
|
|
38
|
+
camera_file = st.camera_input(
|
|
39
|
+
camera_label or "Use camera to capture image",
|
|
40
|
+
)
|
|
41
|
+
if uploaded_file is not None:
|
|
42
|
+
return uploaded_file
|
|
43
|
+
if camera_file is not None:
|
|
44
|
+
return camera_file
|
|
45
|
+
return None
|
|
@@ -69,7 +69,7 @@ PRIMITIVE_RENDERERS = {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
def render_union_field(
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
key
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
|
254
|
+
if target_type is int and not isinstance(result, int):
|
|
161
255
|
return int(result)
|
|
162
|
-
if
|
|
256
|
+
if target_type is float and not isinstance(result, float):
|
|
163
257
|
return float(result)
|
|
164
|
-
if
|
|
258
|
+
if target_type is str and not isinstance(result, str):
|
|
165
259
|
return str(result)
|
|
166
|
-
if
|
|
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 {
|
|
263
|
+
error_msg = f"Cannot convert {result} to {target_type.__name__}: {e!s}"
|
|
170
264
|
st.error(error_msg)
|
|
171
|
-
if
|
|
265
|
+
if target_type is int:
|
|
172
266
|
return 0
|
|
173
|
-
if
|
|
267
|
+
if target_type is float:
|
|
174
268
|
return 0.0
|
|
175
|
-
if
|
|
269
|
+
if target_type is str:
|
|
176
270
|
return ""
|
|
177
|
-
if
|
|
271
|
+
if target_type is bool:
|
|
178
272
|
return False
|
|
179
273
|
return None
|
|
180
274
|
else:
|
|
@@ -199,73 +293,27 @@ def render_sequence_field(
|
|
|
199
293
|
disabled: bool = False,
|
|
200
294
|
help: str | None = None, # noqa: A002
|
|
201
295
|
**field_info: Any,
|
|
202
|
-
) -> list[Any]:
|
|
296
|
+
) -> list[Any] | tuple[Any, ...]:
|
|
203
297
|
"""Render a field for sequence types (list, tuple, set)."""
|
|
204
|
-
|
|
205
|
-
items_key = f"{key}_items"
|
|
206
|
-
if items_key not in st.session_state:
|
|
207
|
-
st.session_state[items_key] = list(value) if value is not None else []
|
|
208
|
-
item_type = get_inner_type(field_info)
|
|
209
|
-
st.markdown(f"**{label or key}**")
|
|
210
|
-
if help:
|
|
211
|
-
st.caption(help)
|
|
298
|
+
from streambricks.widgets.sequence import sequence_widget
|
|
212
299
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
)
|
|
224
|
-
|
|
225
|
-
return st.session_state[items_key]
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
def render_sequence_items(
|
|
229
|
-
items: list,
|
|
230
|
-
item_type: Any,
|
|
231
|
-
key: str,
|
|
232
|
-
items_key: str,
|
|
233
|
-
disabled: bool,
|
|
234
|
-
field_info: dict,
|
|
235
|
-
) -> None:
|
|
236
|
-
"""Render items in a sequence with delete buttons."""
|
|
237
|
-
item_info = field_info.copy()
|
|
238
|
-
item_info["type"] = item_type
|
|
239
|
-
item_info["inside_expander"] = True
|
|
240
|
-
|
|
241
|
-
try:
|
|
242
|
-
renderer = get_field_renderer(item_info)
|
|
243
|
-
items_to_delete = []
|
|
244
|
-
for i, item in enumerate(items):
|
|
245
|
-
st.divider()
|
|
246
|
-
st.markdown(f"**Item {i + 1}**")
|
|
247
|
-
|
|
248
|
-
# Render the item
|
|
249
|
-
items[i] = renderer(
|
|
250
|
-
key=f"{key}_item_{i}",
|
|
251
|
-
value=item,
|
|
252
|
-
label=f"Item {i + 1}",
|
|
253
|
-
disabled=disabled,
|
|
254
|
-
**item_info,
|
|
255
|
-
)
|
|
256
|
-
|
|
257
|
-
delete_key = f"{key}_delete_{i}"
|
|
258
|
-
if st.button("Delete Item", key=delete_key, disabled=disabled):
|
|
259
|
-
items_to_delete.append(i)
|
|
300
|
+
item_type = get_inner_type(field_info)
|
|
301
|
+
result = sequence_widget(
|
|
302
|
+
label=label or key,
|
|
303
|
+
item_type=item_type,
|
|
304
|
+
value=value, # type: ignore
|
|
305
|
+
key=key,
|
|
306
|
+
disabled=disabled,
|
|
307
|
+
help=help,
|
|
308
|
+
**field_info,
|
|
309
|
+
)
|
|
260
310
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
st.rerun() # Force rerun after deletion
|
|
311
|
+
# Cast back to original sequence type if needed
|
|
312
|
+
annotation = field_info.get("type")
|
|
313
|
+
if annotation is tuple or (get_origin(annotation) is tuple):
|
|
314
|
+
return tuple(result)
|
|
266
315
|
|
|
267
|
-
|
|
268
|
-
st.error(f"Error rendering sequence items: {e!s}")
|
|
316
|
+
return result
|
|
269
317
|
|
|
270
318
|
|
|
271
319
|
def render_set_field(
|
|
@@ -898,7 +946,7 @@ if __name__ == "__main__":
|
|
|
898
946
|
boolean: bool = True
|
|
899
947
|
"""Optional text field."""
|
|
900
948
|
|
|
901
|
-
long_text: str
|
|
949
|
+
long_text: str = "test " * 40
|
|
902
950
|
"""Long text."""
|
|
903
951
|
|
|
904
952
|
optional_int: int | None = None
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Sequence management widget for streamlit applications."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import streamlit as st
|
|
8
|
+
|
|
9
|
+
from streambricks.widgets.model_widget.main import get_field_renderer
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def sequence_widget[T](
|
|
13
|
+
label: str,
|
|
14
|
+
item_type: type[T],
|
|
15
|
+
value: list[T] | None = None,
|
|
16
|
+
key: str | None = None,
|
|
17
|
+
disabled: bool = False,
|
|
18
|
+
help: str | None = None, # noqa: A002
|
|
19
|
+
**field_info: Any,
|
|
20
|
+
) -> list[T]:
|
|
21
|
+
"""Create a widget for managing sequences (lists) of items.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
label: Label to display
|
|
25
|
+
item_type: Type of items in the sequence
|
|
26
|
+
value: Current sequence value
|
|
27
|
+
key: Optional unique key for widget state
|
|
28
|
+
disabled: Whether the widget is disabled
|
|
29
|
+
help: Help text to display
|
|
30
|
+
field_info: Additional field information
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
Updated sequence of items
|
|
34
|
+
"""
|
|
35
|
+
widget_key = key or label
|
|
36
|
+
items_key = f"{widget_key}_items"
|
|
37
|
+
add_item_key = f"{widget_key}_add_item"
|
|
38
|
+
if items_key not in st.session_state:
|
|
39
|
+
st.session_state[items_key] = list(value) if value is not None else []
|
|
40
|
+
|
|
41
|
+
label_col, widget_col = st.columns([1, 3])
|
|
42
|
+
with label_col:
|
|
43
|
+
st.markdown(f"**{label}**")
|
|
44
|
+
if help:
|
|
45
|
+
st.caption(help)
|
|
46
|
+
|
|
47
|
+
with widget_col:
|
|
48
|
+
if st.button("Add Item", key=add_item_key, disabled=disabled):
|
|
49
|
+
from streambricks.widgets.type_helpers import add_new_item
|
|
50
|
+
|
|
51
|
+
add_new_item(st.session_state[items_key], item_type)
|
|
52
|
+
|
|
53
|
+
items_to_delete = []
|
|
54
|
+
item_info = field_info.copy()
|
|
55
|
+
item_info["type"] = item_type
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
renderer = get_field_renderer(item_info)
|
|
59
|
+
for i, item in enumerate(st.session_state[items_key]):
|
|
60
|
+
cols = st.columns([0.8, 0.2])
|
|
61
|
+
with cols[0]:
|
|
62
|
+
st.session_state[items_key][i] = renderer(
|
|
63
|
+
key=f"{widget_key}_item_{i}",
|
|
64
|
+
value=item,
|
|
65
|
+
label=f"Item {i + 1}",
|
|
66
|
+
disabled=disabled,
|
|
67
|
+
**item_info,
|
|
68
|
+
)
|
|
69
|
+
with cols[1]:
|
|
70
|
+
st.write("")
|
|
71
|
+
if st.button("🗑️", key=f"{widget_key}_delete_{i}", disabled=disabled):
|
|
72
|
+
items_to_delete.append(i)
|
|
73
|
+
|
|
74
|
+
if items_to_delete:
|
|
75
|
+
for idx in sorted(items_to_delete, reverse=True):
|
|
76
|
+
if 0 <= idx < len(st.session_state[items_key]):
|
|
77
|
+
st.session_state[items_key].pop(idx)
|
|
78
|
+
st.rerun()
|
|
79
|
+
|
|
80
|
+
except Exception as e: # noqa: BLE001
|
|
81
|
+
st.error(f"Error rendering sequence items: {e!s}")
|
|
82
|
+
|
|
83
|
+
return st.session_state[items_key]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
from pydantic import BaseModel
|
|
88
|
+
|
|
89
|
+
from streambricks.helpers import run
|
|
90
|
+
|
|
91
|
+
class SubItem(BaseModel):
|
|
92
|
+
name: str = ""
|
|
93
|
+
value: int = 0
|
|
94
|
+
|
|
95
|
+
def demo():
|
|
96
|
+
st.title("Sequence Widget Demo")
|
|
97
|
+
numbers = sequence_widget(
|
|
98
|
+
"Numbers",
|
|
99
|
+
int,
|
|
100
|
+
value=[1, 2, 3],
|
|
101
|
+
help="A list of integers",
|
|
102
|
+
)
|
|
103
|
+
st.write("Current numbers:", numbers)
|
|
104
|
+
items = sequence_widget(
|
|
105
|
+
"Complex Items",
|
|
106
|
+
SubItem,
|
|
107
|
+
value=[SubItem(name="Item 1", value=42)],
|
|
108
|
+
help="A list of SubItems",
|
|
109
|
+
)
|
|
110
|
+
st.write("Current items:", [item.model_dump() for item in items])
|
|
111
|
+
|
|
112
|
+
run(demo)
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"""Utility functions for Streamlit applications."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import asyncio
|
|
6
|
-
import inspect
|
|
7
|
-
import sys
|
|
8
|
-
from typing import TYPE_CHECKING, Any, TypeVar
|
|
9
|
-
|
|
10
|
-
from streamlit import runtime
|
|
11
|
-
from streamlit.web.cli import main
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if TYPE_CHECKING:
|
|
15
|
-
from collections.abc import Callable, Coroutine
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
T = TypeVar("T")
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def run(
|
|
22
|
-
fn: Callable[..., T | Coroutine[Any, Any, T]] | Coroutine[Any, Any, T],
|
|
23
|
-
*args: Any,
|
|
24
|
-
**kwargs: Any,
|
|
25
|
-
) -> None:
|
|
26
|
-
"""Run a function or coroutine with Streamlit.
|
|
27
|
-
|
|
28
|
-
If Streamlit runtime exists, execute the function directly. Otherwise,
|
|
29
|
-
start Streamlit with the current script.
|
|
30
|
-
|
|
31
|
-
Args:
|
|
32
|
-
fn: The function or coroutine to run
|
|
33
|
-
args: Positional arguments to pass to the function
|
|
34
|
-
kwargs: Keyword arguments to pass to the function
|
|
35
|
-
"""
|
|
36
|
-
if runtime.exists():
|
|
37
|
-
if inspect.iscoroutine(fn):
|
|
38
|
-
asyncio.run(fn)
|
|
39
|
-
# Handle coroutine function
|
|
40
|
-
elif inspect.iscoroutinefunction(fn):
|
|
41
|
-
coro = fn(*args, **kwargs)
|
|
42
|
-
asyncio.run(coro)
|
|
43
|
-
# Handle regular function
|
|
44
|
-
else:
|
|
45
|
-
fn(*args, **kwargs) # type: ignore
|
|
46
|
-
else:
|
|
47
|
-
sys.argv = ["streamlit", "run", sys.argv[0]]
|
|
48
|
-
sys.exit(main())
|
|
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
|
{streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{streambricks-0.2.14 → streambricks-0.2.16}/src/streambricks/widgets/model_widget/type_renderers.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|