From 4f98a2a0dd5d236c7c4b04644fa9d12aa085a2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florentin=20D=C3=B6rre?= Date: Fri, 24 Jan 2025 11:53:54 +0100 Subject: [PATCH 1/5] Document enum options and node/rel fields Co-authored-by: Adam Schill Collberg --- python-wrapper/pyproject.toml | 1 + python-wrapper/src/neo4j_viz/node.py | 9 +++++++++ python-wrapper/src/neo4j_viz/options.py | 17 ++++++++++++++--- python-wrapper/src/neo4j_viz/relationship.py | 7 +++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/python-wrapper/pyproject.toml b/python-wrapper/pyproject.toml index 9f995ad1..02befaeb 100644 --- a/python-wrapper/pyproject.toml +++ b/python-wrapper/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "ipython >=8, <9", "pydantic >=2 , <3", "pydantic-extra-types >=2, <3", + "enum-tools==0.12.0" ] requires-python = ">=3.9" diff --git a/python-wrapper/src/neo4j_viz/node.py b/python-wrapper/src/neo4j_viz/node.py index 0ed9cfb9..be1bf2e4 100644 --- a/python-wrapper/src/neo4j_viz/node.py +++ b/python-wrapper/src/neo4j_viz/node.py @@ -17,13 +17,17 @@ class Node(BaseModel, extra="allow"): All options available in the NVL library (see https://neo4j.com/docs/nvl/current/base-library/#_nodes) """ + #: Unique identifier for the node id: NodeIdType = Field( validation_alias=AliasChoices("id", "nodeId", "node_id"), description="Unique identifier for the node" ) + #: The caption of the node caption: Optional[str] = Field(None, description="The caption of the node") + #: The alignment of the caption text caption_align: Optional[CaptionAlignment] = Field( None, serialization_alias="captionAlign", description="The alignment of the caption text" ) + #: The size of the caption text. The font size to node radius ratio caption_size: Optional[int] = Field( None, ge=1, @@ -31,10 +35,15 @@ class Node(BaseModel, extra="allow"): serialization_alias="captionSize", description="The size of the caption text. The font size to node radius ratio", ) + #: The size of the node as radius in pixel size: Optional[RealNumber] = Field(None, ge=0, description="The size of the node as radius in pixel") + #: The color of the node color: Optional[ColorType] = Field(None, description="The color of the node") + #: Whether the node is pinned in the visualization pinned: Optional[bool] = Field(None, description="Whether the node is pinned in the visualization") + #: The x-coordinate of the node x: Optional[RealNumber] = Field(None, description="The x-coordinate of the node") + #: The y-coordinate of the node y: Optional[RealNumber] = Field(None, description="The y-coordinate of the node") @field_serializer("color") diff --git a/python-wrapper/src/neo4j_viz/options.py b/python-wrapper/src/neo4j_viz/options.py index 3d449dc0..73ec9c78 100644 --- a/python-wrapper/src/neo4j_viz/options.py +++ b/python-wrapper/src/neo4j_viz/options.py @@ -5,24 +5,35 @@ from typing import Any, Optional from pydantic import BaseModel, Field +import enum_tools.documentation +@enum_tools.documentation.document_enum class CaptionAlignment(str, Enum): + """" + The alignment of the caption text for nodes and relationships. + """ + TOP = "top" CENTER = "center" BOTTOM = "bottom" + +@enum_tools.documentation.document_enum class Layout(str, Enum): FORCE_DIRECTED = "forcedirected" HIERARCHICAL = "hierarchical" - GRID = "grid" + """ + The nodes are then arranged by the directionality of their relationships + """ COORDINATE = "free" - """" + """ The coordinate layout sets the position of each node based on the `x` and `y` properties of the node. """ + GRID = "grid" - +@enum_tools.documentation.document_enum class Renderer(str, Enum): """ The renderer used to render the visualization. diff --git a/python-wrapper/src/neo4j_viz/relationship.py b/python-wrapper/src/neo4j_viz/relationship.py index 25d66f37..040da1d9 100644 --- a/python-wrapper/src/neo4j_viz/relationship.py +++ b/python-wrapper/src/neo4j_viz/relationship.py @@ -15,26 +15,33 @@ class Relationship(BaseModel, extra="allow"): All options available in the NVL library (see https://neo4j.com/docs/nvl/current/base-library/#_relationships) """ + #: Unique identifier for the relationship id: Union[str, int] = Field( default_factory=lambda: uuid4().hex, description="Unique identifier for the relationship" ) + #: Node ID where the relationship points from source: Union[str, int] = Field( serialization_alias="from", validation_alias=AliasChoices("source", "sourceNodeId", "source_node_id", "from"), description="Node ID where the relationship points from", ) + #: Node ID where the relationship points to target: Union[str, int] = Field( serialization_alias="to", validation_alias=AliasChoices("target", "targetNodeId", "target_node_id", "to"), description="Node ID where the relationship points to", ) + #: The caption of the relationship caption: Optional[str] = Field(None, description="The caption of the relationship") + #: The alignment of the caption text caption_align: Optional[CaptionAlignment] = Field( None, serialization_alias="captionAlign", description="The alignment of the caption text" ) + #: The size of the caption text caption_size: Optional[Union[int, float]] = Field( None, gt=0.0, serialization_alias="captionSize", description="The size of the caption text" ) + #: The color of the relationship color: Optional[ColorType] = Field(None, description="The color of the relationship") @field_serializer("color") From eab865c3e287ac0c02c104a36eeaba988fdf5bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florentin=20D=C3=B6rre?= Date: Fri, 24 Jan 2025 13:38:55 +0100 Subject: [PATCH 2/5] Move type hints to parameters instead of signature Co-authored-by: Adam Schill Collberg --- docs/source/conf.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/conf.py b/docs/source/conf.py index a3a5ef5e..d19093c2 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -31,6 +31,10 @@ # -- Options for notebook extension ------------------------------------------- nbsphinx_execute = "never" +# -- Options for autodoc extension ------------------------------------------- +autodoc_typehints = "description" + + # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output From 5f81500a7a08f2d738656c2dcc4918b354f28a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florentin=20D=C3=B6rre?= Date: Fri, 24 Jan 2025 13:39:06 +0100 Subject: [PATCH 3/5] Format code --- python-wrapper/src/neo4j_viz/options.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python-wrapper/src/neo4j_viz/options.py b/python-wrapper/src/neo4j_viz/options.py index 73ec9c78..0a17b44c 100644 --- a/python-wrapper/src/neo4j_viz/options.py +++ b/python-wrapper/src/neo4j_viz/options.py @@ -4,13 +4,13 @@ from enum import Enum from typing import Any, Optional -from pydantic import BaseModel, Field import enum_tools.documentation +from pydantic import BaseModel, Field @enum_tools.documentation.document_enum class CaptionAlignment(str, Enum): - """" + """ The alignment of the caption text for nodes and relationships. """ @@ -19,7 +19,6 @@ class CaptionAlignment(str, Enum): BOTTOM = "bottom" - @enum_tools.documentation.document_enum class Layout(str, Enum): FORCE_DIRECTED = "forcedirected" @@ -33,6 +32,7 @@ class Layout(str, Enum): """ GRID = "grid" + @enum_tools.documentation.document_enum class Renderer(str, Enum): """ From 6f27ac3267b3704747921762dfbf5d53c2dce6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florentin=20D=C3=B6rre?= Date: Fri, 24 Jan 2025 13:39:44 +0100 Subject: [PATCH 4/5] Remove BaseModel inheritance from VisualizationGraph not needed as no validation required here --- .../src/neo4j_viz/visualization_graph.py | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/python-wrapper/src/neo4j_viz/visualization_graph.py b/python-wrapper/src/neo4j_viz/visualization_graph.py index 14546176..a017910c 100644 --- a/python-wrapper/src/neo4j_viz/visualization_graph.py +++ b/python-wrapper/src/neo4j_viz/visualization_graph.py @@ -5,7 +5,6 @@ from typing import Optional from IPython.display import HTML -from pydantic import BaseModel, Field from pydantic_extra_types.color import Color, ColorType from .colors import ColorsType, neo4j_colors @@ -16,13 +15,29 @@ from .relationship import Relationship -class VisualizationGraph(BaseModel): +class VisualizationGraph: """ A graph to visualize. """ - nodes: list[Node] = Field(description="The nodes in the graph") - relationships: list[Relationship] = Field(description="The relationships in the graph") + #: "The nodes in the graph" + nodes: list[Node] + #: "The relationships in the graph" + relationships: list[Relationship] + + def __init__(self, nodes: list[Node], relationships: list[Relationship]) -> None: + """ " + Create a new `VisualizationGraph`. + + Parameters + ---------- + nodes: + The nodes in the graph. + relationships: + The relationships in the graph. + """ + self.nodes = nodes + self.relationships = relationships def render( self, From f7133b1c73a9f5bff0babf0873980d308c91af60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florentin=20D=C3=B6rre?= Date: Fri, 24 Jan 2025 13:44:25 +0100 Subject: [PATCH 5/5] Omit pydantic internal members Co-authored-by: Adam Schill Collberg --- docs/source/api-reference/node.rst | 1 + docs/source/api-reference/relationship.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/source/api-reference/node.rst b/docs/source/api-reference/node.rst index 11ba39e0..65af92d7 100644 --- a/docs/source/api-reference/node.rst +++ b/docs/source/api-reference/node.rst @@ -1,2 +1,3 @@ .. autoclass:: neo4j_viz.Node :members: + :exclude-members: model_config diff --git a/docs/source/api-reference/relationship.rst b/docs/source/api-reference/relationship.rst index 29647130..b212e4dc 100644 --- a/docs/source/api-reference/relationship.rst +++ b/docs/source/api-reference/relationship.rst @@ -1,2 +1,3 @@ .. autoclass:: neo4j_viz.Relationship :members: + :exclude-members: model_config