# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Usage Example: Pydantic
#
# This notebook shows an example of using erdantic with [Pydantic](https://pydantic-docs.helpmanual.io/) models.
#
# Let's take a look at the models from the `erdantic.examples.pydantic` module. Here's their source code for clariy.

import erdantic.examples.pydantic
??erdantic.examples.pydantic

# ## Using the CLI
#
# The fastest way to rendering a diagram is to use the command-line interface. Below we use IPython's `!` to run a command in the system shell. We pass the full dotted path to the root class of our composition hierarchy, along with an output file path. erdantic will walk the composition graph to find all child classes. 

# !erdantic erdantic.examples.pydantic.Party -o diagram.png

# The format rendered is inferred from the file extension.

# ## Using the Python library
#
# You can also use the erdantic Python library, which lets you inspect the diagram object. The diagram object even automatically renders in Jupyter notebooks as demonstrated below. 

# +
import erdantic as erd
from erdantic.examples.pydantic import Party


diagram = erd.create(Party)
diagram
# -

diagram.models

diagram.edges

# You can use the `draw` method to render the diagram to disk.

# +
diagram.draw("pydantic.svg")

# Equivalently, use erd.draw directly from Party
# erd.draw(Party, out="pydantic.svg")
# -

# erdantic uses [Graphviz](https://graphviz.org/), a venerable open-source C library, to create the diagram. Graphviz uses the [DOT language](https://graphviz.org/doc/info/lang.html) for describing graphs. You use the `to_dot` method to get the DOT representation as a string. 

# +
print(diagram.to_dot())

# Equivalently, use erd.to_dot directly from Party
assert diagram.to_dot() == erd.to_dot(Party)
# -

# ## Terminal Models
#
# If you have an enormous composition graph and want to chop it up, you can make that work by specifying models to be terminal nodes.
#
# For the CLI, use the `-t` option to specify a model to be a terminus. To specify more than one, used repeated `-t` options. So, for example, if you want one diagram rooted by `Party` that terminates at `Quest`, and another diagram that is rooted by `Quest`, you can use the following two shell commands.
#
# ```bash
# erdantic erdantic.examples.pydantic.Party \ 
#     -t erdantic erdantic.examples.pydantic.Quest \
#     -o party.png
# erdantic erdantic.examples.pydantic.Quest -o quest.png
# ```

# When using the Python library, pass your terminal node in a list to the `termini` keyword argument. Below is the Python code for creating diagrams equivalent to the above shell commands.

# +
from erdantic.examples.pydantic import Quest


erd.create(Party, termini=[Quest])
# -

erd.create(Quest)

StarcoderdataJupyterScriptsDedupFiltered