Interactive Cytoscape Widget for Jupyter
ipycytoscape is a Python library that provides a Cytoscape widget for Jupyter notebooks and JupyterLab environments. It allows users to visualize and interact with complex networks directly within their Jupyter workflows, leveraging the powerful rendering capabilities of Cytoscape.js. The current version is 1.3.3, with minor releases occurring every few months for bug fixes and compatibility updates.
Common errors
-
Widget not found. This usually happens when the widgets JavaScript hasn't been run.
cause The Jupyter environment (JupyterLab or classic Notebook) has not correctly loaded the ipycytoscape frontend extension.fixFor JupyterLab 3+, ensure `ipycytoscape>=1.2.0` is installed. No manual build should be needed. For classic Notebook, try `jupyter nbextension enable --py --sys-prefix ipycytoscape`. Restart your kernel and browser. -
TraitError: The 'layout' trait of a CytoscapeWidget instance must be a dict or None, but a value of type <class 'str'> was specified.
cause You passed a string directly to `cy.layout` instead of using the `set_layout` method, or the `set_layout` method was used with an invalid parameter type.fixUse the `set_layout` method with the `name` parameter, e.g., `cy.set_layout(name='cola')`. If you need custom layout options, provide a dictionary: `cy.set_layout(name='cose', options={'padding': 10})`. -
ModuleNotFoundError: No module named 'ipycytoscape'
cause The `ipycytoscape` package is not installed in the Python environment where Jupyter is running.fixInstall the package: `pip install ipycytoscape`. Ensure you are installing into the correct Python environment if you use virtual environments or conda environments.
Warnings
- breaking Version 1.2.0 significantly changed how ipycytoscape integrates with JupyterLab. Prior to 1.2.0, an explicit `jupyter lab build` step and Node.js were often required. Version 1.2.0+ no longer requires these for JupyterLab 3 and above.
- gotcha Graphs may not display properly (nodes overlapping at the center) if a layout is not explicitly set using `cy.set_layout()`. Cytoscape.js requires a layout algorithm to arrange nodes and edges.
- gotcha When using `ipycytoscape` in classic Jupyter Notebook (not JupyterLab), you might need to ensure the nbextension is enabled, especially if the widget does not render.
Install
-
pip install ipycytoscape
Imports
- CytoscapeWidget
from ipycytoscape import CytoscapeWidget
Quickstart
import ipycytoscape
import networkx as nx
# Create a Cytoscape widget instance
cy = ipycytoscape.CytoscapeWidget()
# Create a simple graph using NetworkX
G = nx.Graph()
G.add_edge('A', 'B')
G.add_edge('B', 'C')
G.add_node('D')
# Add the graph data to the widget
cy.graph.add_graph_from_networkx(G, directed=False)
# Set a layout; this is crucial for the graph to display properly
cy.set_layout(name='cose') # Other options: 'cola', 'grid', 'circle', etc.
# Display the widget
cy