An unofficial, fully typed python interface for building .d2 graph files in python.
Project description
py-d2
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
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
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
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-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c0e2c22be500ce8afe64a02c02725569b751839223983d3e81502620df353f0
|
|
| MD5 |
f51f2d2012581b501e6eb243c3a1c3b1
|
|
| BLAKE2b-256 |
c2472c3f7242cf281e68191c0c63d6a50c724e8b4002c85e2cde028991102993
|
Provenance
The following attestation bundles were made for d2_python-1.0.0.tar.gz:
Publisher:
publish.yaml on h0rv/d2-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
d2_python-1.0.0.tar.gz -
Subject digest:
4c0e2c22be500ce8afe64a02c02725569b751839223983d3e81502620df353f0 - Sigstore transparency entry: 272646205
- Sigstore integration time:
-
Permalink:
h0rv/d2-python@0d65374ae7240786e0efdb6f0efea0105b268831 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/h0rv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@0d65374ae7240786e0efdb6f0efea0105b268831 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
024da2fb3649198de4b846210761cf65634c84ad6fa881cbf6631b247b830390
|
|
| MD5 |
40dd48e4bce0919c26819ab5d7867c19
|
|
| BLAKE2b-256 |
63416d7ab096c8c0454c4b36084b35d7f85b5ac53fb7f30652fd7e455fc204c5
|
Provenance
The following attestation bundles were made for d2_python-1.0.0-py3-none-any.whl:
Publisher:
publish.yaml on h0rv/d2-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
d2_python-1.0.0-py3-none-any.whl -
Subject digest:
024da2fb3649198de4b846210761cf65634c84ad6fa881cbf6631b247b830390 - Sigstore transparency entry: 272646206
- Sigstore integration time:
-
Permalink:
h0rv/d2-python@0d65374ae7240786e0efdb6f0efea0105b268831 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/h0rv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@0d65374ae7240786e0efdb6f0efea0105b268831 -
Trigger Event:
release
-
Statement type: