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

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-1.0.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

d2_python-1.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file d2_python-1.0.0.tar.gz.

File metadata

  • Download URL: d2_python-1.0.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for d2_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4c0e2c22be500ce8afe64a02c02725569b751839223983d3e81502620df353f0
MD5 f51f2d2012581b501e6eb243c3a1c3b1
BLAKE2b-256 c2472c3f7242cf281e68191c0c63d6a50c724e8b4002c85e2cde028991102993

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2_python-1.0.0.tar.gz:

Publisher: publish.yaml on h0rv/d2-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file d2_python-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: d2_python-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for d2_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 024da2fb3649198de4b846210761cf65634c84ad6fa881cbf6631b247b830390
MD5 40dd48e4bce0919c26819ab5d7867c19
BLAKE2b-256 63416d7ab096c8c0454c4b36084b35d7f85b5ac53fb7f30652fd7e455fc204c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2_python-1.0.0-py3-none-any.whl:

Publisher: publish.yaml on h0rv/d2-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Supported by

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