d2-widget 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.
d2_widget/__init__.py ADDED
@@ -0,0 +1,103 @@
1
+ import importlib.metadata
2
+ import pathlib
3
+
4
+ import anywidget
5
+ import traitlets
6
+ from typing import TypedDict, Optional, Literal, Annotated
7
+
8
+ try:
9
+ __version__ = importlib.metadata.version("d2-widget")
10
+ except importlib.metadata.PackageNotFoundError:
11
+ __version__ = "unknown"
12
+
13
+
14
+ class RenderOptions(TypedDict, total=False):
15
+ """Matches RenderOptions from @terrastruct/d2"""
16
+
17
+ sketch: Annotated[
18
+ bool,
19
+ "Enable sketch mode [default: false]",
20
+ ]
21
+ themeID: Annotated[
22
+ int,
23
+ "Theme ID to use [default: 0]",
24
+ ]
25
+ darkThemeID: Annotated[
26
+ int,
27
+ "Theme ID to use when client is in dark mode",
28
+ ]
29
+ center: Annotated[
30
+ bool,
31
+ "Center the SVG in the containing viewbox [default: false]",
32
+ ]
33
+ pad: Annotated[
34
+ int,
35
+ "Pixels padded around the rendered diagram [default: 100]",
36
+ ]
37
+ scale: Annotated[
38
+ float,
39
+ "Scale the output",
40
+ ]
41
+ forceAppendix: Annotated[
42
+ bool,
43
+ "Adds an appendix for tooltips and links [default: false]",
44
+ ]
45
+ target: Annotated[
46
+ str,
47
+ "Target board/s to render",
48
+ ]
49
+ animateInterval: Annotated[
50
+ int,
51
+ "Multiple boards transition interval (in milliseconds)",
52
+ ]
53
+ salt: Annotated[
54
+ str,
55
+ "Add a salt value to ensure the output uses unique IDs",
56
+ ]
57
+ noXMLTag: Annotated[
58
+ bool,
59
+ "Omit XML tag from output SVG files",
60
+ ]
61
+
62
+
63
+ class CompileOptions(RenderOptions, total=False):
64
+ """Matches CompileOptions from @terrastruct/d2"""
65
+
66
+ layout: Annotated[
67
+ Literal["dagre", "elk"],
68
+ "Layout engine to use [default: 'dagre']",
69
+ ]
70
+ fontRegular: Annotated[
71
+ bytes,
72
+ "A byte array containing .ttf file for regular font",
73
+ ]
74
+ fontItalic: Annotated[
75
+ bytes,
76
+ "A byte array containing .ttf file for italic font",
77
+ ]
78
+ fontBold: Annotated[
79
+ bytes,
80
+ "A byte array containing .ttf file for bold font",
81
+ ]
82
+ fontSemibold: Annotated[
83
+ bytes,
84
+ "A byte array containing .ttf file for semibold font",
85
+ ]
86
+
87
+
88
+ class D2Widget(anywidget.AnyWidget):
89
+ _esm = pathlib.Path(__file__).parent / "static" / "widget.js"
90
+ _css = pathlib.Path(__file__).parent / "static" / "widget.css"
91
+
92
+ _svg = traitlets.Unicode().tag(sync=True)
93
+ diagram = traitlets.Unicode().tag(sync=True)
94
+ options = traitlets.Dict({}).tag(sync=True)
95
+
96
+ def __init__(self, diagram: str, options: Optional[CompileOptions] = None):
97
+ super().__init__()
98
+ self.diagram = diagram
99
+ self.options = options or {}
100
+
101
+ @property
102
+ def svg(self) -> str:
103
+ return self._svg
@@ -0,0 +1 @@
1
+ .d2-widget{width:100%}.error{padding:10px;color:#721c24;background-color:#f8d7da;border:1px solid #f5c6cb;border-radius:4px;margin:10px 0;font-family:sans-serif}
@@ -0,0 +1 @@
1
+ import{D2 as c}from"https://esm.sh/@terrastruct/d2@0.1.23-nightly.20250328.0b2203c10";async function d(e,r,o){let t=await e.compile(r,{options:o});return e.render(t.diagram,t.renderOptions)}var u=()=>{let e=new c;return{async render({model:r,el:o}){let t=()=>r.get("diagram"),i=()=>r.get("options"),a=n=>{r.set("_svg",n),r.save_changes()},s=async()=>{try{let n=await d(e,t(),i());a(n),o.innerHTML=n,o.classList.add("d2-widget")}catch(n){console.error(n);let g=n instanceof Error?n.message:"Unknown error";o.innerHTML=`<div class="error">Error generating diagram: ${g}</div>`}};r.on("change:diagram",s),r.on("change:options",s),await s()}}};export{u as default};
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.4
2
+ Name: d2-widget
3
+ Version: 0.0.0
4
+ Summary: AnyWidget wrapper for D2 diagrams
5
+ Project-URL: Homepage, https://github.com/peter-gy/d2-widget
6
+ Project-URL: Repository, https://github.com/peter-gy/d2-widget
7
+ Project-URL: Documentation, https://github.com/peter-gy/d2-widget#readme
8
+ Project-URL: Bug Tracker, https://github.com/peter-gy/d2-widget/issues
9
+ Author-email: Péter Ferenc Gyarmati <dev.petergy@gmail.com>
10
+ License: Copyright © 2025 Péter Ferenc Gyarmati
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
+ License-File: LICENSE
18
+ Keywords: anywidget,d2,diagram,jupyter,widget
19
+ Classifier: Development Status :: 4 - Beta
20
+ Classifier: Framework :: Jupyter
21
+ Classifier: Intended Audience :: Developers
22
+ Classifier: Intended Audience :: Science/Research
23
+ Classifier: License :: OSI Approved :: MIT License
24
+ Classifier: Programming Language :: Python
25
+ Classifier: Programming Language :: Python :: 3
26
+ Classifier: Programming Language :: Python :: 3.8
27
+ Classifier: Programming Language :: Python :: 3.9
28
+ Classifier: Programming Language :: Python :: 3.10
29
+ Classifier: Programming Language :: Python :: 3.11
30
+ Classifier: Programming Language :: Python :: 3.12
31
+ Classifier: Programming Language :: Python :: 3.13
32
+ Requires-Python: >=3.8
33
+ Requires-Dist: anywidget>=0.9
34
+ Requires-Dist: traitlets>=5
35
+ Description-Content-Type: text/markdown
36
+
37
+ # d2-widget
38
+
39
+ ## Installation
40
+
41
+ ```sh
42
+ pip install d2-widget
43
+ ```
44
+
45
+ or with [uv](https://github.com/astral-sh/uv):
46
+
47
+ ```sh
48
+ uv add d2-widget
49
+ ```
50
+
51
+ ## Development
52
+
53
+ We recommend using [uv](https://github.com/astral-sh/uv) for development.
54
+ It will automatically manage virtual environments and dependencies for you.
55
+
56
+ ```sh
57
+ uv run jupyter lab example.ipynb
58
+ ```
59
+
60
+ Alternatively, create and manage your own virtual environment:
61
+
62
+ ```sh
63
+ python -m venv .venv
64
+ source .venv/bin/activate
65
+ pip install -e ".[dev]"
66
+ jupyter lab example.ipynb
67
+ ```
68
+
69
+ The widget front-end code bundles it's JavaScript dependencies. After setting up Python,
70
+ make sure to install these dependencies locally:
71
+
72
+ ```sh
73
+ npm install
74
+ ```
75
+
76
+ While developing, you can run the following in a separate terminal to automatically
77
+ rebuild JavaScript as you make changes:
78
+
79
+ ```sh
80
+ npm run dev
81
+ ```
82
+
83
+ Open `example.ipynb` in JupyterLab, VS Code, or your favorite editor
84
+ to start developing. Changes made in `js/` will be reflected
85
+ in the notebook.
@@ -0,0 +1,7 @@
1
+ d2_widget/__init__.py,sha256=ixnBcm3-8nuupm-vaLHhPI_OZojmVG45YwEtP0dDWLM,2669
2
+ d2_widget/static/widget.css,sha256=YBNDbbpS4kH6s_88dl1aObaVGpdoZ2ZXXcZN4-zJycg,162
3
+ d2_widget/static/widget.js,sha256=HOFSCyiEZ7WQvlVPqGvPmYeK9VBVaWAlK4USH6X0miE,662
4
+ d2_widget-0.0.0.dist-info/METADATA,sha256=qma9PKodaimamgDsVL7m7lUgNM_vRp5t-bALTrinUp4,3340
5
+ d2_widget-0.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ d2_widget-0.0.0.dist-info/licenses/LICENSE,sha256=4Mzdw12DcjbqLSKafAd_3GtpiWDUBYqbIsdnirpsm7o,1072
7
+ d2_widget-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,7 @@
1
+ Copyright © 2025 Péter Ferenc Gyarmati
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.