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

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:

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.

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#

class ipyelk.elements.shapes.Rect(**data)#

A rectangle at the size of the node. A node with no shape draws the same.

It has no payload: use must be None.

class ipyelk.elements.shapes.Circle(**data)#

A circle. It serializes width and height as two times radius.

The node:round renderer does not read radius. It draws an ellipse with half the laid-out width and height of the node as its radii. If the laid-out width and height differ, the ellipse is not a circle. JSON loads a Circle as an Ellipse of the same size, because JSON does not hold radius.

Fields:
  • radius (float, default 0): the radius in pixels, not serialized. The default gives a node of 0 by 0 pixels.

  • x, y (float or None, default None): the center. If they are None, the renderer centers the ellipse in the node.

  • width, height: these fields have no effect.

  • use: no payload, so it must be None.

NodeProperties(shape=Circle(radius=6))
class ipyelk.elements.shapes.Ellipse(**data)#

An ellipse. The node:round renderer draws it as it draws Circle.

Fields:
  • rx, ry (float, default 0): the horizontal and the vertical radius in pixels, not serialized.

  • width, height (float or None, default None): if width is None or 0, Ellipse serializes two times rx. The same rule applies to height and ry.

  • x, y (float or None, default None): as for Circle.

  • use: no payload, so it must be None.

class ipyelk.elements.shapes.Diamond(**data)#

A polygon with its four corners at the middle of the four sides of the node.

It has no payload: use must be None.

class ipyelk.elements.shapes.Comment(**data)#

A rectangle with a corner notch, a triangle cut from the top-right corner.

Fields:
  • use (NotchSize, default 15): the length in pixels of the two short sides of the notch, a finite number of 0 or more. 0 draws no notch. The renderer limits the notch to the width and the height of the node. A string of a number, as 2.x wrote it, loads as the number.

Comment(use=20).use == 20.0
class ipyelk.elements.shapes.Path(**data)#

An SVG path. The renderer does not scale it to the size of the node.

Fields:
  • use (PathData, required): the path data, that is, the d attribute of an SVG path element. It starts with a move, M or m. Its coordinates are pixels from the top-left corner of the node.

Path(use="M 0,0 L 0,100 L 20,30 Z")
Path.from_list([(0, 0), (0, 100), (20, 30)], closed=True)
classmethod from_list(segments, closed=False)#

Make a Path of straight lines through the points in segments.

segments is a sequence of (x, y) pairs. If closed is true, the path ends with a line back to the first point.

class ipyelk.elements.shapes.SVG(**data)#

SVG markup. The renderer does not scale it to the size of the node.

Put only trusted markup in use, as the warning on the Shapes page explains.

Fields:
  • use (Markup, required): the SVG markup.

  • x, y (float or None, default None): the offset of the markup in pixels. None is 0.

SVG(use='<circle cx="10" cy="10" r="10"/>', width=20, height=20)
class ipyelk.elements.shapes.Use(**data)#

A symbol at the size of the node.

Fields:
  • use (SymbolId, required): a symbol identifier. If no symbol has it, the renderer draws nothing.

NodeProperties(shape=Use(use="final_state"))
class ipyelk.elements.shapes.Image(**data)#

An image in an SVG image element, at the size of the node.

Fields:
  • use (ImageUrl, required): the URL of the image.

class ipyelk.elements.shapes.ForeignObject(**data)#

HTML markup in an SVG foreignObject element in the diagram SVG.

Put only trusted markup in use, as the warning on the Shapes page explains.

Fields:
  • use (Markup, required): the HTML markup.

class ipyelk.elements.shapes.HTML(**data)#

HTML markup in the HTML layer, at the position and size of the node.

The HTML layer is a div above the diagram SVG that moves and zooms with the diagram. Put only trusted markup in use, as the warning on the Shapes page explains.

Fields:
  • use (Markup, required): the HTML markup.

class ipyelk.elements.shapes.Widget(**data)#

A Jupyter widget in the HTML layer, which HTML describes.

Fields:
  • widget (ipywidgets DOMWidget, required): the widget to draw. Serialization writes its model_id to use. A use that is not the model_id of an open widget raises ValidationError, at construction and when you assign use. An assignment of widget sets use to None. JSON loads the widget that has the model_id in use. If no open widget has it, ELK JSON loads a NodeShape with the same fields, and a dictionary in user code raises.

  • delay (int or None, default None): a time in milliseconds. If it is set and not 0, the frontend applies the diagram zoom to the widget only after delay. None and 0 apply the zoom at once. The 15_Nesting_Plots example uses it.

NodeProperties(shape=Widget(widget=IntSlider()))

Port, label and edge shapes#

class ipyelk.elements.shapes.PortShape(**data)#

The shape of a port.

Fields:
  • use (SymbolId or None, default None): a symbol identifier, drawn at the size of the port. If use is None or no symbol has it, the renderer draws a rectangle.

class ipyelk.elements.shapes.LabelShape(**data)#

The shape of a label.

If width and height are both set and not 0, the frontend does not measure the text of the label.

Fields:
  • use (SymbolId or None, default None): a symbol identifier, drawn in place of the text. If use is None or no symbol has it, the renderer draws the text.

class ipyelk.elements.shapes.Icon(**data)#

A label shape for a symbol in front of the text of its parent label.

Put a label with an Icon shape first in the labels of another label, the parent label. The parent label draws the symbol to the left of its own text. A label that is not nested draws the symbol in place of its text.

Fields:
  • use (SymbolId, required): a symbol identifier.

icon = Icon(use="class", width=12, height=12)
Label(
    text="Point",
    labels=[Label(text=" ", properties=LabelProperties(shape=icon))],
)
class ipyelk.elements.shapes.EdgeShape(**data)#

The shape of an edge: a line along the route of the edge.

The renderer draws the start symbol at the first point of the route and the end symbol at the last point, turned to the direction of the route. If no symbol has the identifier, the renderer draws nothing at that end. An EndpointSymbol can also make the line shorter.

Fields:
  • start, end (str or None, default None): a symbol identifier.

EdgeProperties(shape=EdgeShape(end="arrow"))

Symbols#

class ipyelk.elements.Symbol(**data)#

A drawing that you define once and add to Diagram.symbols.

A shape refers to the symbol by its symbol identifier, that is, its identifier. The frontend draws element in an SVG symbol element.

Fields:
  • identifier (str, required): the symbol identifier.

  • element (Node, required): the root element of the drawing.

  • x, y (float, default 0), width, height (float, required): the viewBox of the symbol element. If width or height is 0, the symbol element has no viewBox.

class ipyelk.elements.EndpointSymbol(**data)#

A symbol for the end of an edge, for example an arrowhead.

Fields, in addition to the fields of Symbol:
  • path_offset (Point, default Point(0, 0)): the frontend moves the end of the line by this offset. A negative x makes the line shorter.

  • symbol_offset (Point, default Point(0, 0)): the frontend moves the symbol by this offset.

  • width, height (float, default 0)

The frontend turns both offsets to the direction of the route.

class ipyelk.elements.SymbolSpec(**data)#

A set of symbols with unique identifiers

add(*symbols)#

Add a series of symbols to the library

Return type:

SymbolSpec

Returns:

current SymbolSpec

merge(*specs)#

Merge a series of SymbolSpec`s into a new `SymbolSpec

Parameters:

specs (SymbolSpec) – series of SymbolSpecs

Return type:

SymbolSpec

Returns:

new SymbolSpec

Payload types#

ipyelk.elements.shapes.SymbolId: str#

The identifier of a symbol in the diagram’s defs. It must not be empty.

ipyelk.elements.shapes.PathData: str#

The d attribute of an SVG path: M or m and a number first, then path commands, numbers, commas and ASCII whitespace.

ipyelk.elements.shapes.Markup: str#

SVG or HTML markup. The frontend inserts it as it is, so it must be trusted.

ipyelk.elements.shapes.ImageUrl: str#

The URL of an image: absolute, relative to the notebook, or a data: URL. It must not be empty.

ipyelk.elements.shapes.NotchSize: 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#

class ipyelk.elements.shapes.BaseShape(**data)#

The base class of all shapes.

Fields:
  • type (str or None): the type string, which selects the renderer. Each subclass sets its own default. None selects the default renderer.

A subclass whose type default is a string that differs from the type default of its parent adds itself to json_classes when Python defines it. So JSON loads a shape with that type string as that class. This also applies to a subclass outside ipyelk, when its module is imported. A subclass that keeps the type of its parent does not add itself, so the parent class still loads that type string. If a second class has a type string that is already in json_classes, its definition warns with a UserWarning and the second class replaces the first, unless it sets loads_from_json to False. A class with the same module and name as the first, for example after a module reload, replaces it with no warning.

dimension(key)#

Return the value of x, y, width or height to serialize.

key is the name of the field. A subclass can calculate the value from other fields, as Circle does.

Return type:

float | None

json_classes: ClassVar[dict[str, type[BaseShape]]] = {'edge': <class 'ipyelk.elements.shapes.EdgeShape'>, 'label': <class 'ipyelk.elements.shapes.LabelShape'>, 'label:icon': <class 'ipyelk.elements.shapes.Icon'>, 'node': <class 'ipyelk.elements.shapes.Rect'>, 'node:comment': <class 'ipyelk.elements.shapes.Comment'>, 'node:diamond': <class 'ipyelk.elements.shapes.Diamond'>, 'node:foreignobject': <class 'ipyelk.elements.shapes.ForeignObject'>, 'node:html': <class 'ipyelk.elements.shapes.HTML'>, 'node:image': <class 'ipyelk.elements.shapes.Image'>, 'node:path': <class 'ipyelk.elements.shapes.Path'>, 'node:round': <class 'ipyelk.elements.shapes.Ellipse'>, 'node:svg': <class 'ipyelk.elements.shapes.SVG'>, 'node:use': <class 'ipyelk.elements.shapes.Use'>, 'node:widget': <class 'ipyelk.elements.shapes.Widget'>, 'port': <class 'ipyelk.elements.shapes.PortShape'>}#

The class that loads each type string from JSON.

loads_from_json: ClassVar[bool] = True#

False keeps the class out of json_classes. A subclass does not inherit it.

class ipyelk.elements.shapes.ElementShape(**data)#

The base class of the shapes of nodes, ports and labels.

Fields, all None by default:
  • x, y (float): a position in pixels in the element. Only Circle, Ellipse and SVG use them.

  • width, height (float): a size in pixels. The element serializes this size if it has no width or height of its own. After a layout, the element holds the laid-out size, and the shape’s size still wins over it as the input to the next layout. The Width and height section of the Shapes page explains this.

  • use (str): the payload. Each built-in subclass gives use its own meaning and type. A custom shape type can give it any meaning. With the type string of another class, for example NodeShape(type="node:path"), use has the type of that class, here PathData.

  • delay (int): a time in milliseconds. Only Widget uses it.

class ipyelk.elements.shapes.NodeShape(**data)#

The base class of the node shapes. With no type, it draws a rectangle.

class ipyelk.elements.shapes.Point(x=0, y=0)#

A point in pixels. Point(-1, 0) is the same as Point(x=-1, y=0).

Fields:
  • x, y (float, default 0)

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.