Skip to content

auto_layout

panel_flowdash.auto_layout

Automatic placement of nodes on the canvas and of tiles on the dashboard grid.

Models are poor at choosing coordinates, so FlowDash places what is added without a position itself and offers to rearrange a whole dashboard on request. Both algorithms are deterministic and work on plain data, so the editor, the YAML reader and tests share them. The spacing and the rules that decide where a tile goes are the constants below, kept here so they are easy to tweak.

Canvas Nodes go in columns by their depth in the dataflow, sources on the left and views on the right; groups of nodes not wired to each other are laid out on their own and packed side by side. A node without inputs moves right up to the column before the first node it feeds, so a control sits beside the transform it filters rather than with the data sources. Rows within a column are ordered by the barycenter heuristic to reduce edge crossings, and each node is then aligned with the nodes it is wired to.

Tiles Tiles are grouped by kind in this order: controls, KPIs, charts, tables and last the tiles of data sources and transforms, which start hidden when the editor places them. Controls, KPIs and data tiles keep the width of their default_size in whole columns and are packed into rows; controls and KPIs share a row only when they all fit in it. Charts fill a grid of two columns, an odd one out taking at most twice its default width, and tables take the full width. Text tiles (headings, notes) stay in front of the tile they preceded, so a heading keeps titling its section. The grid moves a tile up into any gap it fits, so the room a row of packed tiles leaves is filled on purpose: by the leading heading in the first row, else by the chart or table that follows, else by widening the row's tiles to at most twice their width. Tiles in a row share one height. The grid generates the layouts of narrower breakpoint bands from the arranged one; a band with a layout of its own gets new tiles inserted at the band's width.

ALIGN_PASSES = 3 module-attribute

ARRANGED_KEYS = frozenset({'instance_id', 'index', 'width', 'height'}) module-attribute

BLOCK_GAP = 120 module-attribute

COLUMN_GAP = 120 module-attribute

FILLER_MIN_COLUMNS = 6 module-attribute

GRID_COLUMNS = 12 module-attribute

HEADING_MIN_COLUMNS = 4 module-attribute

HIDDEN_KINDS = frozenset({'data'}) module-attribute

KPI_MAX_WIDTH = 320 module-attribute

KPI_TAGS = frozenset({'kpi', 'metric', 'indicator'}) module-attribute

MAX_WIDENING = 2 module-attribute

NARROWEST_WIDTH = 360 module-attribute

ORDER_SWEEPS = 6 module-attribute

PACKED = {'control': 'top', 'kpi': 'top', 'data': 'data'} module-attribute

Point = tuple[float, float] module-attribute

ROLE_COLUMN = {'source': 0.0, 'widget': 0.0, 'transform': 0.5, 'view': 1.0, 'other': 1.0} module-attribute

ROLE_COLUMN_UNWIRED = {'source': 0, 'widget': 1, 'transform': 2, 'view': 3, 'other': 3} module-attribute

ROW_GAP = 40 module-attribute

SHELF_ASPECT = 1.6 module-attribute

TABLE_TAGS = frozenset({'table'}) module-attribute

TEXT_TAGS = frozenset({'text', 'heading', 'markdown', 'notes', 'divider'}) module-attribute

TILE_KINDS = ('control', 'kpi', 'chart', 'table', 'data') module-attribute

TILE_MARGIN = 10 module-attribute

TILE_MIN_WIDTH = {'control': 240, 'kpi': 200, 'chart': 420, 'data': 280} module-attribute

TILE_WIDTH = {'control': 360, 'kpi': 280, 'data': 360} module-attribute

WHOLE_ROW_KINDS = frozenset({'table', 'text'}) module-attribute

NodeBox dataclass

A node to place: its id, its card's size in flow units and its role.

height instance-attribute

id instance-attribute

role = 'other' class-attribute instance-attribute

width instance-attribute

TileBox dataclass

A tile to lay out: its id, kind and the size hints of its component, in pixels.

default_size = field(default_factory=dict) class-attribute instance-attribute

id instance-attribute

kind instance-attribute

lead = False class-attribute instance-attribute

max_size = field(default_factory=dict) class-attribute instance-attribute

min_size = field(default_factory=dict) class-attribute instance-attribute

_Row dataclass

columns instance-attribute

free property

keep = False class-attribute instance-attribute

ragged = False class-attribute instance-attribute

tiles instance-attribute

_arrange_groups(nodes, edges, origin, initial)

Lay out each connected group of nodes on its own and pack the groups in shelves.

Groups not wired to each other go side by side, left to right, starting a new shelf below once a shelf is about :data:SHELF_ASPECT times as wide as all the groups would be tall stacked into a square.

_arrange_layers(nodes, edges, origin, initial)

_below(row, tiles, j, width)

Whether no tile after tiles[j] fits in the room under the packed tiles of row.

With tiles[j] beside them in a taller row, the grid would move such a tile up under them.

_bounded_height(tile, height)

_carve(slots, rect)

Return the free rectangles of slots once rect is taken, as the grid's packing does.

_chart_rows(tiles, width)

Put charts two to a row where two fit, the odd one out on a row of its own.

A chart on its own takes at most twice its default width.

_close_gaps(rows, width)

Widen the tiles of a row of packed tiles that a later tile would otherwise move into.

The grid moves a tile up into any gap it fits.

_column_bounds(tile, width)

_columns(tile, width)

Return the columns a tile takes at a grid width: its default width, within its bounds.

_components(ids, preds, succs)

_crossings(layers, column, preds)

Count the crossings between edges that join the same two columns.

_entry(tile, width)

Return the entry of a tile laid out on its own.

_family(tile)

_finish(row, tile, width)

Put tile, a chart or table, into the room left in a row of packed tiles, if it fits.

_free_below(x, y, box, placed)

Return the first y from y down at which box at x overlaps none of placed.

_graph(ids, edges)

_ordered(tiles)

Return the leading text tiles and the rest in display order.

Kinds go in :data:TILE_KINDS order; a text tile stays in front of the tile that followed it, and text after the last other tile stays last.

_overlaps(a, b)

Whether two (x, y, width, height) boxes come closer than the row gap.

_pack(layer, desired, height)

Return tops for layer, in its order and ROW_GAP apart, as close to desired as allowed.

_pack_tiles(tiles, width)

Fill rows left to right with tiles at their own width.

_pairs_charts(tiles, j)

Whether the charts from tiles[j] on start with a pair, in an even run.

_pairs_with(tile, before, entry, width)

Whether a new chart tile can share the row of the chart before it, both half wide.

Only a chart alone in its row, at full width, that the editor laid out (before is its box) makes room, and only when both fit in half the row.

_percent(pixels, width)

_rows_of(lead, tiles, width)

Group tiles in display order into the rows they are shown in.

Returns the leading text that still needs rows of its own, and the rows. A single leading heading finishes the first row of packed tiles; the last row of a run of packed tiles takes the chart or table after it into the room it leaves.

_settle_rows(entries, start, before, free, least, bounds, whole)

Leave no row from start on that changed part empty: take up the next tile or widen.

_spread(row, width)

Widen the tiles of row a column at a time, to at most twice their own width.

_topological(ids, preds, succs)

Order ids so every node follows its inputs, breaking any cycle at the earliest node.

_widen_to(row, total)

Widen every tile of row by a column at a time, alike, until they take total columns.

_widest(tile, width)

Return the most columns a chart or table takes: twice its default width, within its bounds.

_width(columns)

arrange_graph(nodes, edges, origin=(0.0, 0.0), initial=None)

Lay out every node by its dataflow and return its top-left position.

The layout's top-left corner is at origin. initial gives a starting row order per node, e.g. its current y on the canvas. Nodes without any edge, e.g. headings, go in a row below the wired ones, unless no node is wired, when they go in columns by role.

arrange_tiles(tiles, width, previous=None)

Lay out tiles, given in their current order, for a grid width pixels wide.

Returns one layout entry per tile, in display order, with the tile's instance_id, its index, its width in percent of the row and the height in pixels of the tallest tile of its row; a row without height hints gets none, so the grid measures its content. Controls, KPIs and the tiles of sources and transforms get the width of their default_size in whole columns, and a chart or table after them fills the room left in their last row at its own height. previous gives each tile's current entry, whose keys other than :data:ARRANGED_KEYS are kept.

assign_columns(nodes, edges)

Return the column of each node, 0 for the leftmost.

band_widths(breakpoints, labels)

Return the width each breakpoint band is laid out for: the narrowest it gets.

insert_tiles(layout, order, kinds, new, width, previous=None, resizable=None, leading=())

Add entries for the new tiles to a layout, leaving the other tiles' sizes alone.

No new tile goes in front of the tiles of leading, the dashboard's titles, at the start of the order.

order lists the shown tiles in display order and kinds gives the kind of each. A new tile goes after the last tile of its kind, or else before the first tile of a later kind, at the size of the tile of its kind before it when their row has room (a packed tile keeps its own width), else in a row of its own. A new chart after a chart alone in its row shares that row, both half wide, when the chart before it is one of resizable, the tiles the editor laid out itself, by id. The other entries change only when a tile goes in among them, and then only their index, or when such a chart makes room. previous gives entries the new tiles had, whose keys other than :data:ARRANGED_KEYS are kept.

layout_rows(entries)

Group the entries of the shown tiles, in display order, into the rows they fill.

The grid fills a row left to right and starts the next one with the first tile that does not fit in the room left.

order_rows(nodes, edges, column, initial=None)

Order the nodes of each column to reduce edge crossings, top to bottom.

initial gives a starting key per node, e.g. its current y, ties and nodes without one keeping the order of nodes.

pack_rows(entries)

Group the shown tiles' entries into rows as the grid packs them in its "rows" mode.

Like :func:layout_rows, except that a tile that no longer fits beside the tiles at the top of its row may drop into the room left below a shorter one of them, beside a taller tile, as the grid does; it never moves into an earlier row. Each entry comes with the position in entries of the tile it sits under, None for one at the top of its row. Tiles without a height never stack, nor does anything under them. Heights count the tiles' margins, as the grid's packing does.

place_nodes(nodes, edges, fixed)

Return positions for the nodes not in fixed, leaving the fixed ones where they are.

A group of new nodes that is not wired to any fixed node is laid out on its own, below everything on the canvas. A new node wired to fixed nodes goes in the column its depth gives it, measured from the fixed nodes around it, at the height of the nodes it is wired to, and moves up or down only as far as it must to overlap nothing.

resize_in_row(entries, tile_id, width=None, height=None, fixed=(), bounds=None, whole=(), fixed_heights=None)

Resize one tile of a layout and rebalance the other tiles of its row, as dragging its edge does.

entries are the shown tiles' entries in display order. A wider tile takes the room its row has left, then shrinks the row's other tiles, those after it first, down to their narrowest in bounds (percent by tile id); it is held at the width they leave. A tile that takes the whole row moves the others to the next row instead. A narrower tile in a full row leaves its room to the tiles of the next row that fit in it, as the grid moves them up, and the other tiles of its row widen alike to fill what is left.

No row the change moved tiles in or out of is left part empty: it takes the next row's first tile when every tile still fits at an equal share of the row, else its tiles widen to fill it. The tiles of whole, e.g. tables, keep a row of their own. A height goes to the row's other tiles too, so the row stays aligned. The tiles of fixed, given a width on purpose, keep their width, and those of fixed_heights (fixed when None) their height. Returns copies; the resized tile's entry holds the width it got.

stack_tiles(tiles, previous=None)

Lay out tiles one to a row at full width, in the order given, each at its default height.

tile_kind(role, tags=(), default_size=None, text=False)

Return how a tile is laid out, one of :data:TILE_KINDS or "text".

role is the component's role in the palette (see :func:panel_flowdash.ui.roles.component_role); text marks the built-in text components.

width_bounds(tile, width)

Return the narrowest and widest a tile is laid out, in percent of a row width pixels wide.