Skip to content

Commit 335e5c6

Browse files
author
Mark Needham
committed
adding more explanation around viz
1 parent a39e8fc commit 335e5c6

File tree

2 files changed

+178
-72
lines changed

2 files changed

+178
-72
lines changed

generate_notebook.py

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,23 @@ def find_tag(file, tag):
106106

107107
streaming_graph_explanation_text = find_tag(algorithm_file, explanation_tag)
108108

109-
expand_viz_cell = '''\
110-
%%html
111-
<style>
112-
.output_wrapper, .output {
113-
height:auto !important;
114-
max-height:600px;
115-
}
116-
.output_scroll {
117-
box-shadow:none !important;
118-
webkit-box-shadow:none !important;
119-
}
120-
</style>
121-
'''
109+
viz_intro_text = '''## Graph Visualisation
110+
111+
Sometimes a picture can tell more than a table of results and this is often the case with graph algorithms.
112+
Let's see how to create a graph visualization using neovis.js.
113+
114+
First we'll create a div into which we will generate the visualisation.'''
115+
116+
python_to_js_text = '''Next we need to define the query that the visualization will be generated from, along with config
117+
that describes which properties will be used for node size, node colour, and relationship width.
118+
119+
We'll then define a JavaScript variable that contains all our parameters.'''
122120

123-
cypher_read_query = "MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *"
121+
neo_vis_js_text = '''Now we're ready to call neovis.js and generate our graph visualisation.
122+
The following code will create an interactive graph into the div defined above.
123+
It will also extract an image representation of the graph and display that in the cell below.'''
124+
125+
query = "MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *"
124126

125127
labels_json = {
126128
"Page": {
@@ -136,15 +138,76 @@ def find_tag(file, tag):
136138
}
137139
}
138140

139-
viz = '''\
140-
from scripts.vis import generate_vis
141+
setup_js_graph_cell = '''\
142+
from IPython.core.display import display, HTML, Javascript
143+
from string import Template
144+
import json
145+
146+
query = "MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *"
147+
labels_json = {'Page': {'caption': 'name', 'size': 'pagerank'}}
148+
relationships_json = {'LINKS': {'thickness': 'weight', 'caption': False}}
149+
150+
json_graph = {
151+
"query": query,
152+
"labels": labels_json,
153+
"relationships": relationships_json,
154+
"host": host,
155+
"user": user,
156+
"password": password
157+
}
141158
142-
query = "%s"
143-
labels_json = %s
144-
relationships_json = %s
159+
Javascript("""window.jsonGraph={};""".format(json.dumps(json_graph)))'''
145160

146-
generate_vis(host, user, password, query, labels_json, relationships_json)
147-
''' % (cypher_read_query, labels_json, relationships_json)
161+
neo_vis_div_cell = '''\
162+
%%html
163+
<style type="text/css">
164+
.output_wrapper, .output {
165+
height:auto !important;
166+
max-height:600px;
167+
}
168+
.output_scroll {
169+
box-shadow:none !important;
170+
webkit-box-shadow:none !important;
171+
}
172+
173+
#viz {
174+
width: 300px;
175+
height: 350px;
176+
font: 22pt arial;
177+
}
178+
</style>
179+
<div id="viz"></div>'''
180+
181+
neo_vis_js_cell = '''\
182+
%%javascript
183+
requirejs(['neovis.js'], function(NeoVis){
184+
var config = {
185+
container_id: "viz",
186+
server_url: window.jsonGraph.host,
187+
server_user: window.jsonGraph.user,
188+
server_password: window.jsonGraph.password,
189+
labels: window.jsonGraph.labels,
190+
relationships: window.jsonGraph.relationships,
191+
initial_cypher: window.jsonGraph.query
192+
};
193+
194+
let viz = new NeoVis.default(config);
195+
viz.render();
196+
197+
viz.onVisualizationRendered(function(ctx) {
198+
let imageSrc = ctx.canvas.toDataURL();
199+
let kernel = IPython.notebook.kernel;
200+
let command = "image_src = '" + imageSrc + "'";
201+
kernel.execute(command);
202+
203+
var nb = Jupyter.notebook;
204+
var cell = nb.select_next().get_selected_cell();
205+
cell.set_text("HTML('<img id=\\"viz-image\\" width=\\"300px\\" src=\\"%s\\" />' % image_src)")
206+
cell.execute();
207+
});
208+
});'''
209+
210+
display_neo_vis_cell = ''''''
148211

149212
nb = nbf.v4.new_notebook()
150213
nb['cells'] = [nbf.v4.new_markdown_cell(heading_text),
@@ -156,8 +219,13 @@ def find_tag(file, tag):
156219
nbf.v4.new_markdown_cell(streaming_graph_text),
157220
nbf.v4.new_code_cell(run_algorithm),
158221
nbf.v4.new_markdown_cell(streaming_graph_explanation_text),
159-
nbf.v4.new_code_cell(expand_viz_cell),
160-
nbf.v4.new_code_cell(viz)
222+
nbf.v4.new_markdown_cell(viz_intro_text),
223+
nbf.v4.new_code_cell(neo_vis_div_cell),
224+
nbf.v4.new_markdown_cell(python_to_js_text),
225+
nbf.v4.new_code_cell(setup_js_graph_cell),
226+
nbf.v4.new_markdown_cell(neo_vis_js_text),
227+
nbf.v4.new_code_cell(neo_vis_js_cell),
228+
nbf.v4.new_code_cell(display_neo_vis_cell)
161229
]
162230

163231
output_file = 'notebooks/{0}.ipynb'.format(algorithm_name.replace(" ", ""))

notebooks/PageRank.ipynb

Lines changed: 87 additions & 49 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)