Skip to content

Commit 387a789

Browse files
committed
Add set_node_caption convenience method
ref GDS-59
1 parent 557d21f commit 387a789

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

changelog.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
## Breaking changes
44

5-
* Removed `table` property from nodes and relationships returned from `from_snowflake`, the table is represented by the `caption` field.
6-
* Changed default value of `override` parameter in `VisualizationGraph.color_nodes()` from `False` to `True`. The method now overrides existing node colors by default. To preserve existing colors, explicitly pass `override=False`.
5+
- Removed `table` property from nodes and relationships returned from `from_snowflake`, the table is represented by the `caption` field.
6+
- Changed default value of `override` parameter in `VisualizationGraph.color_nodes()` from `False` to `True`. The method now overrides existing node colors by default. To preserve existing colors, explicitly pass `override=False`.
77

88
## New features
99

10+
- Added `set_node_captions()` convenience method to `VisualizationGraph` for setting node captions from a field or property.
1011

1112
## Bug fixes
1213

1314
## Improvements
1415

15-
1616
## Other changes

docs/source/customizing.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,45 @@ If you have not yet created a ``VisualizationGraph`` object, please refer to one
2020
:backlinks: none
2121

2222

23+
Setting node captions
24+
---------------------
25+
26+
Node captions are the text labels displayed on nodes in the visualization.
27+
28+
The ``set_node_captions`` method
29+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30+
31+
By calling the :meth:`neo4j_viz.VisualizationGraph.set_node_captions` method, you can set node captions based on a
32+
node field (like ``id``, ``size``, etc.) or a node property (members of the ``Node.properties`` map).
33+
34+
The method accepts an ``override`` parameter (default ``True``) that controls whether to replace existing captions.
35+
If ``override=False``, only nodes without captions will be updated.
36+
37+
Here's an example of setting node captions from a property:
38+
39+
.. code-block:: python
40+
41+
# VG is a VisualizationGraph object with nodes that have a "name" property
42+
VG.set_node_captions(property="name")
43+
44+
You can also set captions from a node field, and choose not to override existing captions:
45+
46+
.. code-block:: python
47+
48+
# VG is a VisualizationGraph object
49+
VG.set_node_captions(field="id", override=False)
50+
51+
For more complex scenarios where you need fallback logic or want to combine multiple properties, you can iterate over
52+
nodes directly:
53+
54+
.. code-block:: python
55+
56+
# VG is a VisualizationGraph object
57+
for node in VG.nodes:
58+
caption = node.properties.get("name") or node.properties.get("title") or node.id
59+
node.caption = str(caption)
60+
61+
2362
Coloring nodes
2463
--------------
2564

python-wrapper/src/neo4j_viz/visualization_graph.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,73 @@ def toggle_nodes_pinned(self, pinned: dict[NodeIdType, bool]) -> None:
194194

195195
node.pinned = node_pinned
196196

197+
def set_node_captions(
198+
self,
199+
*,
200+
field: Optional[str] = None,
201+
property: Optional[str] = None,
202+
override: bool = True,
203+
) -> None:
204+
"""
205+
Set the caption for nodes in the graph based on either a node field or a node property.
206+
207+
Parameters
208+
----------
209+
field:
210+
The field of the nodes to use as the caption. Must be None if `property` is provided.
211+
property:
212+
The property of the nodes to use as the caption. Must be None if `field` is provided.
213+
override:
214+
Whether to override existing captions of the nodes, if they have any.
215+
216+
Examples
217+
--------
218+
Given a VisualizationGraph `VG`:
219+
220+
>>> nodes = [
221+
... Node(id="0", properties={"name": "Alice", "age": 30}),
222+
... Node(id="1", properties={"name": "Bob", "age": 25}),
223+
... ]
224+
>>> VG = VisualizationGraph(nodes=nodes)
225+
226+
Set node captions from a property:
227+
228+
>>> VG.set_node_captions(property="name")
229+
230+
Set node captions from a field, only if not already set:
231+
232+
>>> VG.set_node_captions(field="id", override=False)
233+
234+
Set captions from multiple properties with fallback:
235+
236+
>>> for node in VG.nodes:
237+
... caption = node.properties.get("name") or node.properties.get("title") or node.id
238+
... if override or node.caption is None:
239+
... node.caption = str(caption)
240+
"""
241+
if not ((field is None) ^ (property is None)):
242+
raise ValueError(
243+
f"Exactly one of the arguments `field` (received '{field}') and `property` (received '{property}') must be provided"
244+
)
245+
246+
if property:
247+
# Use property
248+
for node in self.nodes:
249+
if not override and node.caption is not None:
250+
continue
251+
252+
value = node.properties.get(property, "")
253+
node.caption = str(value)
254+
else:
255+
# Use field
256+
attribute = to_snake(field)
257+
for node in self.nodes:
258+
if not override and node.caption is not None:
259+
continue
260+
261+
value = getattr(node, attribute, "")
262+
node.caption = str(value)
263+
197264
def resize_nodes(
198265
self,
199266
sizes: Optional[dict[NodeIdType, RealNumber]] = None,

0 commit comments

Comments
 (0)