Diagram¶
Schema visualization
Diagram visualization for DataJoint schemas.
This module provides the Diagram class for visualizing schema structure as directed acyclic graphs showing tables and their foreign key relationships.
Diagram ¶
Bases: DiGraph
Schema diagram as a directed acyclic graph (DAG).
Visualizes tables and foreign key relationships derived from
connection.dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Table, Schema, or module
|
A table object, table class, schema, or module with a schema. |
required |
context
|
dict
|
Namespace for resolving table class names. If None, uses caller's frame globals/locals. |
None
|
Examples:
>>> diag = dj.Diagram(schema.MyTable)
>>> diag.draw()
Operators:
diag1 + diag2- union of diagramsdiag1 - diag2- difference of diagramsdiag1 * diag2- intersection of diagramsdiag + n- expand n levels of successors (children)diag - n- expand n levels of predecessors (parents)
>>> dj.Diagram(schema.Table) + 1 - 1 # immediate ancestors and descendants
Notes
diagram + 1 - 1 may differ from diagram - 1 + 1.
Only tables loaded in the connection are displayed.
Layout direction is controlled via dj.config.display.diagram_direction
(default "TB"). Use dj.config.override() to change temporarily::
with dj.config.override(display_diagram_direction="LR"):
dj.Diagram(schema).draw()
from_sequence
classmethod
¶
from_sequence(sequence)
Create combined Diagram from a sequence of sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence
|
iterable
|
Sequence of table objects, classes, or schemas. |
required |
Returns:
| Type | Description |
|---|---|
Diagram
|
Union of diagrams: |
add_parts ¶
add_parts()
Add part tables of all masters already in the diagram.
Returns:
| Type | Description |
|---|---|
Diagram
|
New diagram with part tables included. |
collapse ¶
collapse()
Mark all nodes in this diagram as collapsed.
Collapsed nodes are shown as a single node per schema. When combined
with other diagrams using +, expanded nodes win: if a node is
expanded in either operand, it remains expanded in the result.
Returns:
| Type | Description |
|---|---|
Diagram
|
A copy of this diagram with all nodes collapsed. |
Examples:
>>> # Show schema1 expanded, schema2 collapsed into single nodes
>>> dj.Diagram(schema1) + dj.Diagram(schema2).collapse()
>>> # Collapse all three schemas together
>>> (dj.Diagram(schema1) + dj.Diagram(schema2) + dj.Diagram(schema3)).collapse()
>>> # Expand one table from collapsed schema
>>> dj.Diagram(schema).collapse() + dj.Diagram(SingleTable)
topo_sort ¶
topo_sort()
Return nodes in topological order.
Returns:
| Type | Description |
|---|---|
list[str]
|
Node names in topological order. |
make_dot ¶
make_dot()
Generate a pydot graph object.
Returns:
| Type | Description |
|---|---|
Dot
|
The graph object ready for rendering. |
Notes
Layout direction is controlled via dj.config.display.diagram_direction.
Tables are grouped by schema, with the Python module name shown as the
group label when available.
make_mermaid ¶
make_mermaid()
Generate Mermaid diagram syntax.
Produces a flowchart in Mermaid syntax that can be rendered in Markdown documentation, GitHub, or https://mermaid.live.
Returns:
| Type | Description |
|---|---|
str
|
Mermaid flowchart syntax. |
Notes
Layout direction is controlled via dj.config.display.diagram_direction.
Tables are grouped by schema using Mermaid subgraphs, with the Python
module name shown as the group label when available.
Examples:
>>> print(dj.Diagram(schema).make_mermaid())
flowchart TB
subgraph my_pipeline
Mouse[Mouse]:::manual
Session[Session]:::manual
Neuron([Neuron]):::computed
end
Mouse --> Session
Session --> Neuron
save ¶
save(filename, format=None)
Save diagram to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output filename. |
required |
format
|
str
|
File format ( |
None
|
Raises:
| Type | Description |
|---|---|
DataJointError
|
If format is unsupported. |
Notes
Layout direction is controlled via dj.config.display.diagram_direction.
Tables are grouped by schema, with the Python module name shown as the
group label when available.