# Python CLI & API

> Generate self-contained offline interactive HTML diagrams and convert Mermaid files from Python using aidiagram.

# Python CLI & API (`aidiagram`)

The `aidiagram` PyPI package bundles the versioned browser engine inside the wheel (`importlib.resources`) so exported HTML pages work completely offline with zero network calls.

## Step 1: Install

```bash
python -m pip install aidiagram
```

*(Optional YAML formatting for `aidiagram convert`: `python -m pip install "aidiagram[yaml]"`.)*

## Step 2: Export Offline Interactive HTML

```bash
aidiagram html workflow.mmd --out workflow.html
aidiagram html workflow.mmd --out workflow.html --theme light --accent indigo
```

| Flag | Default | Options |
|---|---|---|
| `--out` | `<spec>.html` | Output HTML path |
| `--theme` | `dark` | `light`, `dark`, `auto` |
| `--accent` | `amber` | `amber`, `indigo`, `teal`, `rose` |

Every exported HTML file embeds:
- The versioned `<praisonai-diagram>` runtime (zero external CDN requests, works via `file://`).
- The verbatim original Mermaid/YAML source.
- An accessible, collapsible Apache-2.0 license and `NOTICE` footer.

## Step 3: Convert Mermaid to Structured Spec

```bash
aidiagram convert workflow.mmd --out workflow.json --format json
aidiagram convert workflow.mmd --out workflow.yaml --format yaml
```

If the Mermaid input uses unsupported syntax or an opt-out comment (`%% diagram: off`), `aidiagram convert` exits cleanly with a clear error rather than writing a partial diagram.

## Step 4: Inspect Runtime Version & Digest

```bash
aidiagram info
```

Returns JSON with the package version, bundled browser runtime version, SHA-256 digest, and `Apache-2.0` license metadata.

## Python API

```python
from aidiagram import (
    mermaid_to_diagram,
    render_interactive_html,
    runtime_info,
    write_interactive_html,
)

# 1. Convert supported Mermaid -> Python dict (or None if unsupported)
spec = mermaid_to_diagram("flowchart LR\n  A <--> B\n")

# 2. Export a self-contained offline HTML file
write_interactive_html("workflow.mmd", "workflow.html", theme="dark", accent="teal")

# 3. Render HTML string in memory
html_str = render_interactive_html("flowchart LR\n  A --> B\n", title="My Flow")

# 4. Inspect bundled runtime digest
print(runtime_info())
```

## Related pages

- [Quick Start](https://diagram.praison.ai/docs/getting-started/quickstart.md)
- [Web Component & Plugins](https://diagram.praison.ai/docs/guides/web-component.md)
- [PraisonAIUI Integration](https://diagram.praison.ai/docs/guides/praisonaiui.md)
