Skip to content

Commit d593e09

Browse files
committed
add missing options from the d2 bin. Update Readme, add CHANGELOG.md, increase package version.
1 parent e21ae08 commit d593e09

5 files changed

Lines changed: 158 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project tries to adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.2.0] - 2024-11-22
9+
10+
### Added
11+
12+
- Dark theme support with `dark_theme` parameter
13+
- Sketch mode rendering with `sketch` parameter
14+
- SVG centering with `center` parameter
15+
- Output scaling with `scale` parameter
16+
- SVG asset bundling control with `bundle` parameter
17+
- Tooltip appendix forcing with `force_appendix` parameter
18+
- Execution timeout control with `timeout` parameter
19+
- Board animation with `animate_interval` parameter
20+
- Target board specification with `target` parameter
21+
- Custom font support:
22+
- Regular font with `font_regular`
23+
- Italic font with `font_italic`
24+
- Bold font with `font_bold`
25+
- Semibold font with `font_semibold`
26+
27+
### Changed
28+
29+
- Improved type hints using `Union` and `Literal` types
30+
31+
### Fixed
32+
33+
-
34+
35+
## [0.1.0] - Initial Release
36+
37+
### Added
38+
39+
- Basic D2 diagram rendering support
40+
- Multiple output formats (svg, png, pdf)
41+
- Theme customization
42+
- Layout engine options
43+
- Platform detection and binary management

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ pytest test_d2.py -v
3737

3838
- Automatic platform detection and binary management
3939
- Support for multiple output formats (svg, png, pdf)
40-
- Theme customization
41-
- Layout engine options
40+
- Theme customization with dark mode support
41+
- Layout engine options (dagre, elk)
42+
- Hand-drawn sketch mode
43+
- SVG enhancements:
44+
- Asset bundling
45+
- Center alignment
46+
- Custom scaling
47+
- Animation support
4248

4349
## Local Development Setup
4450

@@ -76,16 +82,33 @@ d2.render("test.d2", "output.svg")
7682

7783
## API Reference
7884

85+
## API Reference
86+
7987
### D2 Class
8088

81-
#### render(input_file, output_file, **options)
82-
- `input_file`: Path to D2 source file
89+
#### render(input_source, output_file, **options)
90+
91+
- `input_source`: Path to D2 source file or string content
8392
- `output_file`: Path for output file
8493
- Options:
8594
- `format`: Output format ('svg', 'png', 'pdf'). Default: 'svg'
86-
- `theme`: Theme name ('dark', 'light', etc.). Default: system theme
95+
- `theme`: Theme ID (0-11) or name. Default: system theme
96+
- `dark_theme`: Theme ID for dark mode. Default: None (uses same as light)
8797
- `layout`: Layout engine ('dagre', 'elk'). Default: 'dagre'
8898
- `pad`: Padding in pixels. Default: 100
99+
- `sketch`: Render in hand-drawn style. Default: False
100+
- `center`: Center SVG in viewbox. Default: False
101+
- `scale`: Output scaling (-1 for auto). Default: -1
102+
- `bundle`: Bundle SVG assets. Default: True
103+
- `force_appendix`: Force tooltip appendix. Default: False
104+
- `timeout`: Execution timeout in seconds. Default: 120
105+
- `animate_interval`: Animation interval in ms. Default: 0
106+
- `target`: Target board to render. Default: "*"
107+
- Font customization:
108+
- `font_regular`: Path to regular .ttf
109+
- `font_italic`: Path to italic .ttf
110+
- `font_bold`: Path to bold .ttf
111+
- `font_semibold`: Path to semibold .ttf
89112

90113
## License
91114

d2_python/__init__.py

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
import platform
33
import subprocess
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import Optional, Union
6+
try:
7+
from typing import Literal
8+
except ImportError:
9+
from typing_extensions import Literal
10+
611
import tempfile
712

813

914
class D2:
15+
"""
16+
Python wrapper for the D2 diagram scripting language.
17+
Supports multiple output formats, themes, and layout options.
18+
"""
19+
1020
def __init__(self):
1121
system = platform.system().lower()
1222
if system == "linux":
@@ -31,29 +41,91 @@ def __init__(self):
3141
def render(self,
3242
input_source: str,
3343
output_file: str,
34-
format: str = "svg",
35-
theme: Optional[str] = None,
36-
layout: str = "dagre",
37-
pad: int = 100) -> None:
44+
format: Literal["svg", "png", "pdf"] = "svg",
45+
theme: Optional[Union[int, str]] = None,
46+
layout: Literal["dagre", "elk"] = "dagre",
47+
pad: int = 100,
48+
dark_theme: Optional[int] = None,
49+
sketch: bool = False,
50+
center: bool = False,
51+
scale: float = -1,
52+
bundle: bool = True,
53+
force_appendix: bool = False,
54+
timeout: int = 120,
55+
animate_interval: int = 0,
56+
target: str = "*",
57+
font_regular: Optional[str] = None,
58+
font_italic: Optional[str] = None,
59+
font_bold: Optional[str] = None,
60+
font_semibold: Optional[str] = None) -> None:
3861
"""
3962
Render D2 diagram to specified format.
4063
4164
Args:
4265
input_source: Input D2 file path or string content
4366
output_file: Output file path
4467
format: Output format (svg, png, pdf)
45-
theme: Theme name (dark, light)
68+
theme: Theme ID (0-11) or name. See https://github.com/terrastruct/d2/tree/master/d2themes
69+
dark_theme: Theme ID to use when in dark mode. -1 uses the same theme as light mode
4670
layout: Layout engine (dagre, elk)
4771
pad: Padding in pixels
72+
sketch: Render the diagram to look like it was sketched by hand
73+
center: Center the SVG in the containing viewbox
74+
scale: Scale the output. -1 means SVGs fit to screen, others use default size
75+
bundle: When outputting SVG, bundle all assets and layers into the output file
76+
force_appendix: Force addition of appendix for tooltips and links in SVG exports
77+
timeout: Maximum number of seconds D2 runs before timing out
78+
animate_interval: Milliseconds between transitions when using multiple boards (SVG only)
79+
target: Target board to render. Use '*' for all scenarios, '' for root only
80+
font_regular: Path to .ttf file for regular font
81+
font_italic: Path to .ttf file for italic font
82+
font_bold: Path to .ttf file for bold font
83+
font_semibold: Path to .ttf file for semibold font
4884
"""
4985
cmd = [self.binary_path]
5086

51-
if theme:
52-
cmd.extend(["--theme", theme])
87+
if theme is not None:
88+
cmd.extend(["--theme", str(theme)])
89+
90+
if dark_theme is not None:
91+
cmd.extend(["--dark-theme", str(dark_theme)])
5392

5493
cmd.extend(["--layout", layout])
5594
cmd.extend(["--pad", str(pad)])
5695

96+
if sketch:
97+
cmd.append("--sketch")
98+
99+
if center:
100+
cmd.append("--center")
101+
102+
if scale != -1:
103+
cmd.extend(["--scale", str(scale)])
104+
105+
if not bundle:
106+
cmd.extend(["--bundle", "false"])
107+
108+
if force_appendix:
109+
cmd.append("--force-appendix")
110+
111+
if timeout != 120:
112+
cmd.extend(["--timeout", str(timeout)])
113+
114+
if animate_interval > 0:
115+
cmd.extend(["--animate-interval", str(animate_interval)])
116+
117+
if target != "*":
118+
cmd.extend(["--target", target])
119+
120+
for font_type, font_path in [
121+
("regular", font_regular),
122+
("italic", font_italic),
123+
("bold", font_bold),
124+
("semibold", font_semibold)
125+
]:
126+
if font_path:
127+
cmd.extend([f"--font-{font_type}", str(font_path)])
128+
57129
if format != "svg":
58130
cmd.extend(["--format", format])
59131

pyproject.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "d2-python-wrapper"
7-
version = "0.1.0"
7+
version = "0.2.0"
88
description = "Python wrapper for D2 diagram tool"
99
authors = [{ name = "Diego Carrasco G." }]
1010
readme = "README.md"
@@ -14,4 +14,8 @@ requires-python = ">=3.7"
1414
include = ["d2_python*"]
1515

1616
[tool.setuptools.package-data]
17-
d2_python = ["bin/linux/*", "bin/win32/*", "bin/darwin/*"]
17+
d2_python = ["bin/linux/*", "bin/win32/*", "bin/darwin/*"]
18+
19+
[tool.d2]
20+
# Track D2 binary version separately
21+
binary_version = "v0.6.8"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="d2-python",
5-
version="0.1.0",
5+
version="0.2.0",
66
packages=find_packages(),
77
package_data={
88
"d2_python": [

0 commit comments

Comments
 (0)