Skip to content

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:

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.

baseurl = root.url.rsplit("/", 2)[0]
syntaxes = {baseurl + x for x in kinds}
responses = {x: requests.get(x) for x in syntaxes}
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¤

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-&gt;&gt;John: Hello John, how are you?
    John--&gt;&gt;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 -&gt; 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
    [*] --&gt; Still
    Still --&gt; [*]

    Still --&gt; Moving
    Moving --&gt; Still
    Moving --&gt; Crash
    Crash --&gt; [*]

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 &lt;|-- Duck
    note for Duck "can fly\ncan swim\ncan dive\ncan help in debugging"
    Animal &lt;|-- Fish
    Animal &lt;|-- 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 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.

appendix¤

> weird thing: i was only able to get mermaid to work when the title for the document did not have mermaid in it.