{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NetworkX " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ipycytoscape\n", "import ipywidgets as widgets\n", "import networkx as nx\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Undirected graph" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8ba325ac0dd1422eafefe5cfef9389ef", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'node', 'css': {'background-c…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "G = nx.complete_graph(5)\n", "undirected = ipycytoscape.CytoscapeWidget()\n", "undirected.graph.add_graph_from_networkx(G)\n", "display(undirected)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You can also add more nodes \n", "\n", "The above graph should update when you run the next cell" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "G2 = nx.Graph()\n", "G2.add_node('separate node 1')\n", "G2.add_node('separate node 2')\n", "G2.add_edge('separate node 1', 'separate node 2')\n", "undirected.graph.add_graph_from_networkx(G2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fully directed graphs\n", "\n", "`add_graph_from_networkx` takes an argument `directed` that if True will ensure all edges given the directed class, which will style them with an arrow." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "09a5b05125664ff8a14fba57bcb3d3a6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'node', 'css': {'background-c…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "G = nx.complete_graph(5)\n", "directed = ipycytoscape.CytoscapeWidget()\n", "directed.graph.add_graph_from_networkx(G, directed=True)\n", "directed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mixed graphs\n", "\n", "You can also make graphs with both directed and undirected edges by adding 'directed' to the 'classes' attribute of the edge data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from random import random" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "716cd55e23ee4996969ce3d24e0bc476", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'node', 'css': {'background-c…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "G = nx.complete_graph(5)\n", "for s, t, data in G.edges(data=True):\n", " if random() > .5:\n", " G[s][t]['classes'] = 'directed'\n", "\n", "mixed = ipycytoscape.CytoscapeWidget()\n", "mixed.graph.add_graph_from_networkx(G)\n", "mixed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom networkx Node Objects\n", "\n", "The most common choices for Nodes in networkx are numbers or strings as shown above. A node can also be any hashable object (except None) which work as well." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fc59b48cb7dc495e93a49fa90eb7eeb6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'node', 'css': {'background-c…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "class Node:\n", " def __init__(self, name):\n", " self.name = name\n", " \n", " def __str__(self):\n", " return \"Node: \" + str(self.name)\n", "\n", "n1 = Node(\"node 1\")\n", "n2 = Node(\"node 2\")\n", " \n", "G = nx.Graph()\n", "\n", "G.add_node(n1)\n", "G.add_node(n2)\n", "\n", "G.add_edge(n1, n2)\n", "\n", "w = ipycytoscape.CytoscapeWidget()\n", "w.graph.add_graph_from_networkx(G)\n", "w" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom networkx Node Objects that inherit from ipycytoscape.Node\n", "\n", "While custom networkx Node objects work, they do not allow as much control over formatting as you may need. The easiest way to achieve customization with custom Node objects is to subclass ipycytoscape.Node as show below." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1233c00a97b24eaaaf262d63905a910f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "CytoscapeWidget(cytoscape_layout={'name': 'cola'}, cytoscape_style=[{'selector': 'node.class1', 'css': {'backg…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "class CustomNode(ipycytoscape.Node):\n", " def __init__(self, name, classes=''):\n", " super().__init__()\n", " self.data['id'] = name\n", " self.classes = classes\n", "\n", "n1 = CustomNode(\"node 1\", classes='class1')\n", "n2 = CustomNode(\"node 2\", classes='class2')\n", " \n", "G = nx.Graph()\n", "\n", "G.add_node(n1)\n", "G.add_node(n2)\n", "\n", "G.add_edge(n1, n2)\n", "\n", "custom_inherited = ipycytoscape.CytoscapeWidget()\n", "custom_inherited.graph.add_graph_from_networkx(G)\n", "custom_inherited.set_style([\n", " {\n", " 'selector': 'node.class1',\n", " 'css': {\n", " 'background-color': 'red'\n", " }\n", " },\n", " {\n", " 'selector': 'node.class2',\n", " 'css': {\n", " 'background-color': 'green'\n", " }\n", " }])\n", "custom_inherited" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }