Skip to content

networkx_json.py

NetworkxJSON dataclass

Bases: IO[Graph]

IO to load from and save graph data using NetworkX's JSON support. Calls networkx.node_link_graph and networkx.node_link_data under the hood.

Example usage:

>>> from pathlib import Path
>>> import networkx as nx
>>> from ordeq_networkx import NetworkxJSON
>>> random_graph = nx.erdos_renyi_graph(10, 0.5)
>>> my_graph = NetworkxJSON(
...     path=Path("graph.json")
... )
>>> my_graph.save(random_graph)  # doctest: +SKIP

load(**load_options)

Load a NetworkX graph from a JSON file using node-link format.

Parameters:

Name Type Description Default
**load_options Any

Additional keyword arguments passed to json.load. These can be used to control JSON decoding options.

{}

Returns:

Type Description
Graph

The loaded NetworkX graph.

save(graph, **save_options)

Save a NetworkX graph to a JSON file using node-link format.

Parameters:

Name Type Description Default
graph Graph

The NetworkX graph to save.

required
**save_options Any

Additional keyword arguments passed to json.dump. These can be used to control JSON encoding options.

{}