Skip to content

Instantly share code, notes, and snippets.

@versae
Last active August 29, 2015 13:57
Show Gist options
  • Save versae/9826274 to your computer and use it in GitHub Desktop.
Save versae/9826274 to your computer and use it in GitHub Desktop.
Neo4j IPython integration through neo4jrestclient
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Neo4j IPython"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": "Neo4j IPython integration through neo4jrestclient"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "First we need the development version of `neo4jrestclient`\n```bash\npip install -U git+https://github.com/versae/neo4j-rest-client.git\n```"
},
{
"cell_type": "code",
"collapsed": false,
"input": "from neo4jrestclient.client import GraphDatabase, Node, Relationship\ngdb = GraphDatabase(url=\"http://localhost:7474\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "gdb.query(\"MATCH (me)-[r]-() RETURN me, r LIMIT 10\")",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "\n <style type=\"text/css\">\n \n #d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6 path.link {\n fill: none;\n stroke-width: 1.5px;\n }\n #d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6 .node {\n /*fill: #ccc;*/\n stroke: #333;\n stroke-width: 1.5px;\n }\n #d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6 text {\n font: 10px sans-serif;\n pointer-events: none;\n }\n #d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6 text.shadow {\n stroke: #fff;\n stroke-width: 3px;\n stroke-opacity: .8;\n }\n #d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6 .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_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6\">\n MATCH (me)-[r]-() RETURN me, r LIMIT 10\n </a>\n </div>\n <div id=\"d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6\" class=\"accordion-body in collapse\">\n <div class=\"accordion-inner\">\n <div id=\"d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6_d3c\">\n <select id=\"d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6_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['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'] = {};\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].graph = {\"directed\": true, \"multigraph\": true, \"links\": [{\"source\": \"2\", \"stroke\": \"black\", \"target\": \"3\", \"properties\": {\"since\": 1995}, \"id\": \"0\", \"label\": \"Hate\\u015b\"}, {\"source\": \"49\", \"stroke\": \"black\", \"target\": \"50\", \"properties\": {\"since\": 1990}, \"id\": \"1\", \"label\": \"loves\"}, {\"source\": \"51\", \"stroke\": \"black\", \"target\": \"52\", \"properties\": {\"since\": 1991}, \"id\": \"2\", \"label\": \"loves\"}, {\"source\": \"53\", \"stroke\": \"black\", \"target\": \"54\", \"properties\": {\"since\": 1992}, \"id\": \"3\", \"label\": \"loves\"}, {\"source\": \"55\", \"stroke\": \"black\", \"target\": \"56\", \"properties\": {\"since\": 1993}, \"id\": \"4\", \"label\": \"loves\"}], \"graph\": [], \"nodes\": {\"2\": {\"properties\": {\"name\": \"J\\u00f3hn Doe\", \"place\": \"Texa\\u015b\"}, \"id\": \"2\", \"label\": \"2\", \"fill\": \"white\"}, \"3\": {\"properties\": {\"name\": \"M\\u00edchael Doe\", \"place\": \"T\\u00edjuana\"}, \"id\": \"3\", \"label\": \"3\", \"fill\": \"white\"}, \"49\": {\"properties\": {\"name\": \"William 0\"}, \"id\": \"49\", \"label\": \"49\", \"fill\": \"white\"}, \"54\": {\"properties\": {\"name\": \"Rose 2\"}, \"id\": \"54\", \"label\": \"54\", \"fill\": \"white\"}, \"55\": {\"properties\": {\"name\": \"William 3\"}, \"id\": \"55\", \"label\": \"55\", \"fill\": \"white\"}, \"56\": {\"properties\": {\"name\": \"Rose 3\"}, \"id\": \"56\", \"label\": \"56\", \"fill\": \"white\"}, \"50\": {\"properties\": {\"name\": \"Rose 0\"}, \"id\": \"50\", \"label\": \"50\", \"fill\": \"white\"}, \"51\": {\"properties\": {\"name\": \"William 1\"}, \"id\": \"51\", \"label\": \"51\", \"fill\": \"white\"}, \"52\": {\"properties\": {\"name\": \"Rose 1\"}, \"id\": \"52\", \"label\": \"52\", \"fill\": \"white\"}, \"53\": {\"properties\": {\"name\": \"William 2\"}, \"id\": \"53\", \"label\": \"53\", \"fill\": \"white\"}}, \"properties\": [\"name\", \"place\"]};\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].container_id = \"d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6_d3c\";\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].container = \"#d3_id_55dc5ad6-b7d7-11e3-903e-f04da29ca5d6_d3c\";\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].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['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].graph,\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].container,\n null,\n 300\n );\n }\n if (!window.d3) {\n $.getScript(\n \"//d3js.org/d3.v2.js?2.9.1\",\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].render\n );\n } else {\n neo4jrestclient['55dc5ad6-b7d7-11e3-903e-f04da29ca5d6'].render();\n }\n </script>\n ",
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": "<neo4jrestclient.query.QuerySequence at 0x7f663c046710>"
}
],
"prompt_number": 3
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment