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.
pip install pyvisVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a basic interactive network graph using Pyvis. It initializes a `Network` object, adds three nodes with custom labels, titles, and colors, then connects them with edges. Finally, it generates an HTML file (`basic_network.html`) that can be opened in a browser to interact with the visualization. The `notebook=False` parameter ensures it generates a standalone HTML file.
Assign unique integer or string IDs to each node using `net.add_node(id, ...)` or ensure lists passed to `net.add_nodes()` contain unique identifiers.
Consult both the Pyvis documentation (specifically `network.Network.add_node()` and `network.Network.add_edge()` docs) and the original VisJS documentation for supported attributes and their Python equivalents. Experimentation may be required.
After interactively adjusting settings via `show_buttons()`, use the 'generate options' button in the UI to copy the JSON configuration. Then, pass this JSON string to `net.set_options(json_string)` in your Python script to save the desired layout and interaction configurations.
Leverage NetworkX for graph creation, manipulation, and analysis. Integrate Pyvis at the visualization stage by converting your NetworkX graph into a Pyvis network using `net.from_nx()`.
Install the library using pip: `pip install pyvis`
Add the import statement at the beginning of your script: `from pyvis.network import Network`
Define the group and its properties within the network options, for example: `net.set_options({'groups': {'my_group': {'color': 'red'}}})` before adding nodes with that group.Manually iterate through your DataFrame to extract node and edge data, then add them to the 'Network' object using `net.add_node()` and `net.add_edge()`.