Skip to main content

Interactive graph visualization for Python notebooks using anywidget

Project description

anywidget-graph

Interactive graph visualization for Python notebooks.

Works with Marimo, Jupyter, VS Code, Colab, anywhere anywidget runs.

Features

  • Universal — One widget, every notebook environment
  • Backend-agnostic — Grafeo, Neo4j, NetworkX, pandas, or raw dicts
  • Interactive — Pan, zoom, click, expand neighbors, select paths
  • Customizable — Colors, sizes, shapes, layouts
  • Performant — Virtualized rendering for large graphs
  • Exportable — PNG, SVG, JSON

Installation

uv add anywidget-graph

Quick Start

from anywidget_graph import Graph

graph = Graph.from_dict({
    "nodes": [
        {"id": "alice", "label": "Alice", "group": "person"},
        {"id": "bob", "label": "Bob", "group": "person"},
        {"id": "paper", "label": "Graph Theory", "group": "document"},
    ],
    "edges": [
        {"source": "alice", "target": "bob", "label": "knows"},
        {"source": "alice", "target": "paper", "label": "authored"},
    ]
})

graph

Data Sources

Dictionary

from anywidget_graph import Graph

graph = Graph.from_dict({
    "nodes": [{"id": "a"}, {"id": "b"}],
    "edges": [{"source": "a", "target": "b"}]
})

Grafeo

from grafeo import GrafeoDB
from anywidget_graph import Graph

db = GrafeoDB()
db.execute("INSERT (:Person {name: 'Alice'})-[:KNOWS]->(:Person {name: 'Bob'})")

result = db.execute("MATCH (a)-[r]->(b) RETURN a, r, b")
graph = Graph.from_grafeo(result)

Neo4j

from neo4j import GraphDatabase
from anywidget_graph import Graph

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
    result = session.run("MATCH (a)-[r]->(b) RETURN a, r, b LIMIT 100")
    graph = Graph.from_neo4j(result)

NetworkX

import networkx as nx
from anywidget_graph import Graph

G = nx.karate_club_graph()
graph = Graph.from_networkx(G)

pandas

import pandas as pd
from anywidget_graph import Graph

edges = pd.DataFrame({
    "source": ["alice", "alice", "bob"],
    "target": ["bob", "carol", "carol"],
    "weight": [1.0, 0.5, 0.8]
})

graph = Graph.from_pandas(edges)

Interactivity

Events

graph = Graph.from_dict(data)

@graph.on_node_click
def handle_node(node_id, node_data):
    print(f"Clicked: {node_id}")

@graph.on_edge_click  
def handle_edge(edge_id, edge_data):
    print(f"Edge: {edge_data['label']}")

Selection

graph.selected_nodes         # Get current selection
graph.select(["alice"])      # Select nodes
graph.clear_selection()      # Clear

Expansion

graph.expand("alice")        # Show neighbors
graph.collapse("alice")      # Hide neighbors

Styling

By Group

graph = Graph.from_dict(
    data,
    node_styles={
        "person": {"color": "#4CAF50", "size": 30},
        "document": {"color": "#2196F3", "shape": "square"},
    }
)

By Property

graph = Graph.from_dict(
    data,
    node_color="group",                    # Color by field
    node_size=lambda n: n["score"] * 10,   # Size by function
    edge_width="weight",                   # Width by field
)

Layouts

Graph.from_dict(data, layout="force")        # Default
Graph.from_dict(data, layout="hierarchical")
Graph.from_dict(data, layout="circular")
Graph.from_dict(data, layout="grid")

Options

graph = Graph.from_dict(
    data,
    width=800,
    height=600,
    directed=True,
    labels=True,
    edge_labels=False,
    physics=True,
    zoom=(0.1, 4),
)

Large Graphs

For 1000+ nodes:

graph = Graph.from_dict(
    data,
    virtualize=True,
    cluster=True,
)

Export

graph.to_png("graph.png")
graph.to_svg("graph.svg")
graph.to_json("graph.json")

Environment Support

Environment Supported
Marimo
JupyterLab
Jupyter Notebook
VS Code
Google Colab
Databricks

Related

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anywidget_graph-0.2.1.tar.gz (70.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anywidget_graph-0.2.1-py3-none-any.whl (37.0 kB view details)

Uploaded Python 3

File details

Details for the file anywidget_graph-0.2.1.tar.gz.

File metadata

  • Download URL: anywidget_graph-0.2.1.tar.gz
  • Upload date:
  • Size: 70.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for anywidget_graph-0.2.1.tar.gz
Algorithm Hash digest
SHA256 33800b36400d0dd32123e95d9264d6b8094ec24ebf7ae5e6e8bdc6c9c33b7546
MD5 5afd8382ae49e8967d025ce5a63cba38
BLAKE2b-256 cd1e5572f4dffb7a59f17edebe05a3d947c1a9cde4a573ebbe2a638e28abd4ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for anywidget_graph-0.2.1.tar.gz:

Publisher: pypi.yml on GrafeoDB/anywidget-graph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file anywidget_graph-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for anywidget_graph-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27f26d0657bd49acfd5a4ca094d5f06421694550e17237bc906242eb947642c2
MD5 ce4a32ca568cbc8bb7001355e3bf2307
BLAKE2b-256 91c4d904b9c34b6998c2dfdd2794b2864c7264d0ef9f4431d01b1c0b3a6e0b2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for anywidget_graph-0.2.1-py3-none-any.whl:

Publisher: pypi.yml on GrafeoDB/anywidget-graph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page