Skip to main content

An AnyWidget for displaying declarative diagrams written in D2

Project description

PyPI

D2 Widget d2-widget logo

Bring the power of D2 to Python notebooks.

d2-widget is an AnyWidget for displaying declarative diagrams written in D2.

Installation

pip install d2-widget

or with uv:

uv add d2-widget

Usage

The following examples demonstrate how to use Widget with increasing complexity.

Basic Usage

The simplest way to use Widget is to pass a D2 diagram as a string to the constructor.

from d2_widget import Widget

Widget("x -> y")
simple example

Inline Configuration

You can add direction and layout settings directly in the D2 markup.

from d2_widget import Widget

Widget("""
direction: right
x -> y
""")
simple example with inline configuration

Compile Options

You can specify compile options using the second argument to the constructor. You can read about the semantics of the options in the D2 documentation.

from d2_widget import Widget

Widget("""
direction: right
x -> y
""",
  {
      "themeID": 200,  # ID of the "Dark mauve" theme
      "pad": 0,        # Disable padding
      "sketch": True,  # Enable sketch mode
  },
)
example with compile options

Accessing the SVG

You can access the generated SVG using the svg attribute.

from d2_widget import Widget

w = Widget("x -> y")
w.svg

%%d2 Cell Magic

You can use the %%d2 cell magic to display a D2 diagram in a Jupyter notebook.

First, you need to load the extension:

%load_ext d2_widget

Then, you can use the %%d2 cell magic to display a D2 diagram. You can pass compile options to the cell magic using keyword arguments.

%%d2 sketch=True themeID=200
direction: right
x -> y
y -> z { style.animated: true }
z -> x
example with cell magic

Development

We recommend using uv for development. It will automatically manage virtual environments and dependencies for you.

uv run jupyter lab example.ipynb

Alternatively, create and manage your own virtual environment:

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
jupyter lab example.ipynb

The widget front-end code bundles it's JavaScript dependencies. After setting up Python, make sure to install these dependencies locally:

pnpm install

While developing, you can run the following in a separate terminal to automatically rebuild JavaScript as you make changes:

pnpm dev

Open example.ipynb in JupyterLab, VS Code, or your favorite editor to start developing. Changes made in js/ will be reflected in the notebook.

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

d2_widget-0.0.1rc2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

d2_widget-0.0.1rc2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file d2_widget-0.0.1rc2.tar.gz.

File metadata

  • Download URL: d2_widget-0.0.1rc2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for d2_widget-0.0.1rc2.tar.gz
Algorithm Hash digest
SHA256 a1fd6268e4dc008f29a304f9aaa9f24d61d88b1ebe72432e07e0361ffe696ab4
MD5 cf63491bf6470d93a4c0f5f0d92ffeb0
BLAKE2b-256 c629bb101e30776f70ec564aed4ca048ca5dbf11eea80322040599c8a3ed7f6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2_widget-0.0.1rc2.tar.gz:

Publisher: python-publish.yml on peter-gy/d2-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file d2_widget-0.0.1rc2-py3-none-any.whl.

File metadata

  • Download URL: d2_widget-0.0.1rc2-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

Hashes for d2_widget-0.0.1rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 7866d11d0f8b0163ece18fcf672d2920907b623d695e3a6f2bfb854059eb727e
MD5 651c5c01370d357a68fe38b68566de47
BLAKE2b-256 671a3c3dd3a58f50224ec8a3ee794bec5927e538050a38815716f526ec62cafb

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2_widget-0.0.1rc2-py3-none-any.whl:

Publisher: python-publish.yml on peter-gy/d2-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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