Skip to main content

py-d2

Banner

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

Installation

pip install py-d2

Usage

from py_d2 import D2Diagram, D2Shape, D2Connection, D2Style

shapes = [
    D2Shape(name="shape_name1", style=D2Style(fill="red")),
    D2Shape(name="shape_name2", style=D2Style(fill="blue"))]
connections = [
    D2Connection(shape_1="shape_name1", shape_2="shape_name2")
]

diagram = D2Diagram(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
  • SQL table shapes
  • Class shapes
  • Comments

Development

Prerequisite

Installation

following the steps below to setup the project:

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

# Install all dependencies
poetry install --sync --all-extras --with dev,test,coverage

# install git hook scripts for development
pre-commit install

# Install dependencies with group 'dev'、'test' for development
poetry install --with dev,test
# Only install required dependencies for production
poetry install

Usage

There are some useful commands for development:

# Run the example
poetry run example

# Debug with ipdb3
poetry run ipdb3 ./src/py_d2/main.py

# Code test
poetry run pytest -s

# Run default coverage test
poetry run tox

# Run example project coverage test at python 3.9 and 3.10
poetry run tox -e py{39,310}-py-d2

# Lint with black
poetry run black ./src --check

# Format code with black
poetry run black ./src

# Check with mypy
poetry run mypy ./src

# Check import order with isort
poetry run isort ./src --check

# Lint with flake8
poetry run flake8 ./src

Metadata

Release files for py-d2 1.0.1

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

Source distribution (sdist)

Source distribution for py-d2 1.0.1
File Size Uploaded
py_d2-1.0.1.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-d2 1.0.1
File Interpreter ABI Platform
py_d2-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / py_d2-1.0.1.tar.gz

Download URL py_d2-1.0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
36b1a81f9acf3b21923a359439c5ed631768c54eee7c9740ca270bd0ee08874b
BLAKE2b-256 checksum
How to use checksums
9ddcafcf4ac8e1ab9e0556edf30d878c1553a5620c7d2b3e98b0d8be9e8a5bce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.12 Linux/6.5.0-1018-azure

Release files / py_d2-1.0.1-py3-none-any.whl

Download URL py_d2-1.0.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
730a17e1527767b3ade14f48a185d9456a4ced90731d49689c7ed99038400eda
BLAKE2b-256 checksum
How to use checksums
c0bdb579380325bde2968117d836bc772e0dc7e377542d954dc0c4990f1f92e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.12 Linux/6.5.0-1018-azure

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.2

2 release files

0.0.1

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