Pyvis Network Visualization Library
Pyvis is a Python library designed for the quick generation of interactive network graphs with minimal Python code. It acts as a wrapper around the popular JavaScript Vis.js library, enabling users to build graphs in Python, customize nodes and edges with various properties, and export them as interactive HTML files. The library is currently at version 0.3.2 and maintains an active development status with regular updates, including a significant 0.3.0 release that merged an experimental branch and added features like node/edge filtering from the web interface.
Warnings
- gotcha Node IDs must be unique within a network. Re-adding a node with an existing ID will overwrite its properties, not create a new node. Ensure uniqueness for proper graph structure.
- gotcha Some attributes from the underlying VisJS library may not work as expected or at all when accessed via Pyvis. This is due to direct Python translation limitations.
- gotcha When using `net.show_buttons(filter_menu=True, select_menu=True, ...)` to interactively tweak layout or filtering settings in the browser, these changes are not automatically saved to your Python code. To persist interactive settings, you need to copy the generated options from the browser UI and apply them to your `Network` object using `net.set_options()`.
- gotcha Pyvis is primarily a visualization tool and is not as robust as libraries like NetworkX for complex graph analysis or generating network metrics (e.g., centrality). For advanced graph algorithms, it's recommended to build and analyze your graph with NetworkX first, then pass the NetworkX graph to Pyvis for visualization using `net.from_nx(nx_graph)`.
Install
-
pip install pyvis
Imports
- Network
from pyvis.network import Network
Quickstart
from pyvis.network import Network
# Create a Network object
net = Network(height="750px", width="100%", bgcolor="#222222", font_color="white", notebook=False)
# Add nodes
net.add_node(1, label="Node 1", title="This is node 1")
net.add_node(2, label="Node 2", title="This is node 2", color="#00ff1e")
net.add_node(3, label="Node 3", title="This is node 3")
# Add edges
net.add_edge(1, 2, title="connects 1 and 2")
net.add_edge(2, 3, title="connects 2 and 3", value=5)
# Generate and save the network visualization to an HTML file
net.show("basic_network.html")