Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bollwyvl/e51b4e724f0b82669c84 to your computer and use it in GitHub Desktop.
Save bollwyvl/e51b4e724f0b82669c84 to your computer and use it in GitHub Desktop.
Embedded Mermaid diagrams in the IPython Notebook
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@drorata
Copy link

drorata commented Dec 29, 2021

I cannot see the rendered diagrams :(

@stoney95
Copy link

stoney95 commented Jan 5, 2022

+1

@sherdim
Copy link

sherdim commented Apr 19, 2022

@mattmorganpdx
Copy link

This didn't work for me and I found errors in the chrome dev tools that allowed me to find that there was an error resolved in a new mermaid so I'd sub out the above <script> with a link to a current mermaid version from a cdn you can get from say here:
https://cdnjs.com/libraries/mermaid
Also I'm using jupyter lab which didn't have jquery loaded, you can see this if the dev tools give you an error about '$'
to fix that I just added a script tag importing jquery from a cdn.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.7/mermaid.min.js" integrity_no="sha512-1ypa9tdUrJAWv5g28Mb5x0zXaUuI4SBofKff88OGyk5D/oOd4x1IPxYHsx3K81bwBKt8NVUvGgw7TgNZ6PJX2A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
    .mermaid *{font-family: sans-serif; }
    .mermaid .node, .mermaid .cluster{
      fill: white !important;
      stroke: black !important;
      stroke-width: 1px !important;
    }
    .mermaid div{
      text-align: center;
    }
    .mermaid .label{
      color: black;
    }
</style>
<script>$(function(){
    // mermaid load a touch weirdly: try immediately, but try again later if it's not available
    var initMermaid = function(){
        return (window.mermaid && mermaid.init()) || setTimeout(initMermaid, 50);         
    }
    initMermaid();

    // for live editing, re-render only the text of the current cell
    window.IPython && $(IPython.events).on("rendered.MarkdownCell", function(evt, data){
        // this is using a hacked mermaid that accepts some nodes!
        mermaid.init(undefined, data.cell.element.find(".mermaid"));
    });
});</script>```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment