Skip to main content

py-d2

Banner

An unofficial, fully typed python interface for building .d2 diagram files in python.

Installation

pip install d2-python

Usage

import d2

shapes = [
    d2.Shape(name="shape_name1", style=d2.Style(fill="red")),
    d2.Shape(name="shape_name2", style=d2.Style(fill="blue"))]
connections = [
    d2.Connection(shape_1="shape_name1", shape_2="shape_name2")
]

diagram = d2.Diagram(shapes=shapes, connections=connections)

with open("graph.d2", "w", encoding="utf-8") as f:
    f.write(str(diagram))

produces the following graph.d2 file:

shape_name1: {
  style: {
    fill: red
  }
}
shape_name2: {
  style: {
    fill: blue
  }
}
shape_name1 -> shape_name2

This can be rendered using d2 graph.d2 graph.svg && open graph.svg or https://play.d2lang.com/ to produce

example graph

See the tests for more detailed usage examples.

Supported Features

  • Shapes (nodes)
  • Connections (links)
  • Styles
  • Containers (nodes/links in nodes)
  • Shapes in shapes
  • Arrow directions
  • Markdown / block strings / code in shapes
  • Icons in shapes
  • Support for empty labels
  • Shape links
  • SQL table shapes
  • Layers
  • Class shapes
  • Comments

Examples

examples/

uv run python examples/<example>.py

SQL Table:

uv run python example/simple_sql_schema.py
# Open diagram:
open simple_sql_schema.svg

Development

Prerequisite

Installation

following the steps below to setup the project:

```bash
# Clone the repository
git clone git@github.com:h0rv/d2-python.git && cd d2-python

# Install all dependencies
uv sync --all-extras --dev

# install git hook scripts for development
pre-commit install

# Install dev dependencies for development
uv sync --dev
# Only install required dependencies for production
uv sync

Usage

There are some useful commands for development:

# Run the example
uv run example

# Code test
uv run pytest -s

# Lint and format with ruff
uv run ruff check ./src

# Format code with ruff
uv run ruff format ./src

# Check with mypy
uv run mypy ./src

# Run coverage test
uv run pytest -s --cov=./src --cov-report=term-missing

Metadata

Release files for d2-python 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for d2-python 1.0.0
File Size Uploaded
d2_python-1.0.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for d2-python 1.0.0
File Interpreter ABI Platform
d2_python-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / d2_python-1.0.0.tar.gz

Download URL d2_python-1.0.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4c0e2c22be500ce8afe64a02c02725569b751839223983d3e81502620df353f0
BLAKE2b-256 checksum
How to use checksums
c2472c3f7242cf281e68191c0c63d6a50c724e8b4002c85e2cde028991102993
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 12, 2025.

Transparency log

Release files / d2_python-1.0.0-py3-none-any.whl

Download URL d2_python-1.0.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
024da2fb3649198de4b846210761cf65634c84ad6fa881cbf6631b247b830390
BLAKE2b-256 checksum
How to use checksums
63416d7ab096c8c0454c4b36084b35d7f85b5ac53fb7f30652fd7e455fc204c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 12, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page