Skip to main content

Interactive network graphs in a Jupyter notebook

Project description

network-graph-viewer

Interactive network graphs in a Jupyter notebook, from the Network Graph Viewer.

The whole app runs in the cell output: the sidebar, the filters, the metrics, the data table. Nothing is sent anywhere. The graph goes out to the browser as the app's own workspace format, and the selection and any edits come back.

Install

pip install network-graph-viewer

or:

uv add network-graph-viewer

Nothing else is needed: the widget ships its own JavaScript, and pandas and networkx are only used if you hand it one of theirs.

Use

import network_graph_viewer as ngv
import pandas as pd

edges = pd.DataFrame(
    [
        {"from": "ana", "to": "ben", "weight": 3, "team": "design"},
        {"from": "ben", "to": "cleo", "weight": 1, "team": "design"},
    ]
)

w = ngv.show(edges, source="from", target="to", color="team")
w

edges can also be a list of dicts, a list of (source, target) pairs, or a networkx graph, in which case the node attributes come with it:

import networkx as nx

ngv.show(nx.karate_club_graph(), color="club")

Node attributes that live in their own table go in as nodes, and any node an edge names but the table does not gets a row anyway:

ngv.show(edges, source="from", target="to", nodes=people, node_id="Id")

What the cell shows

The cell shows the graph and nothing else. Every panel has a labelled tab on the edge of the stage that opens it, and panels= opens any of them to start with:

ngv.show(edges, source="from", target="to", panels=["sidebar", "table"])

The widget follows the notebook's own light or dark theme and keeps following it, so switching the JupyterLab theme switches the graph too. Pin it with theme="light" or theme="dark", or change it live in the View menu on the graph.

Reading the graph back

The widget is live. Click a node, edit a cell, run a metric, and the kernel sees it:

w.selected_node  # 'ana', or None
w.edges  # the edge table as a DataFrame, edits included
w.nodes  # likewise, with any computed columns
w.save("graph.ngv.json")

w.edges and w.nodes are None until the browser has reported something, which it does shortly after the widget first draws.

How it works

The graph is handed to the browser as the app's own .ngv.json workspace, so a notebook goes in through the same door as a dropped file or a shared link, and meets the same reader on the other side. What comes back is the selection and the edited tables. Nothing is uploaded: the kernel and the browser are talking over the notebook's own connection.

build_workspace is that translation on its own, if you want the dictionary without a widget:

workspace = ngv.build_workspace(edges, source="from", target="to")

Every cell has to survive the trip as JSON, so NumPy scalars, timestamps and decimals are each converted to the nearest thing a cell can hold, and an integer too large for a JSON number keeps its digits as text rather than rounding into a collision.

Development

The widget loads one JavaScript file, src/network_graph_viewer/static/widget.js. It is built from the TypeScript app at the repository root and is not in the repository: a fresh clone has to build it before the package will run, and so does anything that rebuilds the app.

pnpm install            # once, at the repository root
pnpm build:widget       # writes static/widget.js

Then, in this directory:

uv sync
uv run pytest
uv run pytest --nbmake examples/demo.ipynb
uv run ruff check && uv run ruff format

The release workflow builds the bundle from the source at the tag, so what gets published is never taken on trust from anybody's working tree.

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

network_graph_viewer-0.2.0.tar.gz (809.1 kB view details)

Uploaded Source

Built Distribution

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

network_graph_viewer-0.2.0-py3-none-any.whl (814.5 kB view details)

Uploaded Python 3

File details

Details for the file network_graph_viewer-0.2.0.tar.gz.

File metadata

  • Download URL: network_graph_viewer-0.2.0.tar.gz
  • Upload date:
  • Size: 809.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for network_graph_viewer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 081b680738c3ffb9fe7d36de20e8476599fb84907ec30032a23c752b72052c3c
MD5 244f232926edae40a3c372ad13c730f1
BLAKE2b-256 400a8e1732b748575b6f48ca33209254d8cde07a12980130fca65996a128e1f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for network_graph_viewer-0.2.0.tar.gz:

Publisher: release.yml on ScriptSmith/network-graph-viewer

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

File details

Details for the file network_graph_viewer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for network_graph_viewer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0037d355522108cbfa4b45f9474ccf785f077b51f0cc4d0ef26036b37fd8fa1
MD5 afdaec54a45c8da22011bb60851335cd
BLAKE2b-256 9c1da23bf8ac73321e55f4d1b5fd721f48f40440b971955986402d1a29ee921c

See more details on using hashes here.

Provenance

The following attestation bundles were made for network_graph_viewer-0.2.0-py3-none-any.whl:

Publisher: release.yml on ScriptSmith/network-graph-viewer

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