From 062ef59c6b294d9833f9f00c854c0d107aa32752 Mon Sep 17 00:00:00 2001 From: Gertjan van Zwieten Date: Thu, 15 Feb 2024 16:57:30 +0100 Subject: [PATCH] make svg graphs clickable This patch inserts a javascript snippet into generated svg graphs to make nodes and edges highlightable in capable viewers (such as a web browser). --- nutils/_graph.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/nutils/_graph.py b/nutils/_graph.py index 939308c58..0d8ed42ca 100644 --- a/nutils/_graph.py +++ b/nutils/_graph.py @@ -5,6 +5,7 @@ import subprocess import abc import html +import re Metadata = TypeVar('Metadata') GraphvizColorCallback = Callable[['Node'], Optional[str]] @@ -68,7 +69,33 @@ def export_graphviz(self, *, fill_color: Optional[GraphvizColorCallback] = None, for i, line in enumerate(src.split('\n'), 1): print('{:04d} {}'.format(i, line)) treelog.warning('graphviz failed for error code', status.returncode) - img.write(status.stdout) + graph = status.stdout + if image_type == 'svg': + i = graph.rindex(b'') + graph = graph[:i] + clickHandler + graph[i:] + img.write(graph) + +clickHandler = rb''' + +''' class RegularNode(Node[Metadata]):