Skip to main content

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

Project description

py-d2

Banner

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

Installation

pip install py-d2

Usage

from py_d2 import D2Graph, D2Node, D2Connection, D2Style

nodes = [
    D2Node(name="node_name1", style=D2Style(fill="red")),
    D2Node(name="node_name2", style=D2Style(fill="blue"))]
links = [
    D2Connection(from_node="node_name1", to_node="node_name2")
]

diagram = D2Diagram(nodes=nodes, links=links)

with open("graph.d2", "w") as f:
    f.write(str(diagram))

produces the following graph.d2 file:

node_name1: {
  style: {
    fill: red
  }
}
node_name2: {
  style: {
    fill: blue
  }
}
node_name1 -> node_name2

This can be rendered using d2 render graph.d2 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

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

py_d2-0.0.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

py_d2-0.0.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file py_d2-0.0.2.tar.gz.

File metadata

  • Download URL: py_d2-0.0.2.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for py_d2-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d2ac0a79f2b7b32f94c80da153c32e5d85a65020136811dd554a7dcfc63a7f1d
MD5 670788ffab8d9b0c8d2fac46abc3d5a2
BLAKE2b-256 b15c56abe5332b6ca762c887f478fcfef7291056e1887f97240ab2aff613afd1

See more details on using hashes here.

Provenance

File details

Details for the file py_d2-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: py_d2-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for py_d2-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5e7b43f47e2760c183066134c92d2b90e95c9c0f900dc025d31bd9dc2ffb371
MD5 88f8094b4201165c0907088e9752dd36
BLAKE2b-256 fe56fe8765a3aeab225bc19f1cc70a28ca08b71bbfcdd6f2d2310889a8633e15

See more details on using hashes here.

Provenance

Supported by

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