cytoscape_data#

cytoscape_data(G, name='name', ident='id')[source]#

Returns data in Cytoscape JSON format (cyjs).

Parameters:
GNetworkX Graph

The graph to convert to cytoscape format

namestring

A string which is mapped to the ‘name’ node element in cyjs format. Must not have the same value as ident.

identstring

A string which is mapped to the ‘id’ node element in cyjs format. Must not have the same value as name.

Returns:
data: dict

A dictionary with cyjs formatted data.

Raises:
NetworkXError

If the values for name and ident are identical.

See also

cytoscape_graph

convert a dictionary in cyjs format to a graph

References

[1]

Cytoscape user’s manual: http://manual.cytoscape.org/en/stable/index.html

Examples

>>> from pprint import pprint
>>> G = nx.path_graph(2)
>>> cyto_data = nx.cytoscape_data(G)
>>> pprint(cyto_data, sort_dicts=False)
{'data': [],
 'directed': False,
 'multigraph': False,
 'elements': {'nodes': [{'data': {'id': '0', 'value': 0, 'name': '0'}},
                        {'data': {'id': '1', 'value': 1, 'name': '1'}}],
              'edges': [{'data': {'source': 0, 'target': 1}}]}}

The json package can be used to serialize the resulting data

>>> import io, json
>>> with io.StringIO() as fh:  # replace io with `open(...)` to write to disk
...     json.dump(cyto_data, fh)
...     fh.seek(0)
...     print(fh.getvalue()[:64])  # View the first 64 characters
{"data": [], "directed": false, "multigraph": false, "elements":