{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# Javascript library in a notebook: vis.js\n", "\n", "Tries [visjs](http://visjs.org/) in a notebook. It is another way to represent graphs, networks in a notebook. It works better in Chrome."]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/html": ["
run previous cell, wait for 2 seconds
\n", ""], "text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import add_notebook_menu\n", "add_notebook_menu()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Simple example"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", "\n"], "text/plain": [""]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import RenderJS\n", "css = ['http://www.xavierdupre.fr/js/visjs/vis.min.css']\n", "libs = [dict(path='http://www.xavierdupre.fr/js/visjs/vis.min.js', name='vis')]\n", "script = \"\"\"\n", " var nodes = new vis.DataSet([\n", " {id: 1, label: 'Node 1'},\n", " {id: 2, label: 'Node 2'},\n", " {id: 3, label: 'Node 3'},\n", " {id: 4, label: 'Node 4'},\n", " {id: 5, label: 'Node 5'}\n", " ]);\n", "\n", " // create an array with edges\n", " var edges = new vis.DataSet([\n", " {from: 1, to: 3},\n", " {from: 1, to: 2},\n", " {from: 2, to: 4},\n", " {from: 2, to: 5},\n", " {from: 3, to: 3}\n", " ]);\n", "\n", " // create a network\n", " var data = {\n", " nodes: nodes,\n", " edges: edges\n", " };\n", " var options = {};\n", " var container = document.getElementById('__ID__');\n", " var network = new vis.Network(container, data, options);\n", "\"\"\"\n", "jr = RenderJS(script, css=css, libs=libs)\n", "jr"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["
\n", "\n", "\n"]}], "source": ["print(jr._repr_html_())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## More complex example"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", "\n"], "text/plain": [""]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import RenderJS\n", "css = ['http://www.xavierdupre.fr/js/visjs/vis-network.min.css']\n", "libs = [dict(path='http://www.xavierdupre.fr/js/visjs/vis.min.js', name='vis')]\n", "script = \"\"\"\n", "var nodes = null;\n", "var edges = null;\n", "var network = null;\n", "\n", "function draw() {\n", " // create people.\n", " // value corresponds with the age of the person\n", " nodes = [\n", " {id: 1, value: 2, label: 'Algie' },\n", " {id: 2, value: 31, label: 'Alston'},\n", " {id: 3, value: 12, label: 'Barney'},\n", " {id: 4, value: 16, label: 'Coley' },\n", " {id: 5, value: 17, label: 'Grant' },\n", " {id: 6, value: 15, label: 'Langdon'},\n", " {id: 7, value: 6, label: 'Lee'},\n", " {id: 8, value: 5, label: 'Merlin'},\n", " {id: 9, value: 30, label: 'Mick'},\n", " {id: 10, value: 18, label: 'Tod'},\n", " ];\n", "\n", " // create connections between people\n", " // value corresponds with the amount of contact between two people\n", " edges = [\n", " {from: 2, to: 8, value: 3, title: '3 emails per week'},\n", " {from: 2, to: 9, value: 5, title: '5 emails per week'},\n", " {from: 2, to: 10,value: 1, title: '1 emails per week'},\n", " {from: 4, to: 6, value: 8, title: '8 emails per week'},\n", " {from: 5, to: 7, value: 2, title: '2 emails per week'},\n", " {from: 4, to: 5, value: 1, title: '1 emails per week'},\n", " {from: 9, to: 10,value: 2, title: '2 emails per week'},\n", " {from: 2, to: 3, value: 6, title: '6 emails per week'},\n", " {from: 3, to: 9, value: 4, title: '4 emails per week'},\n", " {from: 5, to: 3, value: 1, title: '1 emails per week'},\n", " {from: 2, to: 7, value: 4, title: '4 emails per week'}\n", " ];\n", "\n", " // Instantiate our network object.\n", " var container = document.getElementById('__ID__');\n", " var data = {\n", " nodes: nodes,\n", " edges: edges\n", " };\n", " var options = {\n", " nodes: {\n", " shape: 'dot',\n", " scaling:{\n", " label: {\n", " min:8,\n", " max:20\n", " }\n", " }\n", " }\n", " };\n", " network = new vis.Network(container, data, options);\n", "}\n", "draw();\n", "\"\"\"\n", "jr = RenderJS(script, css=css, libs=libs, style=\"width: 400px; height: 400px; border: 1px solid lightgray;\")\n", "jr"]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["
\n", "\n", "\n"]}], "source": ["print(jr._repr_html_())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## With the DOT language"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", "\n"], "text/plain": [""]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import RenderJS\n", "css = ['http://www.xavierdupre.fr/js/visjs/vis.min.css']\n", "libs = [dict(path='http://www.xavierdupre.fr/js/visjs/vis.min.js', name='vis')]\n", "script = \"\"\"\n", "var DOTstring = 'digraph {1 -> 1 -> 2; 2 -> 3; 2 -- 4; 2 -> 1 }';\n", "var parsedData = vis.network.convertDot(DOTstring);\n", "var container = document.getElementById('__ID__');\n", "var network = new vis.Network(container, parsedData, parsedData.options);\n", "\"\"\"\n", "jr = RenderJS(script, css=css, libs=libs)\n", "jr"]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", "\n"], "text/plain": [""]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import RenderJS\n", "css = ['http://www.xavierdupre.fr/js/visjs/vis.min.css']\n", "libs = [dict(path='http://www.xavierdupre.fr/js/visjs/vis.min.js', name='vis')]\n", "script = \"\"\"\n", "var DOTstring = 'digraph { d[label=\"longer label\"]; a -> b -> c; b -> d; }';\n", "var parsedData = vis.network.convertDot(DOTstring);\n", "var container = document.getElementById('__ID__');\n", "var network = new vis.Network(container, parsedData, parsedData.options);\n", "\"\"\"\n", "jr = RenderJS(script, css=css, libs=libs)\n", "jr"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["
\n", "\n", "\n"]}], "source": ["print(jr._repr_html_())"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", "\n"], "text/plain": [""]}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import RenderJS\n", "css = ['http://www.xavierdupre.fr/js/visjs/vis.min.css']\n", "libs = [dict(path='http://www.xavierdupre.fr/js/visjs/vis.min.js', name='vis')]\n", "script = \"\"\"\n", "var DOTstring = 'digraph { d[label=\"longer label\"]; a -> b -> c; b -> d; }';\n", "var parsedData = vis.network.convertDot(DOTstring);\n", "var container = document.getElementById('__ID__');\n", "var options = parsedData.options;\n", "var options = {\n", " layout: {\n", " hierarchical: {\n", " direction: \"UD\",\n", " sortMethod: \"directed\"\n", " }\n", " }\n", " };\n", "var network = new vis.Network(container, parsedData, options);\n", "\"\"\"\n", "jr = RenderJS(script, css=css, libs=libs, height=\"200px\")\n", "jr"]}, {"cell_type": "code", "execution_count": 10, "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.7.2"}}, "nbformat": 4, "nbformat_minor": 2}