-
Notifications
You must be signed in to change notification settings - Fork 1
Auto-serialize EdgeSpec and NodeSpec to dictionaries #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
…ds correctly Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
…es-nodes Review feedback
MarcSkovMadsen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local Claude and I am happy.
Manual Test App
"""Test app for verifying auto-serialization of NodeSpec/EdgeSpec.
This app demonstrates the new auto-serialization feature where NodeSpec
and EdgeSpec objects are passed directly to ReactFlow without calling
.to_dict() manually.
"""
import panel as pn
from panel_reactflow import EdgeSpec, NodeSpec, NodeType, ReactFlow
pn.extension("jsoneditor")
# Define a custom node type with a schema
node_types = {
"task": NodeType(
type="task",
label="Task",
schema={
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["pending", "running", "done"],
"title": "Status",
},
"priority": {
"type": "number",
"minimum": 1,
"maximum": 5,
"title": "Priority",
},
},
},
inputs=["input"],
outputs=["output"],
),
}
# Auto-serialization: pass NodeSpec objects directly, no .to_dict() needed
nodes = [
NodeSpec(
id="n1",
type="task",
position={"x": 50, "y": 50},
label="Fetch Data",
data={"status": "done", "priority": 3},
),
NodeSpec(
id="n2",
type="task",
position={"x": 300, "y": 50},
label="Transform",
data={"status": "running", "priority": 2},
),
NodeSpec(
id="n3",
type="task",
position={"x": 550, "y": 50},
label="Load Results",
data={"status": "pending", "priority": 1},
),
# Mix: plain dict alongside NodeSpec objects
{
"id": "n4",
"type": "panel",
"position": {"x": 300, "y": 250},
"label": "Plain dict node",
"data": {},
},
]
# Auto-serialization also works for EdgeSpec
edges = [
EdgeSpec(id="e1", source="n1", target="n2", label="pipe"),
EdgeSpec(id="e2", source="n2", target="n3", label="pipe"),
EdgeSpec(id="e3", source="n2", target="n4"),
]
flow = ReactFlow(
nodes=nodes,
edges=edges,
node_types=node_types,
min_height=500,
sizing_mode="stretch_both",
)
# Verify auto-serialization happened
for i, node in enumerate(flow.nodes):
assert isinstance(node, dict), f"Node {i} should be a dict, got {type(node)}"
for i, edge in enumerate(flow.edges):
assert isinstance(edge, dict), f"Edge {i} should be a dict, got {type(edge)}"
description = pn.pane.Markdown(
"""## Auto-Serialization Test
NodeSpec and EdgeSpec objects are passed **directly** to ReactFlow (no `.to_dict()` needed).
- **Nodes 1-3**: Created with `NodeSpec(...)` and passed directly
- **Node 4**: Created as a plain dict (still works)
- **Edges**: Created with `EdgeSpec(...)` and passed directly
- All nodes and edges are auto-converted to dicts internally
""",
sizing_mode="stretch_width",
)
layout = pn.Column(description, flow, sizing_mode="stretch_both", min_height=700)
layout.servable()
docs/how-to/define-nodes-edges.md
Outdated
|
|
||
| !!! note "Automatic Serialization" | ||
| `NodeSpec` and `EdgeSpec` objects are automatically converted to dictionaries | ||
| when passed to `ReactFlow`. You no longer need to call `.to_dict()` manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the no longer. This framework is so new that almost no one would have read the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot . Please fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f343d26. Changed "You no longer need" to "You don't need" to avoid implying a previous state that users wouldn't be familiar with.
Co-authored-by: MarcSkovMadsen <42288570+MarcSkovMadsen@users.noreply.github.com>
Auto-serialize EdgeSpec and NodeSpec Objects
This PR implements automatic serialization of
EdgeSpecandNodeSpecobjects to dictionaries, eliminating the need for users to manually call.to_dict().Problem Fixed
Previously, users had to call
.to_dict()on EdgeSpec/NodeSpec objects when passing them to ReactFlow:Without
.to_dict(), the code would fail with:Solution
Now EdgeSpec and NodeSpec objects are automatically converted to dictionaries:
Implementation
_normalize_nodes()and_normalize_edges()methods that convert Spec objects to dicts__init__for objects passed during initializationsuper().__init__()Changes
Code:
src/panel_reactflow/base.py: Implementation of auto-serializationtests/test_api.py: Comprehensive test coverageDocumentation:
docs/how-to/define-nodes-edges.md: Updated guide with new featureTesting
✅ All auto-serialization tests pass
✅ Original issue example works correctly
✅ No infinite recursion
✅ Backward compatible with existing dict-based code
✅ No security vulnerabilities detected
Benefits
.to_dict()Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.