Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Wrapper #2230

Open
dacog opened this issue Nov 22, 2024 · 0 comments
Open

Python Wrapper #2230

dacog opened this issue Nov 22, 2024 · 0 comments

Comments

@dacog
Copy link

dacog commented Nov 22, 2024

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.

Is this useful enough to add to the "Related" section under "Community plugins"?

@cyborg-ts cyborg-ts added this to D2 Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant