API Reference¶
What follows is an API reference. If you’d like a more hands-on tutorial, have a look at stretchable by Example.
Nodes¶
- class stretchable.Node(*children: Node, key: str = None, measure: Callable[[Node, SizePoints, SizeAvailableSpace], SizePoints] = None, style: Style = None, **kwargs)¶
A node in a layout.
- Parameters:
*children (
Node) – Any child nodes to add to this node, optionalkey (
str, default:None) – Node identifier that can be used to locate the node in the node tree, optionalmeasure (
Callable[[Node,SizePoints,SizeAvailableSpace],SizePoints], default:None) – A method that is able to measure and return the size of the node during computation of layout, optionalstyle (
Style, default:None) – The style to apply to the node, optional**kwargs – If the
styleparameter is not provided, any additional keyword arguments are passed to a new instance ofStylewhich is assigned to this node, optional
- add(*children: Node) Node¶
Add one or more child nodes and return the node itself (enables chaining of node instantiation, see Building Node Trees).
- compute_layout(available_space: SizeAvailableSpace | tuple[float, float] | None = None, *, use_rounding: bool = False) bool¶
Computes the layout for this node and any child nodes.
- Parameters:
available_space (
Union[SizeAvailableSpace,tuple[float,float],None], default:None) – The available space for the layout. It may be provided asSizeAvailableSpace, as atupleof width and height, or omitteduse_rounding (
bool, default:False) – IfTrue, all positions and dimensions will be rounded to integers.
- Return type:
Trueif layout was computed successfully,Falseotherwise.
Notes
Depending on the nodes, the resulting layout may extend outside
available_space.
- find(address: str) Node¶
Returns the node at the specified address.
Node addresses use a syntax similar to file paths, eg. a leading
/starts from the root of the node tree,./indicates the current location and../steps up a level.Nodes can be identified either by the
Node.key(optional), or the 0-based node index.See Locating Nodes for some examples of how to locate nodes using this method.
- get_box(edge: Edge = Edge.BORDER, *, relative: bool = True, flip_y: bool = False) Box¶
Get the computed layout (position and size) for the node.
See CSS box model for more information.
- Parameters:
edge (
Edge, default:<Edge.BORDER: 'border'>) – The edge for which to get the correspondingBoxrelative (
bool, default:True) – Determines if returned position is relative to parent (ifTrue) or relative to the root (ifFalse)flip_y (
bool, default:False) – Determines if the vertical position (y) is measured from the top (ifFalse), or from the bottom (ifTrue)
- Return type:
The
Boxcorresponding to the provided arguments
- mark_dirty()¶
Marks this node as
dirtymeaning that the layout needs to be recomputed.
- property is_dirty: bool¶
Trueif the layout needs to be (re)computed to get the layout of this node,Falseotherwise.
- class stretchable.Box(x: float, y: float, width: float, height: float) None¶
Represents a rectangle with a position and size.
- Parameters:
x (float) – The horizontal position of the left edge of the box
y (float) – The vertical position of the top (default) or bottom (if using
flip_y = TrueinNode.get_box()) edge of the boxwidth (float) – The width of the box
height (float) – The height of the box
Styles¶
- class stretchable.Style(**kwargs)¶
All
Styleproperties listed below can also be passed as keyword arguments when creating a new instance.Note
The
Styleclass is immutable. To change the style of a node, assign a new Style instance.- property overflow_x: Overflow¶
Controls the desired behavior when content does not fit horizontally inside the parent node.
- property overflow_y: Overflow¶
Controls the desired behavior when content does not fit vertically inside the parent node.
- property scrollbar_width: float¶
How much space (in points) should be reserved for the scrollbars of
Overflow.SCROLLnodes.
- property align_items: AlignItems¶
Used to control how child nodes are aligned.
- property justify_items: JustifyItems¶
Used to control how child nodes are aligned.
- property justify_self: JustifySelf¶
Used to control how the node is aligned.
- property align_content: AlignContent¶
Sets the distribution of space between and around content items.
- property justify_content: JustifyContent¶
Sets the distribution of space between and around content items.
- property gap: SizePointsPercent¶
Sets the gaps (gutters) between rows and columns in a grid layout (default:
0.0).
- property padding: RectPointsPercent¶
Sets the padding area between the content edge and the padding edge (default:
0.0).
- property border: RectPointsPercent¶
Sets the border area between the padding edge and the border edge (default:
0.0).
- property margin: RectPointsPercentAuto¶
Sets the margin area between the border edge and the margin edge (default:
0.0).
- property size: SizePointsPercentAuto¶
Sets the desired width and height of the border box (default:
AUTO).
- property min_size: SizePointsPercentAuto¶
Sets the minimum width and height of the border box (default:
AUTO).
- property max_size: SizePointsPercentAuto¶
Sets the maximum width and height of the border box (default:
AUTO).
- property aspect_ratio: float¶
Sets the desired width-to-height of the border box (default:
None).
- property flex_wrap: FlexWrap¶
Sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked (default:
NO_WRAP).
- property flex_direction: FlexDirection¶
Sets how flex items are placed in the flex container defining the main axis and the direction (normal or reversed) (default:
ROW).
- property flex_grow: float¶
Sets the flex grow factor, which specifies how much of the flex container’s remaining space should be assigned to the flex item’s main size. (default:
0.0).
- property flex_shrink: float¶
Sets the flex shrink factor of a flex item. If the size of all flex items is larger than the flex container, items shrink to fit according to flex-shrink (default:
1.0).
- property flex_basis: LengthPointsPercentAuto¶
Sets the initial main size of the border box of a flex item (default:
AUTO)
- property grid_auto_flow: GridAutoFlow¶
Controls how the auto-placement algorithm works, specifying exactly how auto-placed items get flowed into the grid.
- property grid_template_rows: list[GridTrackSizing]¶
Defines the track sizing functions of the grid rows (default:
None).
- property grid_template_columns: list[GridTrackSizing]¶
Defines the track sizing functions of the grid columns (default:
None).
- property grid_auto_rows: list[GridTrackSize]¶
Specifies the size of an implicitly-created grid row track or pattern of tracks. (default:
None).
- property grid_auto_columns: list[GridTrackSize]¶
Specifies the size of an implicitly-created grid column track or pattern of tracks. (default:
None).
- property grid_row: GridPlacement¶
Specifies a grid item’s size and location within a grid row (default:
AUTO).
- property grid_column: GridPlacement¶
Specifies a grid item’s size and location within a grid column (default:
AUTO).
Options¶
- enum stretchable.style.Display¶
Used to control node visibility and layout strategy.
See display on MDN for more information.
- Members:
NONEFLEXGRIDBLOCK
- enum stretchable.style.Overflow¶
Controls the desired behavior when content does not fit inside the parent node.
See overflow on MDN for more information.
- Members:
VISIBLEHIDDENSCROLLCLIP
- enum stretchable.style.Position¶
The positioning strategy for this node.
This controls both how the origin is determined for the
insetproperty, and whether or not the item will be controlled by flexbox’s layout algorithm.See position on MDN for more information.
Warning
This enum follows the behavior of CSS’s
positionproperty, which can be unintuitive.- Members:
RELATIVEABSOLUTE
Alignment¶
- enum stretchable.style.AlignItems¶
Used to control how child nodes are aligned.
For Flexbox it controls alignment in the cross axis. For Grid it controls alignment in the block axis.
See align-items on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERBASELINESTRETCH
- enum stretchable.style.JustifyItems¶
Used to control how child nodes are aligned.
Does not apply to Flexbox, and will be ignored if specified on a flex container. For Grid it controls alignment in the inline axis.
See justify-items on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERBASELINESTRETCH
- enum stretchable.style.AlignSelf¶
Used to control how the node is aligned.
Overrides the parent Node’s
AlignItemsproperty. For Flexbox it controls alignment in the cross axis For Grid it controls alignment in the block axisSee align-self on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERBASELINESTRETCH
- enum stretchable.style.JustifySelf¶
Used to control how the node is aligned.
Overrides the parent node
Styleproperty. Does not apply to Flexbox, and will be ignored if specified on a flex child For Grid it controls alignment in the inline axisSee justify-self on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERBASELINESTRETCH
- enum stretchable.style.AlignContent¶
Sets the distribution of space between and around content items.
For Flexbox it controls alignment in the cross axis. For Grid it controls alignment in the block axis.
See align-content on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERSTRETCHSPACE_BETWEENSPACE_EVENLYSPACE_AROUND
- enum stretchable.style.JustifyContent¶
Sets the distribution of space between and around content items.
For Flexbox it controls alignment in the main axis. For Grid it controls alignment in the inline axis.
See justify-content on MDN for more information.
- Members:
STARTENDFLEX_STARTFLEX_ENDCENTERSTRETCHSPACE_BETWEENSPACE_EVENLYSPACE_AROUND
Flexbox¶
- enum stretchable.style.FlexWrap¶
Controls whether flex items are forced onto one line or can wrap onto multiple lines.
See flex-wrap on MDN for more information.
- Members:
NO_WRAPWRAPWRAP_REVERSE
- enum stretchable.style.FlexDirection¶
The direction of the flexbox layout main axis.
There are always two perpendicular layout axes: main (or primary) and cross (or secondary). Adding items will cause them to be positioned adjacent to each other along the main axis. By varying this value throughout your tree, you can create complex axis-aligned layouts.
Items are always aligned relative to the cross axis, and justified relative to the main axis.
See flex-direction on MDN for more information.
- Members:
ROWCOLUMNROW_REVERSECOLUMN_REVERSE
Grid¶
- enum stretchable.style.GridAutoFlow¶
Controls whether grid items are placed row-wise or column-wise, and whether the sparse or dense packing algorithm is used.
The “dense” packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.
See grid-auto-flow on MDN for more information.
- Members:
ROWCOLUMNROW_DENSECOLUMN_DENSE
- enum stretchable.style.GridIndexType¶
- Members:
AUTOINDEXSPAN
- class stretchable.style.GridTrackSize¶
- class stretchable.style.GridTrackSizing¶
- class stretchable.style.GridPlacement¶
Geometry¶
The stretchable.style.geometry module describes length, size (2 lengths, typically width and height) and rectangles (4 lengths). For each of these, different scales (eg. points, percentages, etc.) are available.
All of the classes have different variants which limit the allowed scales for a specific context/setting on the Style class.
Scale¶
- enum stretchable.style.geometry.Scale¶
All available length scales/settings (support is context-dependent).
- Members:
AUTOPOINTSPERCENTMIN_CONTENTMAX_CONTENTFIT_CONTENT_POINTSFIT_CONTENT_PERCENTFLEX
- enum stretchable.style.geometry.Points¶
- Members:
POINTS
- enum stretchable.style.geometry.PointsPercent¶
- Members:
POINTSPERCENT
- enum stretchable.style.geometry.PointsPercentAuto¶
- Members:
AUTOPOINTSPERCENT
- enum stretchable.style.geometry.AvailableSpace¶
- Members:
DEFINITEMIN_CONTENTMAX_CONTENT
- enum stretchable.style.geometry.MinTrackSize¶
- Members:
POINTSPERCENTMIN_CONTENTMAX_CONTENTAUTO
- enum stretchable.style.geometry.MaxTrackSize¶
- Members:
POINTSPERCENTMIN_CONTENTMAX_CONTENTFIT_CONTENT_POINTSFIT_CONTENT_PERCENTAUTOFLEX
Length¶
- class stretchable.style.geometry.Length(scale: Scale, value: float) Length¶
Represents a length. For some values of Scale,
valueis not applicable.It is recommended to use the static constructors on the different variants of Length, instead of using the constructor directly.
Todo
Add documentation for Length constructors and variants.
Size¶
- class stretchable.style.geometry.Size(*values: Length, width: Length = None, height: Length = None) Size¶
Represents two lengths (typically the size of a rectangle eg. width and height).
Todo
Add documentation for Size constructors and variants.
Rect¶
- class stretchable.style.geometry.Rect(*values: Length, top: Length = None, right: Length = None, bottom: Length = None, left: Length = None) Rect¶
Represents four lengths.
Todo
Add documentation for Rect constructors and variants.
Exceptions¶
- exception stretchable.exceptions.NodeLocatorError¶
- exception stretchable.exceptions.NodeNotFound¶
- exception stretchable.exceptions.LayoutNotComputedError¶
Todo
Add documentation for exceptions.