# Shapes
A shape tells the frontend how to draw one element of a diagram. The frontend is the
ipyelk JupyterLab extension that draws the diagram in the browser. An element is a node,
a port, a label or an edge. The frontend draws most shapes in one SVG element, the
diagram SVG.
## How an element gets its shape
Each element has a `properties` object, and the shape is the `shape` field of that
object. Each kind of element has its own properties class:
| Element | Properties class | Shape classes |
| ------- | ----------------- | ---------------------------------------------- |
| node | `NodeProperties` | `NodeShape` and its subclasses, such as `Rect` |
| port | `PortProperties` | `PortShape` |
| label | `LabelProperties` | `LabelShape` and `Icon` |
| edge | `EdgeProperties` | `EdgeShape` |
```python
from ipyelk.elements import Node, NodeProperties, shapes
node = Node(properties=NodeProperties(shape=shapes.Circle(radius=20)))
```
The `type` field of a shape holds the type string, for example `node:round`. The
frontend uses the type string to select the renderer. A renderer is the frontend view
that draws one kind of shape. If an element has no shape, or its shape has no `type`,
the frontend uses the default renderer for the element. The default renderers draw a
rectangle for a node or a port, the text for a label and a line for an edge.
## The `use` field
Most shapes hold their payload in the `use` field. The payload is the data that the
renderer draws, for example the path data of a `Path`. The type string of the shape sets
the type of the payload. Each shape class validates its payload when you make the shape,
when you assign `use` and when ipyelk loads JSON. An invalid payload raises a
`ValidationError` at the `use` field. The one exception is a `Widget` whose `use` is not
the `model_id` of its widget: its error is at the shape, not at the field.
| Class | Payload type | Meaning of `use` | Required |
| ------------------------------------------- | ------------ | ------------------------------------------------------------- | -------- |
| `Path` | `PathData` | the `d` attribute of an SVG `path`, which starts with `M` | yes |
| `SVG` | `Markup` | SVG markup | yes |
| `ForeignObject`, `HTML` | `Markup` | HTML markup | yes |
| `Image` | `ImageUrl` | the URL of an image, not empty | yes |
| `Use`, `Icon` | `SymbolId` | a symbol identifier, not empty | yes |
| `PortShape`, `LabelShape` | `SymbolId` | a symbol identifier. `None` draws a rectangle or the text. | no |
| `Comment` | `NotchSize` | the size of the notch in pixels, a finite number of 0 or more | no, 15 |
| `Widget` | a `model_id` | ipyelk writes the `model_id` of the widget | no |
| `Rect`, `Circle`, `Ellipse`, `Diamond` | none | `use` must be `None` | no |
| `NodeShape` and the shape classes of others | `str` | the payload of a custom renderer | no |
A symbol is a drawing that you define once with `Symbol` and add to the `symbols` of the
diagram. The symbol identifier is the `identifier` of that `Symbol`. If no symbol has
the identifier, a port draws a rectangle and a label draws its text. A `Use` node and an
edge end draw nothing.
A `Comment` notch is the length of the two short sides of the triangle that the renderer
cuts from the top-right corner. 0 draws no notch, so the node is a rectangle. The
renderer limits the notch to the width and the height of the node. A string of a number,
which ipyelk 2.x wrote, loads as the number.
A `NodeShape`, `PortShape`, `LabelShape` or `ElementShape` with the type string of
another class gives `use` the type of that class: its annotation, constraints and model
configuration, but not a `field_validator` of the class. It writes `use` as that class
does. So `NodeShape(type="node:comment", use="abc")` raises a `ValidationError`, as
`Comment(use="abc")` does, and `NodeShape(type="node:comment", use="20").use` is `20.0`.
A type string whose class requires `use`, such as `node:path`, requires it here too. The
JSON leaves out a `use` of `None`, so a `node:comment` shape with no `use` draws the
default notch. A `node:widget` shape takes any string or `None`, because ELK JSON loads
a `Widget` with no open widget as a `NodeShape`.
To give a shape class of your own a payload, subclass `NodeShape`, `PortShape` or
`LabelShape`. Set a new type string, and give `use` or new fields a type. JSON then
loads the type string as your class, with your validation. The frontend needs a renderer
for that type string.
```{warning}
The frontend inserts the markup of `SVG`, `ForeignObject` and `HTML` into the page
without changes. Put only markup from a source that you trust in `use`. The `Markup`
type does not make markup safe.
```
## Width and height
The `width` and `height` of a shape are a size in pixels. If the element has no `width`
or `height` of its own, the element serializes the size of its shape. The layout can
then change the size of the element. The laid-out size is the size of the element after
the layout. The renderers use it as follows:
- Most renderers draw the shape at the laid-out size.
- The `node:round` renderer of `Circle` and `Ellipse` draws an ellipse. Its radii are
half the laid-out width and height. If `x` and `y` are not set, the ellipse is at the
center of the node.
- `Path` and `SVG` draw their markup at its own coordinates. The renderer does not scale
it.
- A parent label places its icon and its text with the `width` and `height` of the
`Icon` shape, if they are set. It draws the icon at the laid-out size.
- If a `LabelShape` sets both `width` and `height`, and neither is 0, the frontend does
not measure the text of the label.
After each layout, ipyelk writes the laid-out `width` and `height` that come back from
the frontend onto the element, where you can read them. A laid-out size is the output of
a layout, not an input to the next one unless nothing else gives the element a size. The
size that ipyelk serializes as the input to the next layout is, in this order:
1. The `width` or `height` that you assigned to the element, also before the first
layout. The layout does not forget it: it is sent again after each layout. Assign
`None` to drop it.
2. The size of the shape. So `node.properties.shape.radius = 20` after a layout changes
the size of the node at the next layout.
3. The laid-out size, for an element whose shape has no size, or that has no shape.
This rule applies when ipyelk serializes the elements as the input to a layout, which
the `VisibilityPipe` does under `exclude_layout`. Any other serialization, for example
the one that shows the elements in the view after a refresh that runs no layout, or a
plain `model_dump()`, carries the laid-out size. So does a pipeline that you compose
without a `VisibilityPipe`.
A change to a shape's size needs a layout to update the view. After changing the radius
of a node in `diagram.source`, request that layout before refreshing:
```python
from ipyelk.pipes import flows as F
node.properties.shape.radius = 20
diagram.source.record(F.Layout)
diagram.refresh()
```
A bare `diagram.refresh()` does not request a layout after this edit.
The same holds for the size that the frontend measures for a label: if the `LabelShape`
sets `width` but not `height`, the layout gets the `width` of the shape and the measured
`height`, starting with the first layout. A `width` or `height` assigned to a `Label` is
also sent to the layout instead of the measured size, starting with the first layout.
The shape keeps its class and its fields through a layout. The frontend does not change
the `properties` of an element, so ipyelk keeps them, with the shape object, for example
a `Widget` with its widget, and does not replace them with what comes back. A layout can
give a node a size other than the size of its shape, for example a `Circle` node with
children. The `radius` then stays as you set it, and the `width` and `height` of the
node hold the laid-out size.
## Shapes from JSON
When ipyelk loads ELK JSON, the type string of each shape selects its class. ELK JSON
comes back from the frontend after each layout, and `from_elkjson` loads it from a
dictionary. A shape with the type string `node:path` loads as a `Path`, and a shape with
`label:icon` loads as an `Icon`. A shape with no type string loads as the shape class of
its element, for example `NodeShape`. If the type string belongs to another kind of
element, for example `label:icon` on a node, loading raises a `ValidationError`.
`Circle` and `Ellipse` have the same type string, `node:round`. The JSON does not hold
`radius`, `rx` or `ry`, and the renderer draws both shapes from the width and height. So
a `node:round` shape loads as an `Ellipse` with the `width` and `height` of the JSON. A
`node:widget` shape loads as a `Widget` with the widget that has the `model_id` in
`use`. If no open widget has that `model_id`, the shape loads as a `NodeShape` with the
same JSON, and ipyelk warns. This occurs when the widget closed, or when the JSON comes
from another kernel. Only ELK JSON loads in this way: a `node:widget` dictionary in your
own code with no open widget raises a `ValidationError`.
If a shape class of your own sets a new type string, the class adds itself to
`BaseShape.json_classes` when Python defines it. If another class already has that type
string, ipyelk warns, and JSON loads the type string as the new class.
An element, its properties and its shape raise a `ValidationError` for a key that they
do not have, because such a key is usually a typo. For example, `Rect(start="arrow")`
raises, because `start` is a field of `EdgeShape`. ELK JSON can have keys that the
models do not have, so loading ELK JSON ignores them. `from_wire(Node, data)` from
`ipyelk.elements` loads a dictionary in the same way.
## The shapes
The example column links to a notebook that runs in your browser.
| Class | `type` string | Example |
| ------------------------------------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| {py:class}`~ipyelk.elements.shapes.Rect` | `node` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Circle` | `node:round` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Ellipse` | `node:round` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Diamond` | `node:diamond` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Comment` | `node:comment` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Path` | `node:path` | 12_Node_Menagerie, 10_Diagram_Defs |
| {py:class}`~ipyelk.elements.shapes.SVG` | `node:svg` | 12_Node_Menagerie, 11_Logic_Gates |
| {py:class}`~ipyelk.elements.shapes.Use` | `node:use` | 10_Diagram_Defs, 11_Logic_Gates |
| {py:class}`~ipyelk.elements.shapes.Image` | `node:image` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.ForeignObject` | `node:foreignobject` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.HTML` | `node:html` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.Widget` | `node:widget` | 12_Node_Menagerie, 15_Nesting_Plots |
| {py:class}`~ipyelk.elements.shapes.PortShape` | `port` | 10_Diagram_Defs |
| {py:class}`~ipyelk.elements.shapes.LabelShape` | `label` | |
| {py:class}`~ipyelk.elements.shapes.Icon` | `label:icon` | 12_Node_Menagerie |
| {py:class}`~ipyelk.elements.shapes.EdgeShape` | `edge` | 10_Diagram_Defs |
## Section dividers in a node
To divide a node into sections, use labels, not a node shape. Set `separator=True` in
the `LabelProperties` of the first label of each section. The frontend draws a line
across the full width of the node above each of these labels. `separatorGap` sets the
distance in pixels between the line and the label. Labels above the first line, for
example a title, are optional. The frontend also has an older `node:compartment`
renderer, which has no shape class.
## API
### Node shapes
```{eval-rst}
.. currentmodule:: ipyelk.elements.shapes
.. autoclass:: Rect
.. autoclass:: Circle
.. autoclass:: Ellipse
.. autoclass:: Diamond
.. autoclass:: Comment
.. autoclass:: Path
:members: from_list
.. autoclass:: SVG
.. autoclass:: Use
.. autoclass:: Image
.. autoclass:: ForeignObject
.. autoclass:: HTML
.. autoclass:: Widget
```
### Port, label and edge shapes
```{eval-rst}
.. currentmodule:: ipyelk.elements.shapes
.. autoclass:: PortShape
.. autoclass:: LabelShape
.. autoclass:: Icon
.. autoclass:: EdgeShape
```
### Symbols
```{eval-rst}
.. currentmodule:: ipyelk.elements
.. autoclass:: Symbol
.. autoclass:: EndpointSymbol
.. autoclass:: SymbolSpec
:members: add, merge
```
### Payload types
```{eval-rst}
.. currentmodule:: ipyelk.elements.shapes
.. py:data:: SymbolId
:type: str
The ``identifier`` of a symbol in the diagram's ``defs``. It must not be empty.
.. py:data:: PathData
:type: str
The ``d`` attribute of an SVG ``path``: ``M`` or ``m`` and a number first, then
path commands, numbers, commas and ASCII whitespace.
.. py:data:: Markup
:type: str
SVG or HTML markup. The frontend inserts it as it is, so it must be trusted.
.. py:data:: ImageUrl
:type: str
The URL of an image: absolute, relative to the notebook, or a ``data:`` URL. It
must not be empty.
.. py:data:: NotchSize
:type: float
The size of the notch of a comment in pixels: a finite number, 0 or more. 0 draws
no notch. A ``bool`` raises ``ValidationError``.
```
### Base classes
```{eval-rst}
.. currentmodule:: ipyelk.elements.shapes
.. autoclass:: BaseShape
:members: dimension, json_classes, loads_from_json
.. autoclass:: ElementShape
.. autoclass:: NodeShape
.. autoclass:: Point
```
When assigning `type` on a shape subclass, payload validation uses a temporary copy. Its
`model_post_init` runs on that copy, and `model_validator(mode="after")` methods run on
both the copy and the original shape. Subclass hooks should allow these extra calls.