[1bd6b5]: / helpers / mermaid.py

Download this file

40 lines (30 with data), 768 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from IPython.core.magic import needs_local_scope, register_cell_magic
from IPython.display import HTML, display
# embed the script
display(
HTML('<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.6.4/mermaid.min.js"></script>')
)
COUNTER = 0
TEMPLATE = """
<div id="mermaid_{id}">
{content}
</div>
<script>mermaid.initialize({init_args})</script>
<script>mermaid.init({init_args}, "#mermaid_{id}");</script>
"""
@register_cell_magic
@needs_local_scope
def mermaid(line, cell, local_ns):
if not line:
line = {}
global COUNTER
content = cell.format(**local_ns)
COUNTER += 1
return HTML(
TEMPLATE
.format(
content=content,
id=COUNTER,
init_args=line
)
)