{ "cells": [ { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "G = nx.complete_graph(5)\n", "undirected = ipycytoscape.CytoscapeWidget()\n", "undirected.graph.add_graph_from_networkx(G)\n", "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": null, "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": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "source": [ "from random import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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" ] } ], "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.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }