nxv renders NetworkX graphs using GraphViz.
- :ref:`using-nxv-inside-jupyter` is the easiest and recommended way to get started.
- :ref:`using-nxv-outside-jupyter` describes how to use nxv in other settings.
Using nxv inside of Jupyter
Start by importing networkx and nxv.
import networkx as nx
import nxv
Define a simple NetworkX graph.
.. literalinclude:: ../src/docs.py :start-after: BEGIN EXAMPLE :end-before: END EXAMPLE :dedent: 4 :pyobject: quickstart_graph
Render the graph with GraphViz using the :func:`~nxv.render` function.
nxv.render(graph)
Use a :class:`~nxv.Style` to specify how to style the graph using GraphViz.
.. literalinclude:: ../src/docs.py :start-after: BEGIN EXAMPLE :end-before: END EXAMPLE :dedent: 4 :pyobject: quickstart_graph_style
See the GraphViz attributes documentation for information on what attributes are available to use.
Render the graph with the :class:`~nxv.Style` by passing it to the :func:`~nxv.render` function.
nxv.render(graph, style)
The :class:`~nxv.Style` parameters can be functions that map the parts of a graph to different styles.
.. literalinclude:: ../src/docs.py :start-after: BEGIN EXAMPLE :end-before: END EXAMPLE :dedent: 4 :pyobject: quickstart_graph_functional_style
nxv.render(graph, style)
Using nxv outside of Jupyter
Outside of Jupyter, the format
parameter of the :func:`~nxv.render` function is required.
When the format
parameter is provided, the behavior of the :func:`~nxv.render` function is to
return the :class:`bytes` of the result in the specified format.
data = nxv.render(graph, style, format="svg")
with open("graph.svg", "wb") as f:
f.write(data)