Skip to main content

React Flow graph engine for pytransitions state machines

Project description

transitions_reactflow

A React Flow graph engine extension for the pytransitions state machine library.

Features

  • ✅ React Flow compatible graph data
  • ✅ Hierarchical states with children parameter
  • ✅ Smart filtering of unused states
  • ✅ Unique edge IDs for duplicate transitions
  • ✅ Full type hints with .pyi stub files
  • ✅ Comprehensive test suite

Installation

pip install transitions-reactflow

Or install from source:

git clone https://github.com/johnsonlm/transitions-reactflow
cd transitions-reactflow
pip install -e .

Quick Start

from transitions_reactflow import ReactFlowMachine

# Define states with hierarchy
states = [
    'idle',
    {'name': 'processing', 'children': ['validating', 'payment']},
    'completed'
]

# Define transitions
transitions = [
    {'trigger': 'start', 'source': 'idle', 'dest': 'processing_validating'},
    {'trigger': 'validate', 'source': 'processing_validating', 'dest': 'processing_payment'},
    {'trigger': 'finish', 'source': 'processing_payment', 'dest': 'completed'}
]

# Create machine and get graph data
machine = ReactFlowMachine(states=states, transitions=transitions, initial='idle')
graph_data = machine.get_graph()

# Returns: {'nodes': [...], 'edges': [...]}

Usage

With Flask Backend

from flask import Flask, jsonify
from flask_cors import CORS
from transitions_reactflow import ReactFlowMachine

app = Flask(__name__)
CORS(app)  # Enable CORS for frontend requests

machine = ReactFlowMachine(states=states, transitions=transitions, initial='idle')

@app.route('/graph-data')
def get_graph_data():
    return jsonify(machine.get_graph())

With React Frontend

import ReactFlow from 'reactflow';

function StateMachineFlow() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('/graph-data').then(res => res.json()).then(setData);
  }, []);

  return data && <ReactFlow nodes={data.nodes} edges={data.edges} fitView />;
}

Hierarchical States

Use children for nested states:

states = [
    'idle',
    {'name': 'error', 'children': ['validation', 'payment', 'network']}
]
# Creates: error, error_validation, error_payment, error_network

Note: Unused parent states are automatically filtered from the graph.

API

ReactFlowMachine

Extends transitions.extensions.GraphMachine.

  • get_graph(title=None, roi_state=None){'nodes': [...], 'edges': [...]}
  • add_states(states) - supports hierarchical definitions
  • All standard pytransitions methods

ReactFlowGraph

Extends transitions.extensions.diagrams_base.BaseGraph.

  • get_graph(title=None, roi_state=None) → React Flow data
  • Standard BaseGraph methods

Graph Data Format

The get_graph() method returns React Flow compatible data:

Nodes:

{
    "id": "state_name",
    "data": {"label": "state_name"},
    "position": {"x": 0, "y": 0}
}

Edges:

{
    "id": "e-source-target",
    "source": "source_state",
    "target": "target_state",
    "label": "trigger_name"
}

Development

# Tests
pytest tests/

# Coverage
pytest --cov=transitions_reactflow tests/

# Type checking
mypy transitions_reactflow/

License

MIT - see LICENSE file

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Credits

Built on top of the pytransitions library.

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

transitions_reactflow-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

transitions_reactflow-0.1.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for transitions_reactflow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 21a21b6911c876734ce64df594e83efb1a41ae2287a19a2aab0c716f0602f75a
MD5 6110cb05abef1a954c2ab01f43039ed3
BLAKE2b-256 19495bb739fc8a59e7d9aa2feaf6225dd31e03e1073f9bf181680443554d9ca2

See more details on using hashes here.

Provenance

The following attestation bundles were made for transitions_reactflow-0.1.0.tar.gz:

Publisher: publish-pypi.yml on JohnsonLM/transitions-reactflow

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

File details

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

File metadata

File hashes

Hashes for transitions_reactflow-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83e9ac3a2f81ffaff279d208e9c1370da654e4feb7f3e699cd0734eb6ead3309
MD5 e8098c278a83e418631b630e941893e5
BLAKE2b-256 108369138ca3a256d06ddd951a9a77a326093a3a9525526687e108781bce5567

See more details on using hashes here.

Provenance

The following attestation bundles were made for transitions_reactflow-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on JohnsonLM/transitions-reactflow

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