Display Mermaid diagram from C# variable in Polyglot notebook

656 Views Asked by At

I know that Polyglot notebooks support Mermaid diagrams. But it doesn't support veriable sharing. Suppose I have diagram code in C# string variable. Are there any possibility to display that string as diagram?

Example:

// below diagram code was generated via some method
var mermaidCode = """
flowchart TD
    A --> B
""";

// here I would like to display mermaidCode as diagram
mermaidCode
3

There are 3 best solutions below

0
WReach On BEST ANSWER

Here is a way that generates a Mermaid output cell by means of a MermaidMarkdown display object:

#r "Microsoft.DotNet.Interactive.Mermaid.dll"
using Microsoft.DotNet.Interactive.Mermaid;

var mermaidCode = """
flowchart TD
    A --> B
""";

new MermaidMarkdown(mermaidCode)

polyglot notebook screenshot

1
ATK On

Its litle late, but you can do this.

1 - generate a "mermaid" cell with

using Microsoft.DotNet.Interactive; 
using Microsoft.DotNet.Interactive.Commands;
    
await Kernel.Root.SendAsync(new SendEditableCode("mermaid", <yourMarkdown>));

2 - run the new generated cell

0
mato On

actually the SubmitCode does not work that way. It has different arguments order.

await Kernel.Root.SendAsync(new SubmitCode("<your-markdown>", "mermaid", SubmissionType.Run));