pyvis

repository·master·Indexed 22 days ago

https://github.com/westhealth/pyvis

A Python library for visualizing networks built on top of the visjs JavaScript visualization library. It allows users to create interactive web-based graphs by instantiating a Network object, adding nodes and edges, and exporting the result to HTML. Pyvis supports importing graph structures from DOT files and NetworkX, provides tools for configuring physics and layout via set_options(), and includes interactive UI elements like select and filter menus for exploring large networks.

Tokens
3K
Snippets
14
Records
21
Agent score
77%

What's inside pyvis

  1. Overview of Pyvis network visualization

    master

    Pyvis is a Python library designed to construct and visualize network graphs within a single workflow. It leverages the VisJS library to provide interactive web-based visualizations.

    Key capabilities include:

    • Customization: Configure nodes and edges individually with specific colors, sizes, labels, and metadata.
    • Interactivity: Graphs support user interactions such as dragging, hovering, and selecting nodes and edges.
    • Layout Control: Access and tweak layout algorithms to optimize the rendering of large-scale graphs.
  2. Use the configuration UI for dynamic tweaking

    master

    You can inject a UI into your HTML output to dynamically adjust network settings (like physics) in the browser. Use show_buttons(filter_=['physics']) to limit the UI to specific categories. You can then copy the generated options from the UI and use set_options(options_string) in your Python code to finalize the configuration.

    # Show only physics settings in the UI
    net.show_buttons(filter_=['physics'])
    
    # Later, after experimenting in the browser:
    net.set_options(generated_options_string)
  3. Quick Start: Create a basic network visualization

    master

    To create a simple network visualization, instantiate a Network object, add nodes using add_node(), add edges using add_edge(), and then export the visualization to an HTML file using show().

    from pyvis.network import Network
    
    g = Network()
    g.add_node(0)
    g.add_node(1)
    g.add_edge(0, 1)
    g.show("basic.html")
  4. Initialize a Network instance

    master

    To start creating a network visualization, you must instantiate the Network class. You can optionally pass parameters like height, width, bgcolor, and font_color to the constructor.

    from pyvis.network import Network
    
    # Basic instantiation
    net = Network()
    
    # Custom instantiation
    got_net = Network(height="750px", width="100%", bgcolor="#222222", font_color="white")
  5. Integrate with NetworkX

    master

    You can convert a networkx.Graph object directly into a Pyvis network using the from_nx(nx_graph) method. NetworkX node properties with names matching Pyvis attributes (like title) are translated automatically.

    import networkx as nx
    from pyvis.network import Network
    
    nx_graph = nx.cycle_graph(10)
    # Add properties to NetworkX nodes
    nx_graph.nodes[1]['title'] = 'Number 1'
    
    nt = Network('500px', '500px')
    tn.from_nx(nx_graph)
    tn.show('nx.html')
  6. Use Pyvis in Jupyter Notebooks

    master

    To embed a Pyvis network in a Jupyter Notebook, instantiate the Network class with notebook=True.

    Note for Chrome users: If the graph fails to render, you may need to pass cdn_resources='remote' or cdn_resources='inline' to the Network constructor.

    # Standard Jupyter usage
    from pyvis.network import Network
    net = Network(notebook=True)
    
    # If using Chrome and experiencing rendering issues:
    net = Network(notebook=True, cdn_resources='remote')
  7. Enable node highlighting and filtering

    master

    Pyvis provides two interactive features for exploring large networks:

    1. Highlighting: Set select_menu=True in the Network constructor. This adds a dropdown that allows you to select a node; the selected node and its neighbors will be highlighted while others are greyed out.
    2. Filtering: Set filter_menu=True in the Network constructor. This allows you to build queries to filter nodes or edges based on specific attributes and values.
    # Enable highlighting via dropdown
    net_highlight = Network(select_menu=True)
    
    # Enable attribute filtering
    net_filter = Network(filter_menu=True)
  8. Use Select and Filter menus

    master

    Pyvis provides interactive UI elements to explore large networks:

    • select_menu=True: Adds a dropdown menu to select a node. Selecting a node highlights it and its neighboring edges and nodes.
    • filter_menu=True: Adds a menu to filter nodes and edges based on their properties (e.g., group, color, or custom attributes). Any custom property added to a node or edge will be available in this menu.

    Example initialization:

    got_net = Network(notebook=True, select_menu=True, filter_menu=True)
  9. Initialize a Pyvis Network

    master

    To create a network visualization, import the Network class from pyvis.network. When working in Jupyter notebooks, set notebook=True. The cdn_resources parameter controls how dependencies are loaded: use 'in_line' to embed resources directly or 'remote' to load them from a CDN.

    Basic usage involves creating a Network instance, adding nodes and edges, and calling .show("filename.html") to generate the visualization.

    from pyvis.network import Network
    
    # notebook=True for Jupyter, cdn_resources='in_line' embeds resources
    g = Network(notebook=True, cdn_resources='in_line')
    g.add_nodes(range(5))
    g.add_edges([(0, 2), (0, 3), (0, 4), (1, 1), (1, 3), (1, 2)])
    g.show("example.html")