Skip to main content

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
  • Layout engine options

Local Development Setup

  1. Create virtual environment:
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows
  1. 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
  1. Install in development mode:
pip install -e .
  1. Render a diagram
echo "hello -> world" > test.d2
from d2_python import D2
d2 = D2()
d2.render("test.d2", "output.svg") 

API Reference

D2 Class

render(input_file, output_file, **options)

  • input_file: Path to D2 source file
  • output_file: Path for output file
  • Options:
    • format: Output format ('svg', 'png', 'pdf'). Default: 'svg'
    • theme: Theme name ('dark', 'light', etc.). Default: system theme
    • layout: Layout engine ('dagre', 'elk'). Default: 'dagre'
    • pad: Padding in pixels. Default: 100

License

Mozilla Public License Version 2.0 (same as https://github.com/terrastruct/d2)

Project details


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.1.0.tar.gz (20.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

d2_python_wrapper-0.1.0-py3-none-any.whl (20.4 MB view details)

Uploaded Python 3

File details

Details for the file d2_python_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: d2_python_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 20.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for d2_python_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 49f034dfd9fca91b1a7a8d4bc7107a330d7ca9ae9181d6b36cc4f95fdf93bf70
MD5 c4fd29dcfca8d736dc921fc2a735d506
BLAKE2b-256 388be43f44f367d3e661598e4334278f5d2be2d4b69ac2f126add974d3554a44

See more details on using hashes here.

File details

Details for the file d2_python_wrapper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for d2_python_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f61cbda3a91b614cf0a61cb79f29fc5b270576c752a1ca34fbab3b5608b61d2
MD5 919b5402608c4679cf15508bc1eaa87f
BLAKE2b-256 0ffa1469476058bcf8283ca66585458187be578f567f790e8d5d4d780c41b8a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page