Omega
Omega

Reputation: 267

Use hyperlinks and add extra notes to each dot in the graph

I am using pygraphviz (I don't want to use other libs).

I am creating a graph with urls and I want them to be clickable and send me to that url in a browser. And I also want to add extra notes to each link when I hover over that link.

This is part of my code (using example.com):

from pygraphviz import *

A = AGraph()
styles = {
    'graph': {
        'label': 'Example',
        'fontsize': '20',
        'fontcolor': 'white',
        'bgcolor': '#333333',
        'rankdir': 'BT',
    },
    'nodes': {
        'fontname': 'Helvetica',
        'shape': 'hexagon',
        'fontcolor': 'white',
        'color': 'white',
        'style': 'filled',
        'fillcolor': '#006699',
    },
    'edges': {
        'style': 'dashed',
        'color': 'white',
        'arrowhead': 'open',
        'fontname': 'Courier',
        'fontsize': '20',
        'fontcolor': 'white',
    }
}

edges = [("https://example.com/", "https://example.com/videos"),
         ("https://example.com/", "https://example.com/videos"),
         ("https://example.com/", "https://example.com/search"),
         ("https://example.com/", "https://example.com/videos?sort=likes-down"),
         ("https://example.com/", "https://example.com/videos?sort=views-down"),
         ("https://example.com/", "https://example.com/videos?sort=duration-down"),
         ("https://example.com/", "https://accounts.example.com/free-signup"),
         ("https://example.com/", "https://accounts.example.com/login")]

A.add_edges_from(edges)
A.draw('example.pdf', prog='fdp')

I want to be able to click all the links and use pdf if possible.

Also, when I hover, I want to show extra notes about that link.

Upvotes: 0

Views: 891

Answers (1)

dumbass
dumbass

Reputation: 27211

Adding hyperlinks and tooltips to GraphViz nodes is pretty easy:

nodes = [
    "https://example.com/",
    "https://example.com/search",
    "https://example.com/videos",
    "https://example.com/videos?sort=duration-down",
    "https://example.com/videos?sort=likes-down",
    "https://example.com/videos?sort=views-down",
    "https://accounts.example.com/free-signup",
    "https://accounts.example.com/login"
]

for (i, node) in enumerate(nodes):
    A.add_node(node, href=node, tooltip=f'node #{i}')

These will not work with all output formats, however. GraphViz documentation states that the href attribute only works in the SVG, map (image map) and PostScript output formats (it does work in PDF, as that is apparently considered a profile of PostScript), while the tooltip attribute works only in the SVG and cmap (client-side/HTML image map) formats. Most PDF readers will probably not support any kind of interactivity other than hyperlinks anyway. (And consider also devices that don’t have any concept of ‘hovering’.)

Upvotes: 1

Related Questions