Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make graphviz svg interactive #858

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
31 changes: 30 additions & 1 deletion nutils/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import abc
import html
import re

Metadata = TypeVar('Metadata')
GraphvizColorCallback = Callable[['Node'], Optional[str]]
Expand Down Expand Up @@ -68,7 +69,35 @@ 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':
graph = re.sub(b'<svg width="(\d+)pt" height="(\d+)pt"', lambda match:
b'<svg width="%dpt" height="%dpt"' % tuple((int(l)*2)//3 for l in match.groups()), graph, count=1)
i = graph.rindex(b'</svg>')
graph = graph[:i] + clickHandler + graph[i:]
img.write(graph)

clickHandler = b'''
<style>
g.edge, g.node { cursor: pointer; }
.highlight path { stroke: orange; stroke-width: 2; }
.highlight polygon:first-of-type { fill: orange; }
.highlight polygon { stroke: orange; }
.highlight text { fill: white; }
</style>
joostvanzwieten marked this conversation as resolved.
Show resolved Hide resolved
<script>
document.addEventListener("click", function (event) {
const g = event.target.closest("g");
joostvanzwieten marked this conversation as resolved.
Show resolved Hide resolved
if (g.classList.contains("edge")) {
g.classList.toggle("highlight"); }
else if (g.classList.contains("node")) {
const index = g.firstElementChild.textContent;
const pattern = new RegExp("(^|[^0-9])" + index + "($|[^0-9])");
const isnew = g.classList.toggle("highlight");
for (const edge of document.getElementsByClassName("edge")) {
if (pattern.test(edge.firstElementChild.textContent)) {
edge.classList.toggle("highlight", isnew); } } } });
</script>'''
joostvanzwieten marked this conversation as resolved.
Show resolved Hide resolved


class RegularNode(Node[Metadata]):
Expand Down
Loading