Python wrapper for D2 diagram tool
Project description
D2 Python Wrapper
A Python wrapper for the D2 diagram scripting language.
Installation
pip install d2-python-wrapper
Quick Start
from d2_python import D2
d2 = D2()
# Simple diagram
with open("test.d2", "w") as f:
f.write("x -> y")
# Default SVG output
d2.render("test.d2", "output.svg")
# PDF output with specific theme
d2.render("test.d2", "output.pdf", format="pdf", theme="dark")
Tests
pip install pytest pytest-mock
pytest test_d2.py -v
Features
- Automatic platform detection and binary management
- Support for multiple output formats (svg, png, pdf)
- Theme customization with dark mode support
- Layout engine options (dagre, elk)
- Hand-drawn sketch mode
- SVG enhancements:
- Asset bundling
- Center alignment
- Custom scaling
- Animation support
Local Development Setup
- Create virtual environment:
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
- Get D2 locally:
curl -L "https://github.com/terrastruct/d2/releases/download/v0.6.8/d2-v0.6.8-linux-amd64.tar.gz" -o linux.tar.gz
tar xzf linux.tar.gz
mkdir -p d2_python/bin/linux
cp d2-v0.6.8/bin/d2 d2_python/bin/linux/d2-bin
chmod +x d2_python/bin/linux/d2-bin
- Install in development mode:
pip install -e .
- Render a diagram
echo "hello -> world" > test.d2
from d2_python import D2
d2 = D2()
d2.render("test.d2", "output.svg")
API Reference
API Reference
D2 Class
render(input_source, output_file, **options)
input_source: Path to D2 source file or string contentoutput_file: Path for output file- Options:
format: Output format ('svg', 'png', 'pdf'). Default: 'svg'theme: Theme ID (0-11) or name. Default: system themedark_theme: Theme ID for dark mode. Default: None (uses same as light)layout: Layout engine ('dagre', 'elk'). Default: 'dagre'pad: Padding in pixels. Default: 100sketch: Render in hand-drawn style. Default: Falsecenter: Center SVG in viewbox. Default: Falsescale: Output scaling (-1 for auto). Default: -1bundle: Bundle SVG assets. Default: Trueforce_appendix: Force tooltip appendix. Default: Falsetimeout: Execution timeout in seconds. Default: 120animate_interval: Animation interval in ms. Default: 0target: Target board to render. Default: "*"- Font customization:
font_regular: Path to regular .ttffont_italic: Path to italic .ttffont_bold: Path to bold .ttffont_semibold: Path to semibold .ttf
License
Mozilla Public License Version 2.0 (same as https://github.com/terrastruct/d2)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
d2_python_wrapper-0.2.0.tar.gz
(61.1 MB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file d2_python_wrapper-0.2.0.tar.gz.
File metadata
- Download URL: d2_python_wrapper-0.2.0.tar.gz
- Upload date:
- Size: 61.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39e3f48db663b50f44cb43d96ecc9368346572c77685b471048a1109ad6bf176
|
|
| MD5 |
ae03242907e0ddf5a7fb8726612745b5
|
|
| BLAKE2b-256 |
7f34527e57e70e63cd8095e98ce09075c0c5cc50ac2df39c6d9e7a0f6ed02e3b
|
File details
Details for the file d2_python_wrapper-0.2.0-py3-none-any.whl.
File metadata
- Download URL: d2_python_wrapper-0.2.0-py3-none-any.whl
- Upload date:
- Size: 61.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20ac6f97fbeee72957bf7b557e91ac19f41f9179f348f89fbc19fe7e74fe710e
|
|
| MD5 |
be6a4dc1e2f3f84cf6b71e0e2106ea6a
|
|
| BLAKE2b-256 |
cc628dc5061e4130f0d674fc513b5ea303770fd294bac08767f6b59bf4063353
|