Excellent declarative language good for creating Cloud & Architecture diagrams.
- Key Points
- Real World Architecture Diagrams
- D2 Template
- Tutorial
- Interactive Online Sandbox
- Install
- Local Development
- Themes
- Layouts
- Issues
- Libraries
- less code than Python diagrams
- CLI watch mode - interactive local web server auto-updates from code file for fast interactive development
- output formats - SVG, PNG, PDF
- good for Cloud Architecture diagrams with icons
- multiple layout engines - use
--layout dagre
or--layout elk
- nice dark theme
HariSekhon/Diagrams-as-Code - ready made architecture diagrams
See thse D2 Code files.
HariSekhon/Templates - diagram.d2
https://d2lang.com/tour/hello-world
In DevOps-Bash-tools install/
directory:
install_d2.sh
Auto-format the .d2 file
:
d2 fmt "$filename.d2"
Every compile auto-formats your $filename.d2
file so you don't even need to run fmt
manually.
d2 input.d2 output.svg
Gives a hand drawn appearance:
d2 --sketch ...
Launch local webserver - auto-updates page upon write to diagram.d2
:
d2 --watch --theme 200 "$templates/diagram.d2" /tmp/template.png
d2 --watch --dark-theme 200 "$templates/diagram.d2" /tmp/template.svg
Interactive development sandbox - doesn't export png files right though, see github issue.
d2 -w -t 200 ...
Bug - animate-interval
requires something in the base to work, see:
d2 --animate-interval=1200 ...
https://d2lang.com/tour/themes
Show list of available themes:
d2 themes
Set theme for both light and dark mode
d2 --theme <number>
Set only when when user's browser is set in dark mode - only works for SVGs, not PNGs:
d2 --dark-theme
200 is Dark Mauve
color theme - my favourite
Define in environment variables:
export D2_THEME=200
export D2_DARK_THEME=200
https://d2lang.com/tour/layouts
show layout engines
d2 layout
Some things I'd like to see improved:
https://terrastruct.com/blog/post/generate-diagrams-programmatically/
Ported from private Knowledge Base page 2023+