Skip to main content

A Reflex custom component wrapping @gravity-ui/graph (a high-performance Canvas + React graph/flowchart/node-editor visualization library).

Project description

reflex-gravityui-graph

A Reflex custom component that wraps @gravity-ui/graph — a high-performance graph / flowchart / node-editor visualization library that renders the full graph on Canvas when zoomed out (smooth with thousands of nodes) and swaps to HTML/React for rich interactions when zoomed in.

It exposes the whole graph declaratively from Python: pass blocks, connections, settings, colors and constants as plain data, and receive selection / connection / camera events back into your Reflex state.

Status: 0.1.0 (beta). Wraps @gravity-ui/graph@1.11.0.


Installation

uv add reflex-gravityui-graph
# or
pip install reflex-gravityui-graph

The first time the demo (or your app) runs, Reflex installs the npm dependency @gravity-ui/graph automatically.

Quick start

import reflex as rx
from reflex_gravityui_graph import graph_canvas, Block, Connection, Anchor, EAnchorType

a = Block(id="a", x=0, y=0, name="Start",
          anchors=[Anchor(id="out", block_id="a", type=EAnchorType.OUT)])
b = Block(id="b", x=320, y=180, name="End",
          anchors=[Anchor(id="in", block_id="b", type=EAnchorType.IN)])
edge = Connection(source_block_id="a", source_anchor_id="out",
                  target_block_id="b", target_anchor_id="in")


def index() -> rx.Component:
    return graph_canvas(
        blocks=[a.to_dict(), b.to_dict()],
        connections=[edge.to_dict()],
        width="100%",
        height="600px",
    )


app = rx.App()
app.add_page(index)

GraphCanvas fills its container, so always give the component an explicit height (height="600px") or a sized parent.

Why a JSX bridge?

@gravity-ui/graph's React API is hook + imperative: you create a Graph with the useGraph hook, push entities with setEntities, and start() the render loop once the canvas is attached. Reflex components can't call React hooks directly, so this package ships a tiny local bridge (graph_canvas.jsx) that encapsulates that lifecycle and presents a flat, declarative, JSON-serializable prop surface. See docs/sdd/02-architecture.md.

API

graph_canvas(...) / GravityGraph

Prop Type Description
blocks list[dict] Nodes (id, is, x, y, width, height, name, optional anchors, selected, group, meta).
connections list[dict] Edges (sourceBlockId/targetBlockId, optional anchor ids, label, styles, dashed, selected).
settings dict TGraphSettingsConfig (bezier, arrows, labels, drag/zoom, anchors, …).
colors dict RecursivePartial<TGraphColors> view colors.
constants dict RecursivePartial<TGraphConstants> view constants.
config_name str Underlying graph configuration name.
auto_start bool Start the render loop on attach (default True).
zoom_on_start bool Fit camera to graph on attach (default True).
zoom_target str | list "center", a rect, or a list of block ids.
zoom_padding int Padding (px) when fitting on start.
render_html_blocks bool Render rich HTML/React blocks at detailed zoom.

Events (each delivers one plain dict to your handler):

on_block_selection_change, on_connection_created, on_connection_selection_change, on_camera_change, on_block_drag_end, on_state_change.

Domain value objects

Block, Connection, Anchor, EAnchorType, GraphData build and validate the entity payloads in Python (duplicate-id and dangling-edge checks included).

Recipe helpers (reproduce the upstream Storybook stories)

one_block, two_connected_blocks, colored_connections, block_with_anchors, generate_layered_blocks (hundred / thousand / five-thousand block graphs), grouped_graph, default_settings, and the THEMES color presets.

Demo app

A full demo with one page per example category lives in reflex_gravityui_graph_demo/:

uv pip install -e .
cd reflex_gravityui_graph_demo
reflex run

Pages: Basics · Connections · Scale · Grouping · Camera & Zoom · Theming · Interaction & Events.

Development

This project uses uv.

uv venv && source .venv/bin/activate
uv pip install -e ".[dev]"
uv run pytest -q                  # run the test suite
uv run reflex component build     # build wheel/sdist + generate the .pyi stub
uv publish                        # publish to PyPI (needs an API token)

See docs/sdd/ for the full Spec-Driven Design artifacts (overview, PRD, architecture, component spec, plan, tasks) and docs/research/ for the upstream research notes.

Credits

License

Apache-2.0 © Ernesto Crespo. @gravity-ui/graph is MIT © YANDEX LLC.

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

reflex_gravityui_graph-0.1.0.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

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

reflex_gravityui_graph-0.1.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file reflex_gravityui_graph-0.1.0.tar.gz.

File metadata

  • Download URL: reflex_gravityui_graph-0.1.0.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for reflex_gravityui_graph-0.1.0.tar.gz
Algorithm Hash digest
SHA256 854cc814ae92ef93cb822b4cef411c5c01b6e490d496ba432580658e4d0cc03b
MD5 bdc7831a424de2d87ce0b38fa6155e72
BLAKE2b-256 c41fab1ef7ab61357da75f69478365f2be50ee9caee09db5f274012a98927b20

See more details on using hashes here.

File details

Details for the file reflex_gravityui_graph-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for reflex_gravityui_graph-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5da166b2f8281e4238ad90cda895c374801caee11ebd7330d215e094c991a21a
MD5 37ddda01ece1d314ae161e1859001d50
BLAKE2b-256 be6970af5b767f8db2656378fa96a6ec5e0ef0e8d67a671f29a98adb1b23dc78

See more details on using hashes here.

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