Widgets#

Diagram Widget#

class ipyelk.Diagram(*args: t.Any, **kwargs: t.Any)#

An Elk diagramming widget to help coordinate the Viewer and Pipe

Attributes#

source: MarkElementWidget

input source to the diagram’s processing pipe. A source feeds one open diagram: a source of another open diagram is refused with TraitError; source.diagram is that diagram. A diagram releases its source when it closes or gets a new one.

pipe: Pipe

processing pipe (that may contain sub-pipes). Pipes perform various tasks like adding x/y and width/height layouts or calculating text label sizes. The diagram owns its pipe and every pipe nested in it. A new pipe, or a stage added to a pipeline’s pipes, is refused if any pipe in it is closed, owned by another open diagram, or part of the current pipe. A stage removed from pipes is released, not closed. Replacing the pipe or closing the diagram closes the pipe, its sub-pipes and the widgets they created (such as status views), but never source or an inlet/outlet passed in.

view: Viewer

output view that will render the pipe outlet; closed with the diagram. Pass it at construction: assigning a different view, or giving a closed view or a view of another open diagram, raises TraitError.

tools: tuple Tool

list of tools that a user of the diagram might use to manipulate the state of the diagram; closed with the diagram. Tools and the toolbar belong to one open diagram. Remove a tool from tools before registering it on another diagram; give each diagram its own toolbar.

symbols: SymbolSpec

additional shape definitions that can be used in rendering the diagram. For example unique arrow head shapes or custom node shapes.

A closed diagram (closed is True) raises ClosedError on refresh(), register_tool() and on setting source, pipe, view, tools, toolbar, symbols, style, layout or children.

A construction that a check refuses (TraitError) leaves the source, view and pipe that it was given open. A construction that fails after the checks, for example in a tool’s on_done, closes the diagram as close() does, with the view and pipe that it was given. Tools and the toolbar close only after the diagram has claimed them; a refused construction never closes another diagram’s widgets.

close()#

Close the diagram with its pipe, view, tools and toolbar.

source and any inlet or outlet passed to a pipe stay open. Closing again does nothing.

property closed: bool#

Whether close() was called; a closed diagram refuses calls.

get_tool(tool_type)#

Get the tool that matches the given Tool type.

Parameters:

tool_type (Type[Tool]) – get specific tool instance based on matched type.

Return type:

Tool

refresh(sender=None)#

Create asynchronous refresh task which will update the view given any changes.

sender is ignored; it lets refresh serve as a tool’s on_done callback (which receives the tool). Returns None when no event loop is running (see Pipe.schedule_run). The task resolves cancelled when the diagram is closed mid-run, so awaiting it then raises CancelledError.

Return type:

Task | None

register_tool(tool)#

Add a new tool to the diagram.

Each name in the tool’s _dependencies that the diagram supplies is bound: diagram, pipe, view and the view’s selection, hover, viewport and painter. Any other dependency stays as it is, and the tool names it in missing_dependencies() while it is None. A progress bar stops getting the reports of a pipe from another diagram.

Parameters:

tool (Tool) – new tool instance to add to the diagram.

Return type:

Diagram

Returns:

current Diagram instance

source#

Syncs Elk JSON Elements

Viewer Widget#

class ipyelk.diagram.Viewer(*args: t.Any, **kwargs: t.Any)#

Generic Viewer of ELK JSON diagrams. Currently only mainly used by SprottyViewer

Attributes#

parameter source:

MarkElementWidget input source for rendering.

parameter selection:

Selection maintains selected ids and methods to resolve the python elements.

parameter hover:

Hover maintains the hovered id.

parameter viewport:

Viewport the camera (origin, zoom, canvas size, viewed ids) of the most recently reporting view, written by the browser. Move a view with set_viewport().

parameter painter:

Painter temporary, view-only CSS classes per element id, applied in every view without touching the model.

parameter control_overlay:

ControlOverlay or None additional JupyterLab widgets that can be rendered on top of the diagram based on the current selected states. Opt-in: None (the default) and an overlay without children render nothing.

parameter fit_on_first_layout:

bool opt-in (default False): each view fits the whole diagram, in the browser, after its first layout, so a diagram displayed after it was laid out (when a fit() message would have reached no view) does not open at 1:1 in the top-left corner. A view that is hidden then (a collapsed accordion, a background tab) fits the first time it is shown. Each view fits once: later layouts (a new source included), reveals and resizes keep its camera, and a set_viewport sent while it waits to be shown cancels its fit; a fit() or center() sent then has no canvas to work on, and the first fit replaces it. Set it before a view’s first layout: a view that was already laid out is not fitted when it is turned on.

parameter fit_padding:

float padding around the diagram, in diagram units (scaled by the zoom on screen), for the first-layout fit and for fit() calls that pass no padding (default 0).

parameter fit_max_zoom:

float or None zoom limit for the first-layout fit and for fit() calls that pass no max_zoom; None (the default) leaves only the viewer’s own limit (10), so a small diagram fills the view. A value above 10 acts as 10.

parameter cull_offscreen:

bool opt-in (default False): each view renders only the elements that touch its canvas, which keeps selection, hover and panning cheap on a large diagram that is zoomed in. The SVG in the page is then not the whole diagram when the view is zoomed in, so code that reads the browser DOM must fit the view first or stay with the default. An Exporter is unaffected either way: it renders its export from the model, so it is complete at any zoom.

center(model_ids=None, animate=None, retain_zoom=None)#

Center the view on model_ids (the whole diagram if None).

A str is one id, never a sequence of characters. The generic viewer has no viewport of its own, so this does nothing; subclasses that render (SprottyViewer) implement it with the same signature.

Return type:

None

close()#

Close the viewer and its tools, not source or control_overlay.

fit(model_ids=None, animate=None, max_zoom=None, padding=None)#

Pan/zoom the view to focus on model_ids (the whole diagram if None).

A str is one id, never a sequence of characters. The generic viewer has no viewport of its own, so this does nothing; subclasses that render (SprottyViewer) implement it with the same signature.

Return type:

None

pan#

Removed in 3.0; raises DeprecatedAPIError throughout 3.x. Viewer.pan was removed in ipyelk 3.0: the Pan tool was a placeholder that nothing ever wrote. Read viewer.viewport.origin and viewer.viewport.canvas_size (the browser’s latest report, None until one arrives) and move a view with viewer.set_viewport(origin=(x, y)). No alias: this error is raised throughout 3.x.

viewed#

Removed in 3.0; raises DeprecatedAPIError throughout 3.x. Viewer.viewed was removed in ipyelk 3.0: nothing ever wrote it. Read viewer.viewport.viewed_ids (the ids whose bounds touch the reporting view’s visible rectangle; None until the browser reports). No alias: this error is raised throughout 3.x.

zoom#

removed names raise DeprecatedAPIError through 3.x

class ipyelk.diagram.SprottyViewer(*args: t.Any, **kwargs: t.Any)#

JupyterLab widget for displaying and interacting with views generated from ELK JSON.

Setting the instance’s value traitlet to valid Eclipse Layout Kernel JSON will call the elkjs layout method and display the returned mark_layout using sprotty.

center(model_ids=None, animate=None, retain_zoom=None)#

Center Diagram View on specified model ids

Parameters:
  • model_ids (str | Sequence[str] | None) – one elk model id, or a sequence of them; None (default) centers the whole diagram

  • animate (bool | None) – specify is the view animates to the given marks

  • retain_zoom (bool | None) – specify if the current zoom level is maintained

Return type:

None

fit(model_ids=None, animate=None, max_zoom=None, padding=None)#

Pan/Zoom the Diagram View to focus on particular model ids

Parameters:
  • model_ids (str | Sequence[str] | None) – one elk model id, or a sequence of them; None (default) fits the whole diagram

  • animate (bool | None) – specify is the view animates to the given marks

  • max_zoom (float | None) – specify if the max zoom level; None (default) uses fit_max_zoom

  • padding (float | None) – specify if the viewport padding around the marks; None (default) uses fit_padding

Return type:

None

set_viewport(*, origin=None, zoom=None, animate=True, view_id=None)#

Move the camera of the connected view(s).

A command, not state: the browser applies it and then reports the resulting camera through viewport.

Parameters:
  • origin (tuple[float, float] | None) – diagram coordinates for the canvas top-left corner; None keeps the view’s current origin

  • zoom (float | None) – CSS pixels per diagram unit; None keeps the current zoom

  • animate (bool) – animate the move (default) or jump

  • view_id (str | None) – only the view with this id (see ipyelk.tools.Viewport.view_id); None moves every connected view

Return type:

None