streamlit-sortable-multiselect 0.1.1__py3-none-any.whl
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.
- streamlit_sortable_multiselect/__init__.py +102 -0
- streamlit_sortable_multiselect/frontend/build/assets/index-BJrb82NW.js +67 -0
- streamlit_sortable_multiselect/frontend/build/assets/index-BmQ7md0v.css +1 -0
- streamlit_sortable_multiselect/frontend/build/index.html +13 -0
- streamlit_sortable_multiselect-0.1.1.dist-info/METADATA +99 -0
- streamlit_sortable_multiselect-0.1.1.dist-info/RECORD +9 -0
- streamlit_sortable_multiselect-0.1.1.dist-info/WHEEL +5 -0
- streamlit_sortable_multiselect-0.1.1.dist-info/licenses/LICENSE +21 -0
- streamlit_sortable_multiselect-0.1.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Streamlit sortable multiselect component."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Iterable, Sequence, cast
|
|
8
|
+
|
|
9
|
+
import streamlit.components.v1 as components
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.1"
|
|
12
|
+
__all__ = ["sortable_multiselect"]
|
|
13
|
+
|
|
14
|
+
_COMPONENT_NAME = "streamlit_sortable_multiselect"
|
|
15
|
+
_DEV_SERVER_URL = "http://localhost:5173"
|
|
16
|
+
_RELEASE = os.environ.get("STREAMLIT_SORTABLE_MULTISELECT_DEV") != "1"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _declare_component():
|
|
20
|
+
if _RELEASE:
|
|
21
|
+
build_dir = Path(__file__).parent / "frontend" / "build"
|
|
22
|
+
return components.declare_component(_COMPONENT_NAME, path=str(build_dir))
|
|
23
|
+
return components.declare_component(_COMPONENT_NAME, url=_DEV_SERVER_URL)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_component_func = _declare_component()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _validate_string_sequence(name: str, values: Sequence[str] | None) -> list[str]:
|
|
30
|
+
if values is None:
|
|
31
|
+
return []
|
|
32
|
+
if isinstance(values, str) or not isinstance(values, Iterable):
|
|
33
|
+
raise TypeError(f"{name} must be a sequence of strings.")
|
|
34
|
+
|
|
35
|
+
result = list(values)
|
|
36
|
+
invalid = [value for value in result if not isinstance(value, str)]
|
|
37
|
+
if invalid:
|
|
38
|
+
raise TypeError(f"{name} must contain only strings.")
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def sortable_multiselect(
|
|
43
|
+
label: str,
|
|
44
|
+
options: Sequence[str],
|
|
45
|
+
default: Sequence[str] | None = None,
|
|
46
|
+
placeholder: str = "Select...",
|
|
47
|
+
disabled: bool = False,
|
|
48
|
+
key: str | None = None,
|
|
49
|
+
) -> list[str]:
|
|
50
|
+
"""Select multiple string values and return them in user-defined order.
|
|
51
|
+
|
|
52
|
+
Parameters
|
|
53
|
+
----------
|
|
54
|
+
label:
|
|
55
|
+
Text label rendered above the component.
|
|
56
|
+
options:
|
|
57
|
+
Available string values.
|
|
58
|
+
default:
|
|
59
|
+
Initially selected values, in the desired initial order.
|
|
60
|
+
placeholder:
|
|
61
|
+
Placeholder text shown in the add-item select control.
|
|
62
|
+
disabled:
|
|
63
|
+
Disable selection and ordering controls.
|
|
64
|
+
key:
|
|
65
|
+
Optional Streamlit component key.
|
|
66
|
+
"""
|
|
67
|
+
if not isinstance(label, str):
|
|
68
|
+
raise TypeError("label must be a string.")
|
|
69
|
+
if not isinstance(placeholder, str):
|
|
70
|
+
raise TypeError("placeholder must be a string.")
|
|
71
|
+
if not isinstance(disabled, bool):
|
|
72
|
+
raise TypeError("disabled must be a bool.")
|
|
73
|
+
|
|
74
|
+
option_values = _validate_string_sequence("options", options)
|
|
75
|
+
default_values = _validate_string_sequence("default", default)
|
|
76
|
+
|
|
77
|
+
duplicate_options = sorted({value for value in option_values if option_values.count(value) > 1})
|
|
78
|
+
if duplicate_options:
|
|
79
|
+
raise ValueError("options must not contain duplicate values.")
|
|
80
|
+
|
|
81
|
+
missing_defaults = [value for value in default_values if value not in option_values]
|
|
82
|
+
if missing_defaults:
|
|
83
|
+
missing = ", ".join(repr(value) for value in missing_defaults)
|
|
84
|
+
raise ValueError(f"default contains values not present in options: {missing}.")
|
|
85
|
+
|
|
86
|
+
duplicate_defaults = sorted({value for value in default_values if default_values.count(value) > 1})
|
|
87
|
+
if duplicate_defaults:
|
|
88
|
+
raise ValueError("default must not contain duplicate values.")
|
|
89
|
+
|
|
90
|
+
component_value = _component_func(
|
|
91
|
+
label=label,
|
|
92
|
+
options=option_values,
|
|
93
|
+
default_selected=default_values,
|
|
94
|
+
placeholder=placeholder,
|
|
95
|
+
disabled=disabled,
|
|
96
|
+
key=key,
|
|
97
|
+
default=default_values,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if component_value is None:
|
|
101
|
+
return default_values
|
|
102
|
+
return cast(list[str], component_value)
|