# py-d2
![Banner](docs/images/banner.png)
An unofficial, fully typed python interface for building [.d2](https://github.com/terrastruct/d2) diagram files in python.
## Installation
```bash
pip install py-d2
```
## Usage
```python
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:
```d2
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/](https://play.d2lang.com/) to produce
![example graph](/docs/images/d2.svg)
See the [tests](/tests/test_py_d2) for more detailed usage examples.
## Supported Features
- [x] Shapes (nodes)
- [x] Connections (links)
- [x] Styles
- [x] Containers (nodes/links in nodes)
- [x] Shapes in shapes
- [x] Arrow directions
- [x] Markdown / block strings / code in shapes
- [ ] Icons in shapes
- [ ] SQL table shapes
- [ ] Class shapes
- [ ] Comments
## Development
### Prerequisite
- [Python 3.7+](https://www.python.org/)
- [Poetry 1.3](https://python-poetry.org/)
- [pre-commit](https://pre-commit.com/)
### Installation
following the steps below to setup the project:
```bash
```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:
```bash
# 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
```
Raw data
{
"_id": null,
"home_page": "https://mrblenny.github.io/py-d2/",
"name": "py-d2",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.7",
"maintainer_email": null,
"keywords": "py-d2, d2-lang, d2-python, diagram-language, terrastruct",
"author": "David Revay",
"author_email": "daverevay@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9d/dc/afcf4ac8e1ab9e0556edf30d878c1553a5620c7d2b3e98b0d8be9e8a5bce/py_d2-1.0.1.tar.gz",
"platform": null,
"description": "# py-d2\n\n![Banner](docs/images/banner.png)\n\nAn unofficial, fully typed python interface for building [.d2](https://github.com/terrastruct/d2) diagram files in python.\n\n## Installation\n\n```bash\npip install py-d2\n```\n\n## Usage\n\n```python\nfrom py_d2 import D2Diagram, D2Shape, D2Connection, D2Style\n\nshapes = [\n D2Shape(name=\"shape_name1\", style=D2Style(fill=\"red\")),\n D2Shape(name=\"shape_name2\", style=D2Style(fill=\"blue\"))]\nconnections = [\n D2Connection(shape_1=\"shape_name1\", shape_2=\"shape_name2\")\n]\n\ndiagram = D2Diagram(shapes=shapes, connections=connections)\n\nwith open(\"graph.d2\", \"w\", encoding=\"utf-8\") as f:\n f.write(str(diagram))\n\n```\n\nproduces the following graph.d2 file:\n\n```d2\n\nshape_name1: {\n style: {\n fill: red\n }\n}\nshape_name2: {\n style: {\n fill: blue\n }\n}\nshape_name1 -> shape_name2\n\n```\n\nThis can be rendered using `d2 graph.d2 graph.svg && open graph.svg` or [https://play.d2lang.com/](https://play.d2lang.com/) to produce\n\n![example graph](/docs/images/d2.svg)\n\nSee the [tests](/tests/test_py_d2) for more detailed usage examples.\n\n\n## Supported Features\n\n- [x] Shapes (nodes)\n- [x] Connections (links)\n- [x] Styles\n- [x] Containers (nodes/links in nodes)\n- [x] Shapes in shapes\n- [x] Arrow directions\n- [x] Markdown / block strings / code in shapes\n- [ ] Icons in shapes\n- [ ] SQL table shapes\n- [ ] Class shapes\n- [ ] Comments\n\n\n## Development\n### Prerequisite\n\n- [Python 3.7+](https://www.python.org/)\n- [Poetry 1.3](https://python-poetry.org/)\n- [pre-commit](https://pre-commit.com/)\n\n### Installation\n\nfollowing the steps below to setup the project:\n\n```bash\n\n```bash\n# Clone the repository\ngit clone git@github.com:MrBlenny/py-d2.git && cd py-d2\n\n# Install all dependencies\npoetry install --sync --all-extras --with dev,test,coverage\n\n# install git hook scripts for development\npre-commit install\n\n# Install dependencies with group 'dev'\u3001'test' for development\npoetry install --with dev,test\n# Only install required dependencies for production\npoetry install\n```\n\n### Usage\n\nThere are some useful commands for development:\n\n```bash\n# Run the example\npoetry run example\n\n# Debug with ipdb3\npoetry run ipdb3 ./src/py_d2/main.py\n\n# Code test\npoetry run pytest -s\n\n# Run default coverage test\npoetry run tox\n\n# Run example project coverage test at python 3.9 and 3.10\npoetry run tox -e py{39,310}-py-d2\n\n# Lint with black\npoetry run black ./src --check\n\n# Format code with black\npoetry run black ./src\n\n# Check with mypy\npoetry run mypy ./src\n\n# Check import order with isort\npoetry run isort ./src --check\n\n# Lint with flake8\npoetry run flake8 ./src\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An unofficial, fully typed python interface for building .d2 graph files in python.",
"version": "1.0.1",
"project_urls": {
"Bug Tracker": "https://github.com/mrblenny/py-d2/issues",
"Documentation": "https://github.com/MrBlenny/py-d2/blob/main/README.md",
"Homepage": "https://mrblenny.github.io/py-d2/",
"Repository": "https://github.com/mrblenny/py-d2"
},
"split_keywords": [
"py-d2",
" d2-lang",
" d2-python",
" diagram-language",
" terrastruct"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c0bdb579380325bde2968117d836bc772e0dc7e377542d954dc0c4990f1f92e9",
"md5": "6de35865fee64680f5daaecfecbb4f78",
"sha256": "730a17e1527767b3ade14f48a185d9456a4ced90731d49689c7ed99038400eda"
},
"downloads": -1,
"filename": "py_d2-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6de35865fee64680f5daaecfecbb4f78",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.7",
"size": 7335,
"upload_time": "2024-05-03T06:25:03",
"upload_time_iso_8601": "2024-05-03T06:25:03.704994Z",
"url": "https://files.pythonhosted.org/packages/c0/bd/b579380325bde2968117d836bc772e0dc7e377542d954dc0c4990f1f92e9/py_d2-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9ddcafcf4ac8e1ab9e0556edf30d878c1553a5620c7d2b3e98b0d8be9e8a5bce",
"md5": "63d7e75b91ab60ab69136cb9233aad57",
"sha256": "36b1a81f9acf3b21923a359439c5ed631768c54eee7c9740ca270bd0ee08874b"
},
"downloads": -1,
"filename": "py_d2-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "63d7e75b91ab60ab69136cb9233aad57",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.7",
"size": 8076,
"upload_time": "2024-05-03T06:25:06",
"upload_time_iso_8601": "2024-05-03T06:25:06.635496Z",
"url": "https://files.pythonhosted.org/packages/9d/dc/afcf4ac8e1ab9e0556edf30d878c1553a5620c7d2b3e98b0d8be9e8a5bce/py_d2-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-03 06:25:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mrblenny",
"github_project": "py-d2",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "py-d2"
}