Skip to content

Instantly share code, notes, and snippets.

@nogawanogawa
Created February 7, 2019 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nogawanogawa/c40ce47c54c897fca0bc39e7a43fd2c9 to your computer and use it in GitHub Desktop.
Save nogawanogawa/c40ce47c54c897fca0bc39e7a43fd2c9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from neo4jrestclient.client import GraphDatabase\n",
"\n",
"url = \"http://<ID>:<PASSWORD>@localhost:7474/db/data/\"\n",
"gdb = GraphDatabase(url)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"alice = gdb.nodes.create(name=\"Alice\", age=30)\n",
"bob = gdb.nodes.create(name=\"Bob\", age=30)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Neo4j Relationship: http://localhost:7474/db/data/relationship/3>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bob.relationships.create(\"Knows\", alice, since=1980)\n",
"alice.relationships.create(\"Knows\", bob, since=1983)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <style type=\"text/css\">\n",
" \n",
" #d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7 path.link {\n",
" fill: none;\n",
" stroke-width: 1.5px;\n",
" }\n",
" #d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7 .node {\n",
" /*fill: #ccc;*/\n",
" stroke: #333;\n",
" stroke-width: 1.5px;\n",
" }\n",
" #d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7 text {\n",
" font: 10px sans-serif;\n",
" pointer-events: none;\n",
" }\n",
" #d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7 text.shadow {\n",
" stroke: #fff;\n",
" stroke-width: 3px;\n",
" stroke-opacity: .8;\n",
" }\n",
" #d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7 .node.sticky {\n",
" /* stroke-width: 2px; */\n",
" }\n",
" \n",
" </style>\n",
"\n",
" <div class=\"accordion\">\n",
" <div class=\"accordion-group\">\n",
" <div class=\"accordion-heading\">\n",
" <a class=\"accordion-toggle collapsed\"\n",
" data-toggle=\"collapse\" data-parent=\"\"\n",
" href=\"#d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7\">\n",
" MATCH (n)-[r]-(m) RETURN n, r, m\n",
" </a>\n",
" </div>\n",
" <div id=\"d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7\" class=\"accordion-body in collapse\">\n",
" <div class=\"accordion-inner\">\n",
" <div id=\"d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7_d3c\">\n",
" <select id=\"d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7_d3c_display\">\n",
" <option value=\"\">ID</option>\n",
" </select>\n",
" </div>\n",
" </div>\n",
" </div>\n",
" </div>\n",
" </div>\n",
"\n",
" <script>\n",
" var neo4jrestclient = window.neo4jrestclient || {};\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'] = {};\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].graph = {\"directed\": true, \"graph\": [], \"multigraph\": true, \"links\": [{\"id\": \"1\", \"source\": \"0\", \"stroke\": \"black\", \"target\": \"1\", \"label\": \"Knows\", \"properties\": {\"since\": 1983}}, {\"id\": \"0\", \"source\": \"1\", \"stroke\": \"black\", \"target\": \"0\", \"label\": \"Knows\", \"properties\": {\"since\": 1980}}, {\"id\": \"3\", \"source\": \"2\", \"stroke\": \"black\", \"target\": \"3\", \"label\": \"Knows\", \"properties\": {\"since\": 1983}}, {\"id\": \"2\", \"source\": \"3\", \"stroke\": \"black\", \"target\": \"2\", \"label\": \"Knows\", \"properties\": {\"since\": 1980}}], \"nodes\": {\"0\": {\"fill\": \"white\", \"id\": \"0\", \"label\": \"0\", \"properties\": {\"name\": \"Alice\", \"age\": 30}}, \"1\": {\"fill\": \"white\", \"id\": \"1\", \"label\": \"1\", \"properties\": {\"name\": \"Bob\", \"age\": 30}}, \"2\": {\"fill\": \"white\", \"id\": \"2\", \"label\": \"2\", \"properties\": {\"name\": \"Alice\", \"age\": 30}}, \"3\": {\"fill\": \"white\", \"id\": \"3\", \"label\": \"3\", \"properties\": {\"name\": \"Bob\", \"age\": 30}}}, \"properties\": [\"age\", \"name\"]};\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].container_id = \"d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7_d3c\";\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].container = \"#d3_id_44bed518-2ad6-11e9-8438-88e9fe5c9bf7_d3c\";\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].render = function () {\n",
" (function (graph, container, width, height) {\n",
" \n",
" var links = graph.links;\n",
" var nodes = graph.nodes;\n",
"\n",
" // Compute the distinct nodes from the links.\n",
" links.forEach(function(link) {\n",
" link.source = (nodes[link.source] ||\n",
" (nodes[link.source] = {name: link.source}));\n",
" link.target = (nodes[link.target] ||\n",
" (nodes[link.target] = {name: link.target}));\n",
" });\n",
"\n",
" var w = width || $(container).width(), h = height;\n",
"\n",
" var force = d3.layout.force()\n",
" .nodes(d3.values(nodes))\n",
" .links(links)\n",
" .size([w, h])\n",
" .linkDistance(60)\n",
" .charge(-300)\n",
" .on(\"tick\", tick)\n",
" .start();\n",
"\n",
" var svg = d3.select(container).append(\"svg:svg\")\n",
" .attr(\"width\", w)\n",
" .attr(\"height\", h);\n",
"\n",
" // Per-type markers, as they don't inherit styles.\n",
" svg.append(\"svg:defs\").selectAll(\"marker\")\n",
" .data([\"arrow\"])\n",
" .enter().append(\"svg:marker\")\n",
" .attr(\"id\", String)\n",
" .attr(\"viewBox\", \"0 -5 10 10\")\n",
" .attr(\"refX\", 15)\n",
" .attr(\"refY\", -1.5)\n",
" .attr(\"markerWidth\", 6)\n",
" .attr(\"markerHeight\", 6)\n",
" .attr(\"orient\", \"auto\")\n",
" .append(\"svg:path\")\n",
" .attr(\"d\", \"M0,-5L10,0L0,5\");\n",
"\n",
" var path = svg.append(\"svg:g\").selectAll(\"path\")\n",
" .data(force.links())\n",
" .enter().append(\"svg:path\")\n",
" .attr(\"class\", function(d) { return \"link \" + d.stroke; })\n",
" .attr(\"stroke\", function(d) { return d.stroke; })\n",
" .attr(\"marker-end\", function(d) { return \"url(#arrow)\"; });\n",
"\n",
" var circle = svg.append(\"svg:g\").selectAll(\"circle\")\n",
" .data(force.nodes())\n",
" .enter().append(\"svg:circle\")\n",
" .attr(\"fill\", function(d) { return d.fill; })\n",
" .attr(\"r\", 6)\n",
" .attr(\"class\", \"node\")\n",
" .call(force.drag)\n",
" .on(\"mousedown\", function(d) {\n",
" d.fixed = true;\n",
" d3.select(this).classed(\"sticky\", true);\n",
" });\n",
"\n",
" var text = svg.append(\"svg:g\").selectAll(\"g\")\n",
" .data(force.nodes())\n",
" .enter().append(\"svg:g\");\n",
"\n",
" // A copy of the text with a thick white stroke for legibility.\n",
" text.append(\"svg:text\")\n",
" .attr(\"x\", 8)\n",
" .attr(\"y\", \".31em\")\n",
" .attr(\"class\", \"shadow\")\n",
" .text(function(d) { return d.label; });\n",
"\n",
" text.append(\"svg:text\")\n",
" .attr(\"x\", 8)\n",
" .attr(\"y\", \".31em\")\n",
" .attr(\"class\", \"front\")\n",
" .text(function(d) { return d.label; });\n",
"\n",
" // Use elliptical arc path segments to doubly-encode directionality.\n",
" function tick() {\n",
" path.attr(\"d\", function(d) {\n",
" var dx = d.target.x - d.source.x,\n",
" dy = d.target.y - d.source.y,\n",
" dr = Math.sqrt(dx * dx + dy * dy);\n",
" return (\"M\" + d.source.x + \",\" + d.source.y + \"A\"\n",
" + dr + \",\" + dr + \" 0 0,1 \" + d.target.x + \",\"\n",
" + d.target.y);\n",
" });\n",
"\n",
" circle.attr(\"transform\", function(d) {\n",
" return \"translate(\" + d.x + \",\" + d.y + \")\";\n",
" });\n",
"\n",
" text.attr(\"transform\", function(d) {\n",
" return \"translate(\" + d.x + \",\" + d.y + \")\";\n",
" });\n",
" }\n",
"\n",
" // Display options\n",
" var display = $(container + \"_display\");\n",
" graph.properties.forEach(function (property) {\n",
" var option = $(\"<OPTION/>\");\n",
" option.text(property);\n",
" option.attr(\"value\", property);\n",
" display.append(option);\n",
" });\n",
" display.on(\"change\", function () {\n",
" var selected = $(this).find(\":selected\").val(),\n",
" displayFunc;\n",
" if (selected.length !== 0) {\n",
" displayFunc = function(d) {\n",
" return d.properties[selected];\n",
" }\n",
" } else {\n",
" displayFunc = function(d) {\n",
" return d.label;\n",
" }\n",
" }\n",
" text.select(\"text.front\").text(displayFunc);\n",
" text.select(\"text.shadow\").text(displayFunc);\n",
" });\n",
" \n",
" })(\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].graph,\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].container,\n",
" null,\n",
" 300\n",
" );\n",
" }\n",
" if (!window.d3) {\n",
" $.getScript(\n",
" \"//d3js.org/d3.v2.js?2.9.1\",\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].render\n",
" );\n",
" } else {\n",
" neo4jrestclient['44bed518-2ad6-11e9-8438-88e9fe5c9bf7'].render();\n",
" }\n",
" </script>\n",
" "
],
"text/plain": [
"<neo4jrestclient.query.QuerySequence at 0x111cd3390>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdb.query(\"MATCH (n)-[r]-(m) RETURN n, r, m\", data_contents=True)"
]
},
{
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment