princemaple

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

livebook screenshot

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

livebook screenshot

Functions

  • Most functions from :digraph and :digraph_utils are mirrored under DG.
  • Some functions have their parameters re-arranged so that the graph is always the first parameter.
    (namely reachable/2, reachable_neighbours/2, reaching/2 and reaching_neighbours/2)
  • new/{0,1,2,3} returns %DG{} instead of an Erlang digraph
  • new/2 and new/3 are 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

princemaple

Edit: added link to repo… How did I miss the most important thing :rofl:

Where Next?

Popular in Announcing Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
mathieuprog
Hello :waving_hand: Allow me to introduce you to Tz, an alternative time zone database support to Tzdata. Why another library? First a...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement