Skip to main content

Python bindings for Facebook Yoga layout engine (CSS Flexbox)

Project description

yoga-python

Python bindings for Facebook's Yoga layout engine — a cross-platform implementation of CSS Flexbox.

Yoga computes a layout tree of nodes with flexbox styles and outputs pixel-perfect positions. This package wraps the full C++ engine via nanobind, giving you the same layout behavior you get in React Native, Litho, and other Yoga-backed frameworks — directly from Python.

Installation

pip install yoga-python

Building from source requires a C++20 compiler, CMake 3.15+, and nanobind:

pip install scikit-build-core nanobind
pip install .

Quick start

from yoga import (
    Node, Config, Direction, FlexDirection, Justify,
    Align, Edge, YGValuePoint, YGValuePercent,
)

root = Node()
root.flex_direction = FlexDirection.Row
root.width = YGValuePoint(300)
root.height = YGValuePoint(200)
root.set_padding(Edge.All, 10)

child_a = Node()
child_a.flex_grow = 1
child_a.set_margin(Edge.Right, 10)
root.insert_child(child_a, 0)

child_b = Node()
child_b.flex_grow = 2
root.insert_child(child_b, 1)

root.calculate_layout(300, 200, Direction.LTR)

print(f"child_a: {child_a.layout_left}, {child_a.layout_top}, "
      f"{child_a.layout_width}x{child_a.layout_height}")
# child_a: 10.0, 10.0, 90.0x180.0

print(f"child_b: {child_b.layout_left}, {child_b.layout_top}, "
      f"{child_b.layout_width}x{child_b.layout_height}")
# child_b: 110.0, 10.0, 180.0x180.0

API overview

Node

The primary object. Create nodes, configure their styles, build a tree, then call calculate_layout.

node = Node()           # default config
node = Node(config)     # with explicit config

Style properties (set directly as attributes):

Property Type Example
flex_direction FlexDirection node.flex_direction = FlexDirection.Row
justify_content Justify node.justify_content = Justify.Center
align_items Align node.align_items = Align.Stretch
align_self Align node.align_self = Align.FlexEnd
align_content Align node.align_content = Align.SpaceBetween
flex_wrap Wrap node.flex_wrap = Wrap.Wrap
overflow Overflow node.overflow = Overflow.Hidden
display Display node.display = Display.Flex
position_type PositionType node.position_type = PositionType.Absolute
flex_grow float node.flex_grow = 1.0
flex_shrink float node.flex_shrink = 0.0
flex_basis YGValue node.flex_basis = YGValuePoint(100)
width / height YGValue node.width = YGValuePercent(50)
min_width / max_width YGValue node.min_width = YGValuePoint(80)
min_height / max_height YGValue node.max_height = YGValuePoint(400)
aspect_ratio float node.aspect_ratio = 16 / 9
box_sizing BoxSizing node.box_sizing = BoxSizing.ContentBox
direction Direction node.direction = Direction.LTR

Edge-based properties (margin, padding, border, position):

node.set_margin(Edge.Left, 10)           # points
node.set_margin(Edge.Top, YGValuePercent(5))  # percent
node.set_padding(Edge.All, 20)
node.set_border(Edge.Bottom, 1)
node.set_position(Edge.Left, YGValuePoint(50))

Tree manipulation:

node.insert_child(child, index)
node.remove_child(child)
node.remove_all_children()
node.set_children([child_a, child_b])
node.child_count                     # int
node[index]                          # Node (supports indexing)

Layout:

node.calculate_layout(width, height, Direction.LTR)

node.layout_left       # float
node.layout_top        # float
node.layout_width      # float
node.layout_height     # float
node.layout_direction  # Direction
node.layout_margin(Edge.Left)   # float
node.layout_padding(Edge.Top)   # float
node.layout_border(Edge.Right)  # float

Measure functions for leaf nodes with custom content sizing:

def measure(node, width, width_mode, height, height_mode):
    return {"width": 100, "height": 50}

node.set_measure_func(measure)

Baseline functions for custom baseline alignment:

def baseline(node, width, height):
    return height * 0.8

node.set_baseline_func(baseline)

Config

config = Config()
config.use_web_defaults = True
config.point_scale_factor = 2.0
config.errata = Errata.StretchFlexBasis

Values

YGValuePoint(100)       # 100px
YGValuePercent(50)      # 50%
YGValueAuto             # auto
YGValueUndefined        # undefined

Enums

All Yoga enums are available as Python enum types (nanobind enums, int-convertible):

Direction, FlexDirection, Justify, Align, PositionType, Wrap, Overflow, Display, Edge, Unit, MeasureMode, Dimension, BoxSizing, Gutter, Errata, NodeType, LogLevel, ExperimentalFeature

Development

git clone https://github.com/banditburai/yoga-python.git
cd yoga-python
uv sync --dev

# Run tests
uv run pytest tests/ -v

# Lint & type checking
uv run ruff check
uv run ruff format --check
uv run pyright

Test coverage

The test suite is a 1:1 port of the Yoga C++ test suite — 822 tests covering all layout modes, edge cases, and behaviors. Tests that the C++ suite itself skips (GTEST_SKIP) are also skipped in Python.

License

MIT — same as Yoga.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yoga_python-0.1.3.tar.gz (105.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

yoga_python-0.1.3-cp313-cp313-win_amd64.whl (168.7 kB view details)

Uploaded CPython 3.13Windows x86-64

yoga_python-0.1.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (201.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

yoga_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (143.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

yoga_python-0.1.3-cp312-cp312-win_amd64.whl (168.7 kB view details)

Uploaded CPython 3.12Windows x86-64

yoga_python-0.1.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (201.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

yoga_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (143.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file yoga_python-0.1.3.tar.gz.

File metadata

  • Download URL: yoga_python-0.1.3.tar.gz
  • Upload date:
  • Size: 105.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yoga_python-0.1.3.tar.gz
Algorithm Hash digest
SHA256 85e9c04b16bd4c950759d244a01a2db850f8f39e38c0eaf3476430d4e11dda0f
MD5 4ea4348767f97ba55d7aefbfd1a625d1
BLAKE2b-256 ec14a3642270f780123673296177c0c213a94433ba0d148f9849fc7ef3659902

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3.tar.gz:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: yoga_python-0.1.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 168.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yoga_python-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 548381b01ac8105b6b60cee89c2b40fd3ef34a5009f1997ab815f1bdeb35fff7
MD5 b4cae04fb7f48aa53c54c1cf72279da9
BLAKE2b-256 5d54eeff23f734d5ac909758b8a6b6af0710d73d127857c69e5627a69ea88b4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp313-cp313-win_amd64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for yoga_python-0.1.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 03e149de8242ca036f7f75ada0d7d61d247c543457d1a1e3af64427a1731c8b1
MD5 2c2b90105f88d2efb8f737925aebd76a
BLAKE2b-256 eca59fbd8e730099b955a5e81e6674a5eeb029d8040112cb8c478435848e2c1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for yoga_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e23143fa7676be935c2ad20a5df92b348af9e3c5ff3b9f940d715a6f86b03db7
MD5 4bf83535905049bc3819e653438c5bc6
BLAKE2b-256 f76a4457d66d97852929499998f90df9c2fd5e8e27df924dfd1104111e183519

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: yoga_python-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 168.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for yoga_python-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 11dba2b3db23d9c079c774756b87202395220165129c7052572fcf427e5e1d30
MD5 0d8c2730ff7625cf83dff94fdaa824f6
BLAKE2b-256 6fdb7062b11612438b53fd02c85918d9c2f15050eb10e72df437af6c5236f198

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp312-cp312-win_amd64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for yoga_python-0.1.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 25d9e0bded701797182595ce646d943b9782d2603b3120ab3b92465289b425c2
MD5 f94c7e8ac9a06d95dfc3924f6cbb8544
BLAKE2b-256 8754e0ed6a06b0407a28c29b93c417a3fb3befbdb3d62e08961cd35ad6aa673c

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yoga_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for yoga_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 58faea564ba718b7b367365b21f265593459a9485381ae266ae5e5ef6c9c60bb
MD5 d1342bf0c5b3ee51c4c8384599b866b9
BLAKE2b-256 ca7d2500f6b3d89dd353e8c4ca5f0a389ef7663eab82a17fdad298745f249713

See more details on using hashes here.

Provenance

The following attestation bundles were made for yoga_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on banditburai/yoga-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page