numerous 0.0.0__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.
Potentially problematic release.
This version of numerous might be problematic. Click here for more details.
- numerous/__init__.py +5 -0
- numerous/__main__.py +50 -0
- numerous/_plotly.py +5 -0
- numerous/appdev/__init__.py +1 -0
- numerous/appdev/commands.py +207 -0
- numerous/apps.py +155 -0
- numerous/cli/__init__.py +5 -0
- numerous/cli/__main__.py +6 -0
- numerous/cli/_color.py +48 -0
- numerous/cli/_upgrade.py +115 -0
- numerous/cli/build/darwin_amd64 +0 -0
- numerous/cli/build/darwin_arm64 +0 -0
- numerous/cli/build/linux_amd64 +0 -0
- numerous/cli/build/linux_arm64 +0 -0
- numerous/cli/build/windows_amd64 +0 -0
- numerous/cli/build/windows_arm64 +0 -0
- numerous/cli/run.py +55 -0
- numerous/data_model.py +210 -0
- numerous/experimental/__init__.py +8 -0
- numerous/experimental/marimo.py +172 -0
- numerous/experimental/model.py +372 -0
- numerous/generated/graphql/__init__.py +133 -0
- numerous/generated/graphql/all_elements.py +118 -0
- numerous/generated/graphql/async_base_client.py +372 -0
- numerous/generated/graphql/base_model.py +29 -0
- numerous/generated/graphql/client.py +179 -0
- numerous/generated/graphql/enums.py +20 -0
- numerous/generated/graphql/exceptions.py +85 -0
- numerous/generated/graphql/fragments.py +81 -0
- numerous/generated/graphql/input_types.py +40 -0
- numerous/generated/graphql/update_element.py +26 -0
- numerous/generated/graphql/updates.py +149 -0
- numerous/py.typed +0 -0
- numerous/session.py +520 -0
- numerous/tools/__init__.py +11 -0
- numerous/updates.py +179 -0
- numerous/utils.py +12 -0
- numerous-0.0.0.dist-info/METADATA +128 -0
- numerous-0.0.0.dist-info/RECORD +42 -0
- numerous-0.0.0.dist-info/WHEEL +5 -0
- numerous-0.0.0.dist-info/entry_points.txt +2 -0
- numerous-0.0.0.dist-info/top_level.txt +1 -0
numerous/data_model.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""Tool data model for communication."""
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Any, Optional, Type
|
|
6
|
+
|
|
7
|
+
import plotly.graph_objects as go
|
|
8
|
+
|
|
9
|
+
from numerous._plotly import plotly_html
|
|
10
|
+
from numerous.apps import HTML, Field, Slider
|
|
11
|
+
from numerous.utils import MISSING, AppT
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class ElementDataModel:
|
|
16
|
+
name: str
|
|
17
|
+
label: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class HTMLElementDataModel(ElementDataModel):
|
|
22
|
+
default: str
|
|
23
|
+
type: str = "html"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class TextFieldDataModel(ElementDataModel):
|
|
28
|
+
default: str
|
|
29
|
+
type: str = "string"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class NumberFieldDataModel(ElementDataModel):
|
|
34
|
+
default: float
|
|
35
|
+
type: str = "number"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class PlotlyElementDataModel(ElementDataModel):
|
|
40
|
+
default: str
|
|
41
|
+
type: str = "html"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class SliderElementDataModel(ElementDataModel):
|
|
46
|
+
default: float
|
|
47
|
+
slider_min_value: float
|
|
48
|
+
slider_max_value: float
|
|
49
|
+
type: str = "slider"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class ActionDataModel(ElementDataModel):
|
|
54
|
+
type: str = "action"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True)
|
|
58
|
+
class ContainerDataModel(ElementDataModel):
|
|
59
|
+
elements: list[ElementDataModel]
|
|
60
|
+
type: str = "container"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class AppDataModel:
|
|
65
|
+
name: str
|
|
66
|
+
title: str
|
|
67
|
+
elements: list[ElementDataModel]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class ToolDataModelError(Exception):
|
|
71
|
+
def __init__(self, cls: Type[AppT], name: str, annotation: str) -> None:
|
|
72
|
+
super().__init__(
|
|
73
|
+
f"Invalid Tool. Unsupport field: {cls.__name__}.{name}: {annotation}",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class ToolDefinitionError(Exception):
|
|
78
|
+
def __init__(self, cls: Type[AppT]) -> None:
|
|
79
|
+
self.cls = cls
|
|
80
|
+
super().__init__(
|
|
81
|
+
f"Tool {self.cls.__name__} not defined with the @app decorator",
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def dump_data_model(cls: Type[AppT]) -> AppDataModel:
|
|
86
|
+
if not getattr(cls, "__numerous_app__", False):
|
|
87
|
+
raise ToolDefinitionError(cls)
|
|
88
|
+
|
|
89
|
+
title = getattr(cls, "__title__", cls.__name__)
|
|
90
|
+
|
|
91
|
+
return AppDataModel(
|
|
92
|
+
name=cls.__name__,
|
|
93
|
+
title=title,
|
|
94
|
+
elements=_dump_element_data_models(cls),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _dump_element_from_annotation(
|
|
99
|
+
name: str,
|
|
100
|
+
annotation: Any, # noqa: ANN401
|
|
101
|
+
default: Any, # noqa: ANN401
|
|
102
|
+
) -> Optional[ElementDataModel]:
|
|
103
|
+
if annotation is str and (
|
|
104
|
+
data_model := _dump_element_from_str_annotation(name, default)
|
|
105
|
+
):
|
|
106
|
+
return data_model
|
|
107
|
+
|
|
108
|
+
if annotation is float and (
|
|
109
|
+
data_model := _dump_element_from_float_annotation(name, default)
|
|
110
|
+
):
|
|
111
|
+
return data_model
|
|
112
|
+
|
|
113
|
+
if annotation is go.Figure:
|
|
114
|
+
label = default.label if isinstance(default, Field) else name
|
|
115
|
+
default = (
|
|
116
|
+
plotly_html(default.default)
|
|
117
|
+
if isinstance(default, Field) and isinstance(default.default, go.Figure)
|
|
118
|
+
else ""
|
|
119
|
+
)
|
|
120
|
+
return PlotlyElementDataModel(name=name, label=label or name, default=default)
|
|
121
|
+
|
|
122
|
+
if getattr(annotation, "__container__", False):
|
|
123
|
+
return dump_container_data_model(name, annotation)
|
|
124
|
+
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _dump_element_from_float_annotation(
|
|
129
|
+
name: str,
|
|
130
|
+
default: Any, # noqa: ANN401
|
|
131
|
+
) -> Optional[ElementDataModel]:
|
|
132
|
+
if isinstance(default, (float, int)) or default is MISSING:
|
|
133
|
+
return NumberFieldDataModel(
|
|
134
|
+
name=name,
|
|
135
|
+
label=name,
|
|
136
|
+
default=float(0.0 if default is MISSING else default),
|
|
137
|
+
)
|
|
138
|
+
if isinstance(default, Slider):
|
|
139
|
+
default_value = (
|
|
140
|
+
default.min_value
|
|
141
|
+
if default.default is MISSING # type: ignore[comparison-overlap]
|
|
142
|
+
else default.default
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
return SliderElementDataModel(
|
|
146
|
+
name=name,
|
|
147
|
+
label=default.label if default.label else name,
|
|
148
|
+
default=default_value,
|
|
149
|
+
slider_min_value=default.min_value,
|
|
150
|
+
slider_max_value=default.max_value,
|
|
151
|
+
)
|
|
152
|
+
if isinstance(default, Field):
|
|
153
|
+
return NumberFieldDataModel(
|
|
154
|
+
name=name,
|
|
155
|
+
label=default.label if default.label else name,
|
|
156
|
+
default=float(0.0 if default.default is MISSING else default.default), # type: ignore[comparison-overlap]
|
|
157
|
+
)
|
|
158
|
+
return None
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _dump_element_from_str_annotation(
|
|
162
|
+
name: str,
|
|
163
|
+
default: Any, # noqa: ANN401
|
|
164
|
+
) -> Optional[ElementDataModel]:
|
|
165
|
+
if isinstance(default, str) or default is MISSING:
|
|
166
|
+
default = "" if default is MISSING else default
|
|
167
|
+
return TextFieldDataModel(name=name, label=name, default=default)
|
|
168
|
+
if isinstance(default, HTML):
|
|
169
|
+
return HTMLElementDataModel(name=name, label=name, default=default.default)
|
|
170
|
+
if isinstance(default, Field):
|
|
171
|
+
return TextFieldDataModel(
|
|
172
|
+
name=name,
|
|
173
|
+
label=default.label if default.label else name,
|
|
174
|
+
default=str("" if default.default is MISSING else default.default), # type: ignore[comparison-overlap]
|
|
175
|
+
)
|
|
176
|
+
return None
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _dump_element_data_models(cls: Type[AppT]) -> list[ElementDataModel]:
|
|
180
|
+
elements: list[ElementDataModel] = []
|
|
181
|
+
annotations = cls.__annotations__ if hasattr(cls, "__annotations__") else {}
|
|
182
|
+
for name, annotation in annotations.items():
|
|
183
|
+
default = getattr(cls, name, MISSING)
|
|
184
|
+
if elem := _dump_element_from_annotation(name, annotation, default):
|
|
185
|
+
elements.append(elem)
|
|
186
|
+
else:
|
|
187
|
+
raise ToolDataModelError(cls, name, annotation)
|
|
188
|
+
|
|
189
|
+
for name, func in inspect.getmembers(cls, inspect.isfunction):
|
|
190
|
+
if getattr(func, "__action__", False):
|
|
191
|
+
elements.append(ActionDataModel(name, name))
|
|
192
|
+
|
|
193
|
+
return elements
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class ContainerDefinitionError(Exception):
|
|
197
|
+
def __init__(self, cls: Type[AppT]) -> None:
|
|
198
|
+
self.cls = cls
|
|
199
|
+
super().__init__(
|
|
200
|
+
f"Container {self.cls.__name__} not defined with the @container decorator",
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def dump_container_data_model(name: str, cls: Type[AppT]) -> ContainerDataModel:
|
|
205
|
+
if not getattr(cls, "__container__", False):
|
|
206
|
+
raise ContainerDefinitionError(cls)
|
|
207
|
+
|
|
208
|
+
elements = _dump_element_data_models(cls)
|
|
209
|
+
|
|
210
|
+
return ContainerDataModel(name=name, label=name, elements=elements)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Marimo Fields.
|
|
3
|
+
|
|
4
|
+
The marimo module provides classes and functions for creating fields
|
|
5
|
+
specifically useful in marimo apps.
|
|
6
|
+
|
|
7
|
+
Marimo is a Python library for building interactive web applications.
|
|
8
|
+
|
|
9
|
+
This module contains the `Field` class, which is a field with a state that
|
|
10
|
+
can be used in a Marimo app. It also provides utility functions
|
|
11
|
+
for creating different types of UI elements such as sliders,
|
|
12
|
+
numbers, and text fields.
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from typing import Any, Dict, Type, TypeVar, Union
|
|
17
|
+
|
|
18
|
+
import marimo as mo
|
|
19
|
+
from marimo._runtime.state import State as MoState
|
|
20
|
+
|
|
21
|
+
from numerous.experimental.model import Field as BaseField
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _auto_label(key: str, label: Union[str, None]) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Automatically assigns a label to a key if the label is None.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
----
|
|
30
|
+
key (str): The key to be labeled.
|
|
31
|
+
label (str): The label to assign to the key. If None,
|
|
32
|
+
the key will be used as the label.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
-------
|
|
36
|
+
str: The assigned label.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
if label is None:
|
|
40
|
+
label = key
|
|
41
|
+
|
|
42
|
+
return label
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
T = TypeVar("T", mo.ui.number, mo.ui.slider)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Field(BaseField, MoState[Union[str, float, int]]):
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
default: Union[str, float, None] = None,
|
|
52
|
+
annotation: Union[type, None] = None,
|
|
53
|
+
**kwargs: Dict[str, Any],
|
|
54
|
+
) -> None:
|
|
55
|
+
"""
|
|
56
|
+
Field with a state that can be used in a Marimo app.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
----
|
|
60
|
+
default (str | float, optional): The default value for the field.
|
|
61
|
+
Defaults to `...`.
|
|
62
|
+
annotation (type | None, optional): The type annotation for the field.
|
|
63
|
+
Defaults to None.
|
|
64
|
+
**kwargs (dict): Additional keyword arguments for the field.
|
|
65
|
+
|
|
66
|
+
"""
|
|
67
|
+
BaseField.__init__(self, default=default, annotation=annotation, **kwargs)
|
|
68
|
+
MoState.__init__(self, self.value)
|
|
69
|
+
|
|
70
|
+
def _number_ui(
|
|
71
|
+
self,
|
|
72
|
+
ui_cls: Type[T],
|
|
73
|
+
step: float = 1,
|
|
74
|
+
label: Union[str, None] = None,
|
|
75
|
+
) -> T:
|
|
76
|
+
if not hasattr(self, "field_info"):
|
|
77
|
+
error_msg = "The field_info attribute is not defined."
|
|
78
|
+
raise AttributeError(error_msg)
|
|
79
|
+
_min = self.field_info.metadata[0].ge
|
|
80
|
+
_max = self.field_info.metadata[1].le
|
|
81
|
+
|
|
82
|
+
return ui_cls(
|
|
83
|
+
_min,
|
|
84
|
+
_max,
|
|
85
|
+
value=float(self.get()),
|
|
86
|
+
on_change=self.set,
|
|
87
|
+
label=_auto_label(self.name, label),
|
|
88
|
+
step=step,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def slider(
|
|
92
|
+
self,
|
|
93
|
+
step: float = 1,
|
|
94
|
+
label: Union[str, None] = None,
|
|
95
|
+
) -> mo.ui.slider:
|
|
96
|
+
"""
|
|
97
|
+
Create a slider UI element.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
----
|
|
101
|
+
step (float, optional): The step size for the slider. Defaults to 1.
|
|
102
|
+
label (str | None, optional): The label for the slider. Defaults to None.
|
|
103
|
+
Defaults to None.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
-------
|
|
107
|
+
mo.ui.slider: The created slider UI element.
|
|
108
|
+
|
|
109
|
+
"""
|
|
110
|
+
return self._number_ui(mo.ui.slider, step, label)
|
|
111
|
+
|
|
112
|
+
def number(
|
|
113
|
+
self,
|
|
114
|
+
step: float = 1,
|
|
115
|
+
label: Union[str, None] = None,
|
|
116
|
+
) -> mo.ui.number:
|
|
117
|
+
"""
|
|
118
|
+
Create a number UI element.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
----
|
|
122
|
+
step (float, optional): The step value for the number UI element.
|
|
123
|
+
Defaults to 1.
|
|
124
|
+
label (str, optional): The label for the number UI element.
|
|
125
|
+
Defaults to None.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
-------
|
|
129
|
+
mo.ui.number: The created number UI element.
|
|
130
|
+
|
|
131
|
+
"""
|
|
132
|
+
number_ui = self._number_ui(mo.ui.number, step, label)
|
|
133
|
+
|
|
134
|
+
if isinstance(number_ui, mo.ui.number):
|
|
135
|
+
return number_ui
|
|
136
|
+
|
|
137
|
+
error_msg = "The number UI element is not an instance of mo.ui.number."
|
|
138
|
+
raise TypeError(error_msg)
|
|
139
|
+
|
|
140
|
+
def text(self, label: Union[str, None] = None) -> mo.ui.text:
|
|
141
|
+
"""
|
|
142
|
+
Return a text field widget.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
----
|
|
146
|
+
label (str, optional): The label for the text field. Defaults to None.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
-------
|
|
150
|
+
mo.ui.text: The text field widget.
|
|
151
|
+
|
|
152
|
+
"""
|
|
153
|
+
return mo.ui.text(
|
|
154
|
+
value=str(self.get()),
|
|
155
|
+
on_change=self.set,
|
|
156
|
+
label=_auto_label(self.name, label),
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def set(self, value: Any) -> None: # noqa: ANN401
|
|
160
|
+
"""
|
|
161
|
+
Set the value of the Marimo object.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
----
|
|
165
|
+
value (int | str): The value to be set. It can be an integer or a string.
|
|
166
|
+
|
|
167
|
+
Overrides the set method to call the mo state set_value and then calls the super
|
|
168
|
+
class's set method.
|
|
169
|
+
|
|
170
|
+
"""
|
|
171
|
+
self._set_value(value)
|
|
172
|
+
super().set(value)
|