Hi,

I wanted to use d2 in an environment where I could only install python and npm packages.

Given that, and that I could not find any other solution, I made d2-python-wrapper, a small python wrapper that bundles the d2 binaries.

Now you can use d2 from python like this:

from d2_python import D2

d2 = D2()

# Simple diagram
with open("test.d2", "w") as f:
    f.write("x -> y")

# Default SVG output
d2.render("test.d2", "output.svg")

# PDF output with specific theme
d2.render("test.d2", "output.pdf", format="pdf", theme="1")

The class just wraps the bin, so it works and supports the same as the bin. There is a GitHub Action that gets the bins for each platform (mac, win, linux) from the releases in this repo and publishes it to pip.

You can install this using

pip install d2-python-wrapper

Here is a short post with more context.

Just in case It’s useful for anyone. 😁