diagrams in jupyter and mkdocs¤
mermaid has emerged as a community standard for diagrams. we can use it in three places relative to this content:
- mermaid can be used on github
- mermaid can be used in
jupyterlab
withjupyterlab-markup
- mermaid can be used with
mkdocs-material
%reload_ext pidgy
import requests, requests_cache, bs4
requests_cache.install_cache(backend="filesystem")
from the `root` page, using `beautifulsoup4` as an intermediary, we can discover that mermaid
supports {{len(kinds)}} chart syntaxes.
```python
soup = bs4.BeautifulSoup((root := requests.get("https://mermaid.js.org/syntax/classDiagram.html")).text, "html.parser")
kinds = set(x.attrs["href"]for x in soup.select("a") if x.attrs["href"].startswith(("/syntax",)))
```
from the root
page, using beautifulsoup4
as an intermediary, we can discover that mermaid
supports 13 chart syntaxes.
soup = bs4.BeautifulSoup((root := requests.get("https://mermaid.js.org/syntax/classDiagram.html")).text, "html.parser")
kinds = set(x.attrs["href"]for x in soup.select("a") if x.attrs["href"].startswith(("/syntax",)))
extracting the syntaxes to build one master chart.
```python
baseurl = root.url.rsplit("/", 2)[0]
syntaxes = {baseurl + x for x in kinds}
responses = {x: requests.get(x) for x in syntaxes}
```
baseurl = root.url.rsplit("/", 2)[0]
syntaxes = {baseurl + x for x in kinds}
responses = {x: requests.get(x) for x in syntaxes}
```python
def strip_front_matter(x):
fence = ('---',)
if x.startswith(fence):
y = None
for line in x.splitlines(1)[1:]:
if y is None:
if line.startswith(fence):
y = ""
else:
y += line
return y
return x
```
def strip_front_matter(x):
fence = ('---',)
if x.startswith(fence):
y = None
for line in x.splitlines(1)[1:]:
if y is None:
if line.startswith(fence):
y = ""
else:
y += line
return y
return x
## all the diagram styles collection programmatically
{% for syntax in syntaxes %}
{% set page = bs4.BeautifulSoup(responses.get(syntax).text, features="html.parser") %}
{% set title = page.select_one("h1").text.rstrip("#") %}
### {{title}}
```mermaid
{{textwrap.dedent(strip_front_matter(page.select_one("pre code").text))}}
```
{% endfor %}
all the diagram styles collection programmatically¤
Mindmap¤
mindmap
root((mindmap))
Origins
Long history
::icon(fa fa-book)
Popularisation
British popular psychology author Tony Buzan
Research
On effectiveness<br/>and features
On Automatic creation
Uses
Creative techniques
Strategic planning
Argument mapping
Tools
Pen and paper
Mermaid
C4 Diagrams¤
C4Context
title System Context diagram for Internet Banking System
Enterprise_Boundary(b0, "BankBoundary0") {
Person(customerA, "Banking Customer A", "A customer of the bank, with personal bank accounts.")
Person(customerB, "Banking Customer B")
Person_Ext(customerC, "Banking Customer C", "desc")
Person(customerD, "Banking Customer D", "A customer of the bank, <br/> with personal bank accounts.")
System(SystemAA, "Internet Banking System", "Allows customers to view information about their bank accounts, and make payments.")
Enterprise_Boundary(b1, "BankBoundary") {
SystemDb_Ext(SystemE, "Mainframe Banking System", "Stores all of the core banking information about customers, accounts, transactions, etc.")
System_Boundary(b2, "BankBoundary2") {
System(SystemA, "Banking System A")
System(SystemB, "Banking System B", "A system of the bank, with personal bank accounts. next line.")
}
System_Ext(SystemC, "E-mail system", "The internal Microsoft Exchange e-mail system.")
SystemDb(SystemD, "Banking System D Database", "A system of the bank, with personal bank accounts.")
Boundary(b3, "BankBoundary3", "boundary") {
SystemQueue(SystemF, "Banking System F Queue", "A system of the bank.")
SystemQueue_Ext(SystemG, "Banking System G Queue", "A system of the bank, with personal bank accounts.")
}
}
}
BiRel(customerA, SystemAA, "Uses")
BiRel(SystemAA, SystemE, "Uses")
Rel(SystemAA, SystemC, "Sends e-mails", "SMTP")
Rel(SystemC, customerA, "Sends e-mails to")
UpdateElementStyle(customerA, $fontColor="red", $bgColor="grey", $borderColor="red")
UpdateRelStyle(customerA, SystemAA, $textColor="blue", $lineColor="blue", $offsetX="5")
UpdateRelStyle(SystemAA, SystemE, $textColor="blue", $lineColor="blue", $offsetY="-10")
UpdateRelStyle(SystemAA, SystemC, $textColor="blue", $lineColor="blue", $offsetY="-40", $offsetX="-50")
UpdateRelStyle(SystemC, customerA, $textColor="red", $lineColor="red", $offsetX="-50", $offsetY="20")
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
Examples¤
pie title NETFLIX
"Time spent looking for movie" : 90
"Time spent watching it" : 10
User Journey Diagram¤
journey
title My working day
section Go to work
Make tea: 5: Me
Go upstairs: 3: Me
Do work: 1: Me, Cat
section Go home
Go downstairs: 5: Me
Sit down: 5: Me
Sequence diagrams¤
sequenceDiagram
Alice->>John: Hello John, how are you?
John-->>Alice: Great!
Alice-)John: See you later!
Requirement Diagram¤
requirementDiagram
requirement test_req {
id: 1
text: the test text.
risk: high
verifymethod: test
}
element test_entity {
type: simulation
}
test_entity - satisfies -> test_req
Flowcharts - Basic Syntax¤
flowchart LR
id
Entity Relationship Diagrams¤
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
Pie chart diagrams¤
pie title Pets adopted by volunteers
"Dogs" : 386
"Cats" : 85
"Rats" : 15
State diagrams¤
stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*]
Gitgraph Diagrams¤
gitGraph
commit
commit
branch develop
checkout develop
commit
commit
checkout main
merge develop
commit
commit
Class diagrams¤
classDiagram
note "From Duck till Zebra"
Animal <|-- Duck
note for Duck "can fly\ncan swim\ncan dive\ncan help in debugging"
Animal <|-- Fish
Animal <|-- Zebra
Animal : +int age
Animal : +String gender
Animal: +isMammal()
Animal: +mate()
class Duck{
+String beakColor
+swim()
+quack()
}
class Fish{
-int sizeInFeet
-canEat()
}
class Zebra{
+bool is_wild
+run()
}
Gantt diagrams¤
gantt
title A Gantt Diagram
dateFormat YYYY-MM-DD
section Section
A task :a1, 2014-01-01, 30d
Another task :after a1 , 20d
section Another
Task in sec :2014-01-12 , 12d
another task : 24d
the following demos are not working, but {{len(syntaxes)-3}}/{{len(syntaxes)}} demos in one place ain't bad.
- [ ] C4 Diagrams
- [ ] Class diagrams (these work the mkdocs version)
- [ ] Mindmap
the following demos are not working, but 10/13 demos in one place ain't bad.
- C4 Diagrams
- Class diagrams (these work the mkdocs version)
- Mindmap
## why did mermaid win?
i <3 `graphviz` and i'm curius why mermaid one.
graphviz is over 30 years old and written in c, but maybe the javascript was enough to win.
does wasm change the game?
i hope to explore this question further when i read the [graphviz authors take][mermaid-dot-author].
[mermaid-dot-author]: https://forum.graphviz.org/t/github-adding-support-for-mermaid-diagrams/998
why did mermaid win?¤
i <3 graphviz
and i'm curius why mermaid one.
graphviz is over 30 years old and written in c, but maybe the javascript was enough to win.
does wasm change the game?
i hope to explore this question further when i read the graphviz authors take.
## appendix
> weird thing: i was only able to get mermaid to work when the title for the document did not have mermaid in it.
appendix¤
> weird thing: i was only able to get mermaid to work when the title for the document did not have mermaid in it.