Skip to main content

Pydantic Mermaid

CI Coverage pypi downloads versions license

To convert Pydantic models to Mermaid charts, you can use the pydantic-mermaid library. This library allows you to generate Mermaid charts from your Pydantic models. Here are the steps to install and use pydantic-mermaid:

If you are using pydantic 2, please visit https://github.com/EricWebsmith/pydantic-2-mermaid

Use in terminal

Install the pydantic-mermaid library using pip:

pip install pydantic-mermaid

Use command line

pydantic-mermaid --module models.py --output models.md

You can run the following comamand get help for the command.

pydantic-mermaid --help

Use in code

  1. Import your Pydantic models into your Python script.
  2. Create an instance of the MermaidGenerator class from the pydantic-mermaid module and pass in your Pydantic models as arguments.
from pydantic_mermaid import MermaidGenerator

import my_module

generator = MermaidGenerator(my_module)
  1. Call the generate_chart() method of the MermaidGenerator instance to generate the Mermaid chart.
chart = generator.generate_chart()
  1. Use the chart variable to display or save the Mermaid chart.
print(chart)

This will print the Mermaid chart as a string. You can also save the chart to a file by writing the chart string to a file:

with open("chart.mmd", "w") as f:
    f.write(chart)

This will save the Mermaid chart to a file called chart.mmd. You can then use a tool like the Mermaid Live Editor to visualize and edit the chart.

examples

Inheritance:

classDiagram
    class Animal {
    }

    class Fish {
        gill: str
    }

    class Beast {
        lags: int
    }

    class Bird {
        winds: int
    }

    class Dog {
    }

    class Cat {
    }

    class Salmon {
    }

    class Eagle {
    }


    Animal <|-- Fish
    Animal <|-- Bird
    Animal <|-- Beast
    Beast <|-- Cat
    Beast <|-- Dog
    Fish <|-- Salmon
    Bird <|-- Eagle

dependencies:

Administrative Divisions of the United States of America

classDiagram
    class Municipality {
        name: str
        population: int
    }

    class MinorCivilDivision {
        name: str
        population: int
    }

    class County {
        name: str
        population: int
        municipalities: list[Municipality]
        minor_civil_divisions: list[MinorCivilDivision]
    }

    class CountyEquivalant {
        name: str
        population: int
    }

    class State {
        name: str
        population: int
        counties: list[County]
        county_equivalants: list[CountyEquivalant]
    }

    class FederalDistrict {
        name: str
        population: int
    }

    class InhabitedTerritory {
        name: str
        population: int
    }

    class HabitedTerritory {
        name: str
        population: int
    }

    class Federal {
        name: str
        population: int
        federal_distric: FederalDistrict
        states: list[State]
        Inhabited_territories: list[InhabitedTerritory]
        Habited_territories: list[HabitedTerritory]
    }


    County ..> MinorCivilDivision
    County ..> Municipality
    State ..> CountyEquivalant
    State ..> County
    Federal ..> InhabitedTerritory
    Federal ..> HabitedTerritory
    Federal ..> State
    Federal ..> FederalDistrict

For details, check examples/ folder.

Metadata

Release files for pydantic-mermaid 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-mermaid 0.7.1
File Size Uploaded Provenance
pydantic_mermaid-0.7.1.tar.gz 8.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-mermaid 0.7.1
File Interpreter ABI Platform
pydantic_mermaid-0.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / pydantic_mermaid-0.7.1.tar.gz

Download URL pydantic_mermaid-0.7.1.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
58d8b0d2e8bb0d0a7db75da29cb612de426103ce73c6d8d1b2ae77eb68962854
BLAKE2b-256 checksum
How to use checksums
a2b8b87c1c196ff4b60a7d1867b6ee6221d27c56073bd9d9b1a84b2b4b07ff23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / pydantic_mermaid-0.7.1-py3-none-any.whl

Download URL pydantic_mermaid-0.7.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d5a66563355d9c72ac3b26ad34fde32d5a3169dc5dbcd143b2463b49efc2ea0
BLAKE2b-256 checksum
How to use checksums
cea8686e5fb3d47d74b69e122f551718d847f7972637b7ba09931759397bbdc6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for pydantic-mermaid 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page