princemaple
DG - Elixir wrapper of :digraph with a pinch of protocols and sigils
Hi All,
After seeing the mermaid.js integration in livebook, I had a little idea to create a library on top of :digraph that bridges between a :digraph graph and a mermaid.js flowchart.
https://github.com/princemaple/dg
Highlights
Inspect
dg = DG.new()
DG.add_vertex(dg, "v1")
DG.add_vertex(dg, "v2", "label 2")
DG.add_vertex(dg, "v3")
DG.add_edge(dg, "v1", "v2")
DG.add_edge(dg, "v1", "v3", "1 to 3")
IO.inspect(dg)
Outputs mermaid.js format flowchart
graph LR
v1-->v2[label 2]
v1--1 to 3-->v3
which can be put into livebook mermaid block and will display as

Collectable
Easily add
- vertices
[{:vertex, 1}, {:vertex, 2, "label 2"}, ...] - or edges
[{:edge, 1, 2}, {:edge, "a", "b", "label ab"}, ...]
to the graph via Enum.into/2
dg = DG.new()
~w(a b c d e) |> Enum.map(&{:vertex, &1}) |> Enum.into(dg)
~w(a b c d e)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [f, t] -> {:edge, f, t} end)
|> Enum.into(dg)
IO.inspect(dg)
outputs
graph LR
b-->c
c-->d
a-->b
d-->e

Functions
- Most functions from
:digraphand:digraph_utilsare mirrored underDG. - Some functions have their parameters re-arranged so that the graph is always the first parameter.
(namelyreachable/2,reachable_neighbours/2,reaching/2andreaching_neighbours/2) new/{0,1,2,3}returns%DG{}instead of an Erlangdigraphnew/2andnew/3are shortcuts that can add vertices and edges on creation,
which don’t exist on:digraph
Sigils
~G and ~g are provided so you can copy mermaid.js flowchart and paste into Elixir to get a %DG{}
import DG.Sigil
dg = ~G"""
graph LR
a[some label]
b[other label]
1-->2
3[three] -- three to four --> 4[four]
a --> b
"""
caveat: :digraph is stateful (using ets), don’t use the sigil at compile time,
e.g. as a module attribute, it won’t carry over to runtime.
Only use it in runtime code, e.g. function body,
and remember to clean up properly when it’s no longer used, with delete/1.
Most Liked
princemaple
Edit: added link to repo… How did I miss the most important thing ![]()
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









