Skip to content

Instantly share code, notes, and snippets.

@cristianvasquez
Last active August 5, 2024 09:00
Show Gist options
  • Select an option

  • Save cristianvasquez/0fe3e3c4149714322b71731b6f13a39d to your computer and use it in GitHub Desktop.

Select an option

Save cristianvasquez/0fe3e3c4149714322b71731b6f13a39d to your computer and use it in GitHub Desktop.
pyvis rdf view
from IPython.core.display import display, HTML
from rdflib import Graph
from pyvis.network import Network
import hashlib
def generate_id(value):
"""Generate a unique ID for each RDF term."""
return hashlib.md5(str(value).encode()).hexdigest()
def graph_html(g):
net = Network(height='600px', width='600px', directed=True)
nodes = set()
for s, p, o in g:
s_id = generate_id(s)
o_id = generate_id(o)
if s_id not in nodes:
net.add_node(s_id, label=str(s))
nodes.add(s_id)
if o_id not in nodes:
net.add_node(o_id, label=str(o))
nodes.add(o_id)
net.add_edge(s_id, o_id, title=str(p))
# Generate and display the HTML
return net.generate_html()
# Load and parse RDF graph
g = Graph()
g.parse("test.ttl") # Replace with the path to your RDF file
net_html = graph_html(g)
display(HTML(net_html))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment