From 304528ebc708fb46ddb02c9517ef3bafa69e175c Mon Sep 17 00:00:00 2001 From: Gertjan van Zwieten Date: Thu, 15 Feb 2024 16:57:30 +0100 Subject: [PATCH 1/2] 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 | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/nutils/_graph.py b/nutils/_graph.py index 939308c58..1c531713b 100644 --- a/nutils/_graph.py +++ b/nutils/_graph.py @@ -68,7 +68,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 = b''' + +''' class RegularNode(Node[Metadata]): From ad33d53118efe092cc4cc77367e0443c0b2056fa Mon Sep 17 00:00:00 2001 From: Gertjan van Zwieten Date: Fri, 16 Feb 2024 15:08:19 +0100 Subject: [PATCH 2/2] scale graphviz svg down to 2/3 of original size This patch modifies the width and height parameters of the graphviz generated svg image in order to scale it down to 2/3 the original size. This avoids having to scale the image via the browser which may affect other tabs as well. Sadly it does not seem possible to instruct the dot executable to generate an image at the desired scale directly. The regular expression that is used instead is fairly sensitive to the generated xml, but that is acceptable given that the only risk is to leave the svg at its original scale. --- nutils/_graph.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nutils/_graph.py b/nutils/_graph.py index 1c531713b..f91c7912f 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]] @@ -70,6 +71,8 @@ def export_graphviz(self, *, fill_color: Optional[GraphvizColorCallback] = None, treelog.warning('graphviz failed for error code', status.returncode) graph = status.stdout if image_type == 'svg': + graph = re.sub(b'') graph = graph[:i] + clickHandler + graph[i:] img.write(graph)