Skip to content

Commit f36b09f

Browse files
authored
Merge pull request #59 from neo4j/pinning
Add node pinning support
2 parents 9afdaa0 + b7e5716 commit f36b09f

5 files changed

Lines changed: 41 additions & 1 deletion

File tree

python-wrapper/src/neo4j_viz/node.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Node(BaseModel, extra="allow"):
3333
)
3434
size: Optional[RealNumber] = Field(None, ge=0, description="The size of the node as radius in pixel")
3535
color: Optional[ColorType] = Field(None, description="The color of the node")
36+
pinned: Optional[bool] = Field(None, description="Whether the node is pinned in the visualization")
3637

3738
@field_serializer("color")
3839
def serialize_color(self, color: Color) -> str:

python-wrapper/src/neo4j_viz/visualization_graph.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ def render(
6666
height,
6767
)
6868

69+
def toggle_nodes_pinned(self, pinned: dict[NodeIdType, bool]) -> None:
70+
"""
71+
Toggle whether nodes should be pinned or not.
72+
73+
Parameters
74+
----------
75+
pinned:
76+
A dictionary mapping from node ID to whether the node should be pinned or not.
77+
"""
78+
for node in self.nodes:
79+
node_pinned = pinned.get(node.id)
80+
81+
if node_pinned is None:
82+
continue
83+
84+
node.pinned = node_pinned
85+
6986
def resize_nodes(
7087
self,
7188
sizes: Optional[dict[NodeIdType, RealNumber]] = None,

python-wrapper/tests/test_node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def test_nodes_with_all_options() -> None:
1111
caption_size=1,
1212
color="#FF0000",
1313
size=10,
14+
pinned=True,
1415
)
1516

1617
assert node.to_dict() == {
@@ -20,6 +21,7 @@ def test_nodes_with_all_options() -> None:
2021
"captionSize": 1,
2122
"color": "#ff0000",
2223
"size": 10,
24+
"pinned": True,
2325
}
2426

2527

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from neo4j_viz import Node, VisualizationGraph
2+
from neo4j_viz.node import NodeIdType
3+
4+
5+
def test_toggle_nodes_pinned() -> None:
6+
nodes = [
7+
Node(id=42, pinned=False),
8+
Node(id=43),
9+
Node(id=44),
10+
Node(id="1337", pinned=True),
11+
]
12+
VG = VisualizationGraph(nodes=nodes, relationships=[])
13+
14+
pinned: dict[NodeIdType, bool] = {42: True, 43: True, "1337": False}
15+
VG.toggle_nodes_pinned(pinned)
16+
17+
assert VG.nodes[0].pinned
18+
assert VG.nodes[1].pinned
19+
assert VG.nodes[2].pinned is None
20+
assert not VG.nodes[3].pinned

python-wrapper/tests/test_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_basic_render(render_option: dict[str, Any], tmp_path: Path) -> None:
2020
nodes = [
2121
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:0", caption="Person"),
2222
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:6", caption="Product"),
23-
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:11", caption="Product"),
23+
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:11", caption="Product", pinned=True),
2424
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:12", caption="Product"),
2525
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:1", caption="Person"),
2626
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:7", caption="Product"),

0 commit comments

Comments
 (0)