marimo-utils 0.1.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.
marimo_utils/__init__.py
ADDED
marimo_utils/display.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
import sys
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, TypeVar
|
|
8
|
+
|
|
9
|
+
import marimo as mo
|
|
10
|
+
from marimo._plugins.ui._core.ui_element import UIElement
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
|
|
13
|
+
TModel = TypeVar("TModel", bound=BaseModel)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"add_marimo_display",
|
|
17
|
+
"defining_path",
|
|
18
|
+
"relative_to_safe",
|
|
19
|
+
"render_model",
|
|
20
|
+
"resolve_repo_root",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def defining_path(obj: Any) -> Path | None:
|
|
25
|
+
"""Best-effort path to the file that defined `obj`."""
|
|
26
|
+
src = inspect.getsourcefile(obj)
|
|
27
|
+
if not src:
|
|
28
|
+
try:
|
|
29
|
+
src = inspect.getfile(obj)
|
|
30
|
+
except TypeError:
|
|
31
|
+
src = None
|
|
32
|
+
if src:
|
|
33
|
+
return Path(src).resolve()
|
|
34
|
+
mod_name = getattr(obj, "__module__", None)
|
|
35
|
+
mod = sys.modules.get(mod_name) if mod_name else None
|
|
36
|
+
mod_file = getattr(mod, "__file__", None) if mod else None
|
|
37
|
+
return Path(mod_file).resolve() if mod_file else None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def resolve_repo_root(model: BaseModel) -> Path:
|
|
41
|
+
if hasattr(model, "repo_root"):
|
|
42
|
+
repo_root = getattr(model, "repo_root")
|
|
43
|
+
if repo_root:
|
|
44
|
+
return Path(str(repo_root))
|
|
45
|
+
if hasattr(model, "paths"):
|
|
46
|
+
paths = getattr(model, "paths")
|
|
47
|
+
if paths and hasattr(paths, "repo_root"):
|
|
48
|
+
repo_root = getattr(paths, "repo_root")
|
|
49
|
+
if repo_root:
|
|
50
|
+
return Path(str(repo_root))
|
|
51
|
+
return Path(__file__).resolve().parent.parent.parent
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def relative_to_safe(path: Path, base: Path) -> Path:
|
|
55
|
+
try:
|
|
56
|
+
return path.relative_to(base)
|
|
57
|
+
except ValueError:
|
|
58
|
+
return path
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def render_model(
|
|
62
|
+
model: BaseModel, class_path: Path | str | None
|
|
63
|
+
) -> UIElement | mo.Html:
|
|
64
|
+
class_path = class_path or "Unknown"
|
|
65
|
+
rel_path = (
|
|
66
|
+
class_path
|
|
67
|
+
if isinstance(class_path, str)
|
|
68
|
+
else relative_to_safe(class_path, resolve_repo_root(model))
|
|
69
|
+
)
|
|
70
|
+
rel_path_str = f"<small>`{rel_path}`</small>"
|
|
71
|
+
return mo.vstack(
|
|
72
|
+
[
|
|
73
|
+
mo.md(f"**{model.__class__.__name__}** | {rel_path_str}"),
|
|
74
|
+
model.model_dump(),
|
|
75
|
+
]
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def add_marimo_display() -> Callable[[type[TModel]], type[TModel]]:
|
|
80
|
+
"""Add a `_display_` method to Pydantic models for marimo rendering."""
|
|
81
|
+
|
|
82
|
+
def decorator(cls: type[TModel]) -> type[TModel]:
|
|
83
|
+
class_path = defining_path(cls)
|
|
84
|
+
|
|
85
|
+
def _display_(self: BaseModel) -> UIElement | mo.Html:
|
|
86
|
+
return render_model(self, class_path)
|
|
87
|
+
|
|
88
|
+
setattr(cls, "_display_", _display_)
|
|
89
|
+
return cls
|
|
90
|
+
|
|
91
|
+
return decorator
|
marimo_utils/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: marimo-utils
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Utilities for working with marimo notebooks, including Pydantic model display
|
|
5
|
+
Project-URL: Homepage, https://github.com/drothermel/marimo_utils
|
|
6
|
+
Project-URL: Repository, https://github.com/drothermel/marimo_utils
|
|
7
|
+
Author-email: Danielle Rothermel <danielle.rothermel@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: display,marimo,notebooks,pydantic
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Requires-Dist: marimo>=0.19.4
|
|
19
|
+
Requires-Dist: pydantic>=2.12.5
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# marimo-utils
|
|
23
|
+
|
|
24
|
+
Utilities for working with marimo notebooks.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install marimo-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### `@add_marimo_display()` decorator
|
|
35
|
+
|
|
36
|
+
Adds a `_display_` method to Pydantic models for rich rendering in marimo notebooks.
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from pydantic import BaseModel
|
|
40
|
+
from marimo_utils import add_marimo_display
|
|
41
|
+
|
|
42
|
+
@add_marimo_display()
|
|
43
|
+
class MyConfig(BaseModel):
|
|
44
|
+
name: str
|
|
45
|
+
value: int
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
When a `MyConfig` instance is the last expression in a marimo cell, it renders with the class name, source file path, and all field values.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
marimo_utils/__init__.py,sha256=rnYqA0S2abZS8J9RyfZ-xB1-7O1fCbUtVJZeJwQF7LE,116
|
|
2
|
+
marimo_utils/display.py,sha256=c6Bzb2fguw2P77coLpK7dGYerirROS5MGkzVDTC-8ko,2550
|
|
3
|
+
marimo_utils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
marimo_utils-0.1.0.dist-info/METADATA,sha256=h5ikSU_rZHhN9dM2C5x-7BCFRMc-naJ6phNwDs55iQI,1430
|
|
5
|
+
marimo_utils-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
marimo_utils-0.1.0.dist-info/licenses/LICENSE,sha256=6tUm1Q55M1UBMbbawzFlF0-DgCazM1BELo_5-RXA1K4,1075
|
|
7
|
+
marimo_utils-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Danielle Rothermel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|