Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function _getConnectedNodes(nodeSet, mode, graphvizInstance) {
return resultSet;
}

function _highlightSelection(graphvizInstance, currentSelection, $) {
function _highlightSelection(graphvizInstance, currentSelection, $, model) {
let highlightedNodes = $();
let highlightedEdges = $();

Expand All @@ -133,9 +133,27 @@ function _highlightSelection(graphvizInstance, currentSelection, $) {
graphvizInstance.highlight(highlightedNodes, highlightedEdges);
graphvizInstance.bringToFront(highlightedNodes);
graphvizInstance.bringToFront(highlightedEdges);

// Extract node and edge IDs for trait synchronization
const highlightedNodeIds = highlightedNodes
.map(function () {
return $(this).data("name");
})
.get();

const highlightedEdgeIds = highlightedEdges
.map(function () {
return $(this).data("name");
})
.get();

// Update the model traits
model.set("highlighted_nodes", highlightedNodeIds);
model.set("highlighted_edges", highlightedEdgeIds);
model.save_changes();
}

export function handleGraphvizSvgEvents(graphvizInstance, $, currentSelection, getSelectedDirection) {
export function handleGraphvizSvgEvents(graphvizInstance, $, currentSelection, getSelectedDirection, model) {
// Add hover event listeners for edges
Logger.debug("Initializing graph events");
graphvizInstance.edges().each(function () {
Expand Down Expand Up @@ -175,7 +193,7 @@ export function handleGraphvizSvgEvents(graphvizInstance, $, currentSelection, g
currentSelection.splice(0, currentSelection.length, selectionObject);
}

_highlightSelection(graphvizInstance, currentSelection, $);
_highlightSelection(graphvizInstance, currentSelection, $, model);
});
Logger.debug("Node click handlers attached");

Expand Down
8 changes: 7 additions & 1 deletion js/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ async function render({ model, el }) {
ready: function () {
Logger.debug(`Widget ${widgetId}: Graph plugin initialization started`);
graphvizInstance = this;
handleGraphvizSvgEvents(graphvizInstance, $, currentSelection, () => selectedDirection);
handleGraphvizSvgEvents(
graphvizInstance,
$,
currentSelection,
() => selectedDirection,
model
);
resolve(); // Signal that we're ready
Logger.debug(`Widget ${widgetId}: Graph plugin initialization complete`);
},
Expand Down
7 changes: 7 additions & 0 deletions src/graphviz_anywidget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class GraphvizAnyWidget(anywidget.AnyWidget):
case_sensitive = traitlets.Bool(False).tag(sync=True) # noqa: FBT003
enable_zoom = traitlets.Bool(True).tag(sync=True)
freeze_scroll = traitlets.Bool(False).tag(sync=True) # noqa: FBT003
last_clicked_node = traitlets.Unicode(None, allow_none=True).tag(sync=True)
highlighted_nodes = traitlets.List(traitlets.Unicode(), default_value=[]).tag(
sync=True
)
highlighted_edges = traitlets.List(traitlets.Unicode(), default_value=[]).tag(
sync=True
)


def graphviz_widget(
Expand Down
Loading