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 |
|
|
port |
|
|
label |
|
|
edge |
|
|
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 |
Required |
|---|---|---|---|
|
|
the |
yes |
|
|
SVG markup |
yes |
|
|
HTML markup |
yes |
|
|
the URL of an image, not empty |
yes |
|
|
a symbol identifier, not empty |
yes |
|
|
a symbol identifier. |
no |
|
|
the size of the notch in pixels, a finite number of 0 or more |
no, 15 |
|
a |
ipyelk writes the |
no |
|
none |
|
no |
|
|
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:roundrenderer ofCircleandEllipsedraws an ellipse. Its radii are half the laid-out width and height. Ifxandyare not set, the ellipse is at the center of the node.PathandSVGdraw their markup at its own coordinates. The renderer does not scale it.A parent label places its icon and its text with the
widthandheightof theIconshape, if they are set. It draws the icon at the laid-out size.If a
LabelShapesets bothwidthandheight, 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:
The
widthorheightthat you assigned to the element, also before the first layout. The layout does not forget it: it is sent again after each layout. AssignNoneto drop it.The size of the shape. So
node.properties.shape.radius = 20after a layout changes the size of the node at the next layout.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.
Class |
|
Example |
|---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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:
usemust beNone.
- class ipyelk.elements.shapes.Circle(**data)#
A circle. It serializes
widthandheightas two timesradius.The
node:roundrenderer does not readradius. 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 aCircleas anEllipseof the same size, because JSON does not holdradius.- Fields:
radius(float, default0): the radius in pixels, not serialized. The default gives a node of 0 by 0 pixels.x,y(floatorNone, defaultNone): the center. If they areNone, the renderer centers the ellipse in the node.width,height: these fields have no effect.use: no payload, so it must beNone.
NodeProperties(shape=Circle(radius=6))
- class ipyelk.elements.shapes.Ellipse(**data)#
An ellipse. The
node:roundrenderer draws it as it drawsCircle.- Fields:
rx,ry(float, default0): the horizontal and the vertical radius in pixels, not serialized.width,height(floatorNone, defaultNone): ifwidthisNoneor 0,Ellipseserializes two timesrx. The same rule applies toheightandry.x,y(floatorNone, defaultNone): as forCircle.use: no payload, so it must beNone.
- 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:
usemust beNone.
- class ipyelk.elements.shapes.Comment(**data)#
A rectangle with a corner notch, a triangle cut from the top-right corner.
- Fields:
use(NotchSize, default15): 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, thedattribute of an SVGpathelement. It starts with a move,Morm. 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
Pathof straight lines through the points insegments.segmentsis a sequence of(x, y)pairs. Ifclosedis 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(floatorNone, defaultNone): the offset of the markup in pixels.Noneis 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
imageelement, 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
foreignObjectelement 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
divabove the diagram SVG that moves and zooms with the diagram. Put only trusted markup inuse, 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
HTMLdescribes.- Fields:
widget(ipywidgetsDOMWidget, required): the widget to draw. Serialization writes itsmodel_idtouse. Ausethat is not themodel_idof an openwidgetraisesValidationError, at construction and when you assignuse. An assignment ofwidgetsetsusetoNone. JSON loads the widget that has themodel_idinuse. If no open widget has it, ELK JSON loads aNodeShapewith the same fields, and a dictionary in user code raises.delay(intorNone, defaultNone): a time in milliseconds. If it is set and not 0, the frontend applies the diagram zoom to the widget only afterdelay.Noneand 0 apply the zoom at once. The15_Nesting_Plotsexample 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(SymbolIdorNone, defaultNone): a symbol identifier, drawn at the size of the port. IfuseisNoneor no symbol has it, the renderer draws a rectangle.
- class ipyelk.elements.shapes.LabelShape(**data)#
The shape of a label.
If
widthandheightare both set and not 0, the frontend does not measure the text of the label.- Fields:
use(SymbolIdorNone, defaultNone): a symbol identifier, drawn in place of the text. IfuseisNoneor 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
Iconshape first in thelabelsof 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
startsymbol at the first point of the route and theendsymbol at the last point, turned to the direction of the route. If no symbol has the identifier, the renderer draws nothing at that end. AnEndpointSymbolcan also make the line shorter.- Fields:
start,end(strorNone, defaultNone): 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 drawselementin an SVGsymbolelement.- Fields:
identifier(str, required): the symbol identifier.element(Node, required): the root element of the drawing.x,y(float, default0),width,height(float, required): theviewBoxof thesymbolelement. Ifwidthorheightis 0, thesymbolelement has noviewBox.
- 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, defaultPoint(0, 0)): the frontend moves the end of the line by this offset. A negativexmakes the line shorter.symbol_offset(Point, defaultPoint(0, 0)): the frontend moves the symbol by this offset.width,height(float, default0)
The frontend turns both offsets to the direction of the route.
- Fields, in addition to the fields of
- class ipyelk.elements.SymbolSpec(**data)#
A set of symbols with unique identifiers
- add(*symbols)#
Add a series of symbols to the library
- Return type:
- Returns:
current SymbolSpec
- merge(*specs)#
Merge a series of SymbolSpec`s into a new `SymbolSpec
- Parameters:
specs (
SymbolSpec) – series of SymbolSpecs- Return type:
- Returns:
new SymbolSpec
Payload types#
- ipyelk.elements.shapes.SymbolId: str#
The
identifierof a symbol in the diagram’sdefs. It must not be empty.
- ipyelk.elements.shapes.PathData: str#
The
dattribute of an SVGpath:Mormand 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
boolraisesValidationError.
Base classes#
- class ipyelk.elements.shapes.BaseShape(**data)#
The base class of all shapes.
- Fields:
type(strorNone): the type string, which selects the renderer. Each subclass sets its own default.Noneselects the default renderer.
A subclass whose
typedefault is a string that differs from thetypedefault of its parent adds itself tojson_classeswhen 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 thetypeof 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 injson_classes, its definition warns with aUserWarningand the second class replaces the first, unless it setsloads_from_jsontoFalse. 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,widthorheightto serialize.keyis the name of the field. A subclass can calculate the value from other fields, asCircledoes.- 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#
Falsekeeps the class out ofjson_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
Noneby default: x,y(float): a position in pixels in the element. OnlyCircle,EllipseandSVGuse them.width,height(float): a size in pixels. The element serializes this size if it has nowidthorheightof 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 givesuseits own meaning and type. A custom shape type can give it any meaning. With the type string of another class, for exampleNodeShape(type="node:path"),usehas the type of that class, herePathData.delay(int): a time in milliseconds. OnlyWidgetuses it.
- Fields, all
- 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 asPoint(x=-1, y=0).- Fields:
x,y(float, default0)
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.