NetworkX Backend Interface
nx-neptune serves as a NetworkX-compatible backend for Neptune Analytics. This enables you to offload graph algorithm workloads to AWS with no code changes — use familiar NetworkX APIs to seamlessly scale graph computations on-demand.
For more on NetworkX backends, see the NetworkX backends documentation.
How it works
Section titled “How it works”- You write standard NetworkX code
- Add
backend="neptune"to any supported algorithm call - nx-neptune transparently syncs your graph to Neptune Analytics, executes the algorithm there, and returns results in NetworkX-compatible format
import networkx as nx
# Build a small social networkG = nx.DiGraph()G.add_edges_from([ ("Alice", "Bob"), ("Alice", "Carol"), ("Bob", "Carol"), ("Carol", "Dave"), ("Dave", "Alice"),])
# Runs on Neptune Analytics, returns standard NetworkX result formatscores = nx.pagerank(G, backend="neptune")
# Who's most influential?top = sorted(scores.items(), key=lambda x: x[1], reverse=True)for name, score in top: print(f"{name}: {score:.4f}")Configuration
Section titled “Configuration”Set the NETWORKX_GRAPH_ID environment variable to point at your Neptune Analytics instance:
export NETWORKX_GRAPH_ID=your-neptune-analytics-graph-idpython ./nx_example.pyOr inline:
NETWORKX_GRAPH_ID=your-neptune-analytics-graph-id python ./nx_example.pyBackend registration
Section titled “Backend registration”The backend is registered via the nx_plugin entry point in pyproject.toml. When you install nx_neptune, NetworkX automatically discovers the "neptune" backend.
# No explicit import needed — just use the backend parameterresult = nx.pagerank(G, backend="neptune")Algorithm implementation pattern
Section titled “Algorithm implementation pattern”Each algorithm follows this pattern:
from nx_neptune.utils.decorators import configure_if_nx_active
@configure_if_nx_active()def algorithm_name(neptune_graph: NeptuneGraph, **kwargs): # Build openCypher query # Execute on Neptune Analytics # Transform results to NetworkX format return resultSupported algorithms
Section titled “Supported algorithms”nx-neptune supports algorithms across four categories:
| Category | Algorithms |
|---|---|
| Traversal | bfs_edges, descendants_at_distance, bfs_layers |
| Link Analysis | pagerank |
| Centrality | degree_centrality, in_degree_centrality, out_degree_centrality, closeness_centrality |
| Community | louvain_communities, label_propagation_communities, fast_label_propagation_communities, asyn_lpa_communities |
Neptune Analytics extensions
Section titled “Neptune Analytics extensions”Beyond standard NetworkX parameters, nx-neptune exposes Neptune Analytics-specific parameters on each algorithm:
vertex_label— filter by vertex labeledge_labels— filter by edge labelsconcurrency— control thread counttraversal_direction—"outbound"or"inbound"write_property— persist results as node propertiesedge_weight_property/edge_weight_type— weighted computation
These are passed as additional keyword arguments:
r = nx.pagerank( G, backend="neptune", vertex_label="Person", edge_labels=["KNOWS"], write_property="pr_score",)