bmitc

bmitc

Way for Elixir code cell in Livebook to emit a Mermaid graph which gets rendered?

Hello! I have some code:

:sentence
|> RuleBasedGrammar.generate_tree(RuleBasedGrammar.bigger_grammar())

that returns something like:

[
  :sentence,
  [:noun_phrase, [:pronoun, "it"]],
  [:verb_phrase, [:verb, "took"], [:noun_phrase, [:name, "Lee"]], []]
]

I would now like to take this output and render it, via a function call, as a graph. Is there a way to have a function emit Mermaid markdown and then get that rendered somehow?

For example, I would have a function RuleBasedGrammar.render_as_mermad/1 that would internally take the above output, convert it to the Mermaid markdown:

graph TD

:sentence --- :noun_phrase
:noun_phrase --- :pronoun
:pronoun --- it["#quot;it#quot;"]

:sentence --- :verb_phrase
:verb_phrase --- :verb
:verb --- took["#quot;took#quot;"]
:verb_phrase --- :name
:name --- lee["#quot;Lee#quot;"]

and then the Mermaid would render to the diagram in the output cell.

Here’s a mockup:

I’m sure there is a way to do this using Kino, but I’m just not sure how at the moment. So I thought I would ask here to check:

  1. Is there already an existing solution for this?
  2. If not, does anyone have some pointers on how to get Kino to do this?

Thank you!

Marked As Solved

w0rd-driven

w0rd-driven

Kino.Markdown.new() is likely what you want. It emits new markdown as a control but is not like a markdown cell. I assume you care about the output and not editing the markdown, correct? If that’s the case, Kino.Markdown is perfect. If you want to edit output around it you could always just use a separate markdown cell. It’d be clunky to mix them.

The only caveat is I don’t know if Mermaid works that way exactly. I remember there being some gotchas but memory is hazy.

The added bonus of using Kino is that’s how apps display output.

Also Liked

bmitc

bmitc

Good call! You caused me to realize that I haven’t recently inspected Kino’s modules, and there’s a lot of stuff there. I should have done so before, but upon looking up Kino.Markdown, I happened to notice Kino.Mermaid. Kino.Mermaid.new/1 does exactly what I needed with zero changes:

So now I just need to translate my graph to the Mermaid markdown, which should be straightforward enough.

I’m unfortunately not using Elixir for work at the moment, and so I get to my Livebook projects in spurts. So this is a good time to go through all the updates that have been added in Kino (unless these have been there a while, and I missed them the past few times. Haha.)

Thanks for your help!

bmitc

bmitc

I had tried with Kino.Markdown.new/1, but I just realized that I didn’t surround the Mermaid markdown with triple backticks and the mermaid designation. Upon doing so, it works as well:

So either Kino.Markdown.new/1 or Kino.Mermaid.new/1 work fine, with the latter requiring only the content.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement