Render Mermaid diagrams in your terminal or Python app
Project description
termaid
Render Mermaid diagrams in your terminal or Python app.
Features
- 9 diagram types: flowcharts, sequence, class, ER, state, block, git graphs, pie charts, and treemaps
- Zero dependencies: pure Python, nothing to install beyond the package itself
- Rich and Textual integration: colored output and TUI widgets with optional extras
- 6 color themes: default, terra, neon, mono, amber, phosphor
- ASCII fallback: works on any terminal, even the most basic ones
- Pipe-friendly CLI:
cat diagram.mmd | termaidjust works
Why?
Mermaid is great for documentation, but rendering it usually means spinning up a browser or calling an external service. termaid lets you render diagrams over SSH, in CI logs, inside TUI apps, or anywhere you have a Python environment. It was built because the existing tools in this space, like mermaid-ascii (Go) and beautiful-mermaid (TypeScript), don't offer a native Python library you can import and call directly.
Install
pip install termaid
Or try it without installing:
uvx termaid diagram.mmd
Quick start
CLI
termaid diagram.mmd
echo "graph LR; A-->B-->C" | termaid
termaid diagram.mmd --theme neon
termaid diagram.mmd --ascii
Python
from termaid import render
print(render("graph LR\n A --> B --> C"))
# Colored output (requires: pip install termaid[rich])
from termaid import render_rich
from rich import print as rprint
rprint(render_rich("graph LR\n A --> B", theme="terra"))
# Textual TUI widget (requires: pip install termaid[textual])
from termaid import MermaidWidget
widget = MermaidWidget("graph LR\n A --> B")
Supported diagram types
Flowcharts
All directions supported: LR, RL, TD/TB, BT.
graph TD
A[Start] --> B{Is valid?}
B -->|Yes| C(Process)
C --> D([Done])
B -->|No| E[Error]
┌─────────────┐
│ │
│ Start │
│ │
└──────┬──────┘
│
│
▼
┌──────◇──────┐
│ │
│ Is valid? │
│ │
└──────◇──────┘
│
│
╰──────────────────╮
Yes│ │No
▼ ▼
╭─────────────╮ ┌─────────────┐
│ │ │ │
│ Process │ │ Error │
│ │ │ │
╰──────┬──────╯ └─────────────┘
│
│
▼
╭─────────────╮
( )
( Done )
( )
╰─────────────╯
Node shapes: rectangle [text], rounded (text), diamond {text}, stadium ([text]), subroutine [[text]], circle ((text)), double circle (((text))), hexagon {{text}}, cylinder [(text)], asymmetric >text], parallelogram [/text/], trapezoid [/text\], and @{shape} syntax
Edge styles: solid -->, dotted -.->, thick ==>, bidirectional <-->, circle endpoint --o, cross endpoint --x, labeled -->|text|, variable length --->, ---->
Styling: classDef, style, linkStyle directives, :::className suffix
Subgraphs: nesting, cross-boundary edges, per-subgraph direction override
Other: %% comments, ; line separators, Markdown labels "`**bold** *italic*`", & operator (A & B --> C)
Sequence diagrams
sequenceDiagram
Alice->>Bob: Hello Bob
Bob-->>Alice: Hi Alice
Alice->>Bob: How are you?
Bob-->>Alice: Great!
┌──────────┐ ┌──────────┐
│ Alice │ │ Bob │
└──────────┘ └──────────┘
┆ Hello Bob ┆
──────────────────►
┆ Hi Alice ┆
◄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
┆ How are you? ┆
──────────────────►
┆ Great! ┆
◄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
┆ ┆
Message types: solid arrow ->>, dashed arrow -->>, solid line ->, dashed line -->
Participants: participant, actor, aliases
Class diagrams
classDiagram
class Animal {
+String name
+int age
+makeSound()
}
class Dog {
+String breed
+fetch()
}
Animal <|-- Dog
┌──────────────┐
│ Animal │
├──────────────┤
│ +String name │
│ +int age │
├──────────────┤
│ +makeSound() │
└──────────────┘
△
│
│
│
┌───────────────┐
│ Dog │
├───────────────┤
│ +String breed │
├───────────────┤
│ +fetch() │
└───────────────┘
Relationships: inheritance <|--, composition *--, aggregation o--, association --, dependency ..>, realization ..|>
Members: attributes and methods with visibility (+ public, - private, # protected, ~ package)
ER diagrams
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
┌──────────────┐
│ CUSTOMER │
└──────────────┘
│1
│ places
│
│0..*
┌──────────────┐
│ ORDER │
└──────────────┘
│1
│ contains
│
│1..*
┌──────────────┐
│ LINE-ITEM │
└──────────────┘
Cardinality: || (exactly one), o| (zero or one), }| (one or more), o{ (zero or more)
Line styles: solid --, dashed ..
Attributes: type, name, keys (PK, FK, UK), comments
State diagrams
stateDiagram-v2
[*] --> Idle
Idle --> Processing : start
Processing --> Done : complete
Done --> [*]
╭───────◯──────╮
│ │
│ ● │
│ │
╰───────◯──────╯
│
│
▼
╭──────────────╮
│ │
│ Idle │
│ │
╰───────┬──────╯
│
start│
▼
╭──────────────╮
│ │
│ Processing │
│ │
╰───────┬──────╯
│
complete│
▼
╭──────────────╮
│ │
│ Done │
│ │
╰───────┬──────╯
│
│
▼
╭───────◯──────╮
│ │
│ ◉ │
│ │
╰───────◯──────╯
Features: [*] start/end states, transition labels, state "name" as alias, composite states (state Parent { }), stereotypes (<<choice>>, <<fork>>, <<join>>)
Block diagrams
block-beta
columns 3
A["Frontend"] B["API"] C["Database"]
┌──────────┐ ┌──────────┐ ┌──────────┐
│ │ │ │ │ │
│ Frontend │ │ API │ │ Database │
│ │ │ │ │ │
└──────────┘ └──────────┘ └──────────┘
Features: columns N, column spanning (blockname:N), links between blocks, nested blocks
Git graphs
gitGraph
commit id: "init"
commit id: "feat"
branch develop
commit id: "dev-1"
commit id: "dev-2"
checkout main
commit id: "fix"
merge develop id: "merge"
main ───●─────●──────┼──────────────●──────●─
init feat │ fix merge
│ │
develop ●───────●─────────────┼
dev-1 dev-2
Directions: LR (default), TB, BT
Commands: commit (with id:, type:, tag:), branch (with order:), checkout/switch, merge, cherry-pick
Commit types: NORMAL (●), REVERSE (✖), HIGHLIGHT (■)
Config: %%{init: {"gitGraph": {"mainBranchName": "master"}}}%%
Pie charts
Yes, the syntax says pie. No, we don't draw a circle. I know. Have you ever tried to read a pie chart made of █ and ▓? Exactly. We render them as horizontal bar charts instead.
pie title Pets adopted by volunteers
"Dogs" : 386
"Cats" : 85
"Rats" : 15
Dogs┃████████████████████████████████ 79.4%
Cats┃▓▓▓▓▓▓▓ 17.5%
Rats┃░ 3.1%
Features: title, showData (display raw values), %% comments
Treemaps
treemap-beta
"Frontend"
"React": 40
"CSS": 15
"Backend"
"API": 35
"Auth": 10
┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
┆ Frontend ┆ ┆ Backend ┆
┆┌───────────────────────┐ ┌───────┐┆ ┆┌───────────────────┐ ┌────┐┆
┆│ React │ │ CSS │┆ ┆│ API │ │Auth│┆
┆│ 40 │ │ 15 │┆ ┆│ 35 │ │ 10 │┆
┆│ │ │ │┆ ┆│ │ │ │┆
┆└───────────────────────┘ └───────┘┆ ┆└───────────────────┘ └────┘┆
└┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘ └┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
Features: nested sections via indentation, "label": value syntax, proportional sizing, %% comments
CLI options
| Flag | Description |
|---|---|
--tui |
Interactive TUI viewer (requires pip install termaid[tui]) |
--ascii |
ASCII-only output (no Unicode box-drawing) |
--theme NAME |
Color theme: default, terra, neon, mono, amber, phosphor (requires pip install termaid[rich]) |
--padding-x N |
Horizontal padding inside boxes (default: 4) |
--padding-y N |
Vertical padding inside boxes (default: 2) |
--gap N |
Space between nodes (default: 4). Use 1 or 2 for compact diagrams |
--sharp-edges |
Sharp corners on edge turns instead of rounded |
Python API
render(source, ...) -> str
Render a Mermaid diagram as a plain text string. Auto-detects diagram type.
render_rich(source, ..., theme="default") -> rich.text.Text
Render as a Rich Text object with colors. Requires pip install termaid[rich].
MermaidWidget
A Textual widget with a reactive source attribute. Requires pip install termaid[textual]. Updates live when you change the source property.
from termaid import MermaidWidget
class MyApp(App):
def compose(self):
yield MermaidWidget("graph LR\n A --> B")
Themes
Six built-in themes for --theme / render_rich():
| Theme | Colors | Description |
|---|---|---|
default |
Cyan nodes, yellow arrows, white labels | |
terra |
Warm earth tones (browns, oranges) | |
neon |
Magenta nodes, green arrows, cyan edges | |
mono |
White/gray monochrome | |
amber |
Amber/gold CRT-style | |
phosphor |
Green phosphor terminal-style |
Optional extras
pip install termaid[rich] # Colored terminal output
pip install termaid[textual] # Textual TUI widget
Limitations
- Layout engine is approximate. Node positioning uses a grid-based barycenter heuristic. Graphs with many cross-layer edges may still produce crossings.
- Manhattan-only edge routing. Edges use A* pathfinding on a grid. Very dense graphs may have overlapping edges.
- Wide LR diagrams. Long horizontal chains can exceed terminal width. Use
--gap 1or--padding-x 0for compact output, or pipe throughless -Sfor horizontal scrolling.
Acknowledgements
Inspired by mermaid-ascii by Alexander Grooff and beautiful-mermaid by Lukilabs.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file termaid-0.2.1.tar.gz.
File metadata
- Download URL: termaid-0.2.1.tar.gz
- Upload date:
- Size: 344.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
915a81b1a7eefa989ec4adb549fc56669d3b81bd47316ac026e79b1357464819
|
|
| MD5 |
0b66043dd2b0139ad5ef94ce68cfca39
|
|
| BLAKE2b-256 |
2c5dca4bab62b7e0bc227632bbb056e54d291a97f3269eead1aa7070d1bf0a0a
|
Provenance
The following attestation bundles were made for termaid-0.2.1.tar.gz:
Publisher:
publish.yml on fasouto/termaid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
termaid-0.2.1.tar.gz -
Subject digest:
915a81b1a7eefa989ec4adb549fc56669d3b81bd47316ac026e79b1357464819 - Sigstore transparency entry: 1180293865
- Sigstore integration time:
-
Permalink:
fasouto/termaid@0e3a4708f844b625e9dcc8cd0b33f0dc880f1890 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/fasouto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0e3a4708f844b625e9dcc8cd0b33f0dc880f1890 -
Trigger Event:
release
-
Statement type:
File details
Details for the file termaid-0.2.1-py3-none-any.whl.
File metadata
- Download URL: termaid-0.2.1-py3-none-any.whl
- Upload date:
- Size: 103.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
224f94512bd6a0e59687e0d3a0f5cbe10ce494931abc4dd05b912a71887b1c8a
|
|
| MD5 |
ca23d0ccd967f25be4f065fbb724cdc0
|
|
| BLAKE2b-256 |
4c97f66443865989b030a3e217795880f857afce815811137d00c01669feaf93
|
Provenance
The following attestation bundles were made for termaid-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on fasouto/termaid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
termaid-0.2.1-py3-none-any.whl -
Subject digest:
224f94512bd6a0e59687e0d3a0f5cbe10ce494931abc4dd05b912a71887b1c8a - Sigstore transparency entry: 1180294017
- Sigstore integration time:
-
Permalink:
fasouto/termaid@0e3a4708f844b625e9dcc8cd0b33f0dc880f1890 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/fasouto
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0e3a4708f844b625e9dcc8cd0b33f0dc880f1890 -
Trigger Event:
release
-
Statement type: