Skip to content

Commit 0aa25d0

Browse files
author
Mark Needham
committed
add more text around each notebook
1 parent dd0c8b1 commit 0aa25d0

10 files changed

+407
-178
lines changed

generate_notebook.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,33 @@ def find_tag(file, tag):
4040
stream_graph_tag = sys.argv[4]
4141

4242

43-
text = """\
43+
heading_text = """\
4444
# {0}
4545
{1}
46+
47+
First we'll import the Neo4j driver and Pandas libraries:
4648
""".format(algorithm_name, algorithm_description)
4749

4850
imports = """\
4951
from neo4j.v1 import GraphDatabase, basic_auth
5052
import pandas as pd
5153
import os"""
5254

55+
driver_setup_text = """\
56+
Next let's create an instance of the Neo4j driver which we'll use to execute our queries.
57+
"""
58+
5359
driver_setup = """\
5460
host = os.environ.get("NEO4J_HOST", "bolt://localhost")
5561
user = os.environ.get("NEO4J_USER", "neo4j")
5662
password = os.environ.get("NEO4J_PASSWORD", "neo")
5763
driver = GraphDatabase.driver(host, auth=basic_auth(user, password))"""
5864

59-
create_graph_content = find_tag(cypher_file, "create-sample-graph")
60-
streaming_query_content = find_tag(cypher_file, stream_graph_tag)
65+
create_graph_text = """\
66+
Now let's create a sample graph that we'll run the algorithm against.
67+
"""
6168

69+
create_graph_content = find_tag(cypher_file, "create-sample-graph")
6270
create_graph = """\
6371
create_graph_query = '''\
6472
@@ -69,6 +77,11 @@ def find_tag(file, tag):
6977
result = session.write_transaction(lambda tx: tx.run(create_graph_query))
7078
print("Stats: " + str(result.consume().metadata.get("stats", {})))""" % create_graph_content
7179

80+
streaming_graph_text = """\
81+
Finally we can run the algorithm by executing the following query:
82+
"""
83+
84+
streaming_query_content = find_tag(cypher_file, stream_graph_tag)
7285
run_algorithm = '''\
7386
streaming_query = """\
7487
@@ -82,10 +95,13 @@ def find_tag(file, tag):
8295
df''' % streaming_query_content
8396

8497
nb = nbf.v4.new_notebook()
85-
nb['cells'] = [nbf.v4.new_markdown_cell(text),
98+
nb['cells'] = [nbf.v4.new_markdown_cell(heading_text),
8699
nbf.v4.new_code_cell(imports),
100+
nbf.v4.new_markdown_cell(driver_setup_text),
87101
nbf.v4.new_code_cell(driver_setup),
102+
nbf.v4.new_markdown_cell(create_graph_text),
88103
nbf.v4.new_code_cell(create_graph),
104+
nbf.v4.new_markdown_cell(streaming_graph_text),
89105
nbf.v4.new_code_cell(run_algorithm)]
90106

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

notebooks/AllPairsShortestPath.ipynb

Lines changed: 100 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"source": [
77
"# All Pairs Shortest Path\n",
88
"_All Pairs Shortest Path_ calculates the shortest weight path between all pairs of nodes.\n",
9-
"An algorithm to solve this is Floyd Warshall or Parallel Johnson's algorithm.\n"
9+
"An algorithm to solve this is Floyd Warshall or Parallel Johnson's algorithm.\n",
10+
"\n",
11+
"First we'll import the Neo4j driver and Pandas libraries:\n"
1012
]
1113
},
1214
{
@@ -20,6 +22,13 @@
2022
"import os"
2123
]
2224
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"Next let's create an instance of the Neo4j driver which we'll use to execute our queries.\n"
30+
]
31+
},
2332
{
2433
"cell_type": "code",
2534
"execution_count": 2,
@@ -32,6 +41,13 @@
3241
"driver = GraphDatabase.driver(host, auth=basic_auth(user, password))"
3342
]
3443
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"Now let's create a sample graph that we'll run the algorithm against.\n"
49+
]
50+
},
3551
{
3652
"cell_type": "code",
3753
"execution_count": 3,
@@ -67,6 +83,13 @@
6783
" print(\"Stats: \" + str(result.consume().metadata.get(\"stats\", {})))"
6884
]
6985
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Finally we can run the algorithm by executing the following query:\n"
91+
]
92+
},
7093
{
7194
"cell_type": "code",
7295
"execution_count": 4,
@@ -101,150 +124,150 @@
101124
" <tbody>\n",
102125
" <tr>\n",
103126
" <th>0</th>\n",
104-
" <td>134</td>\n",
105-
" <td>134</td>\n",
127+
" <td>96</td>\n",
128+
" <td>96</td>\n",
106129
" <td>0.0</td>\n",
107130
" </tr>\n",
108131
" <tr>\n",
109132
" <th>1</th>\n",
110-
" <td>16</td>\n",
111-
" <td>16</td>\n",
133+
" <td>97</td>\n",
134+
" <td>97</td>\n",
112135
" <td>0.0</td>\n",
113136
" </tr>\n",
114137
" <tr>\n",
115138
" <th>2</th>\n",
116-
" <td>177</td>\n",
117-
" <td>134</td>\n",
118-
" <td>110.0</td>\n",
139+
" <td>97</td>\n",
140+
" <td>196</td>\n",
141+
" <td>40.0</td>\n",
119142
" </tr>\n",
120143
" <tr>\n",
121144
" <th>3</th>\n",
122-
" <td>177</td>\n",
123-
" <td>177</td>\n",
124-
" <td>0.0</td>\n",
145+
" <td>97</td>\n",
146+
" <td>197</td>\n",
147+
" <td>70.0</td>\n",
125148
" </tr>\n",
126149
" <tr>\n",
127150
" <th>4</th>\n",
128-
" <td>177</td>\n",
129-
" <td>178</td>\n",
130-
" <td>40.0</td>\n",
151+
" <td>97</td>\n",
152+
" <td>198</td>\n",
153+
" <td>110.0</td>\n",
131154
" </tr>\n",
132155
" <tr>\n",
133156
" <th>5</th>\n",
134-
" <td>177</td>\n",
135-
" <td>179</td>\n",
136-
" <td>70.0</td>\n",
157+
" <td>96</td>\n",
158+
" <td>97</td>\n",
159+
" <td>50.0</td>\n",
137160
" </tr>\n",
138161
" <tr>\n",
139162
" <th>6</th>\n",
140-
" <td>16</td>\n",
141-
" <td>134</td>\n",
142-
" <td>110.0</td>\n",
163+
" <td>96</td>\n",
164+
" <td>195</td>\n",
165+
" <td>50.0</td>\n",
143166
" </tr>\n",
144167
" <tr>\n",
145168
" <th>7</th>\n",
146-
" <td>16</td>\n",
147-
" <td>178</td>\n",
148-
" <td>40.0</td>\n",
169+
" <td>96</td>\n",
170+
" <td>196</td>\n",
171+
" <td>50.0</td>\n",
149172
" </tr>\n",
150173
" <tr>\n",
151174
" <th>8</th>\n",
152-
" <td>16</td>\n",
153-
" <td>179</td>\n",
154-
" <td>70.0</td>\n",
175+
" <td>96</td>\n",
176+
" <td>197</td>\n",
177+
" <td>80.0</td>\n",
155178
" </tr>\n",
156179
" <tr>\n",
157180
" <th>9</th>\n",
158-
" <td>15</td>\n",
159-
" <td>15</td>\n",
160-
" <td>0.0</td>\n",
181+
" <td>96</td>\n",
182+
" <td>198</td>\n",
183+
" <td>120.0</td>\n",
161184
" </tr>\n",
162185
" <tr>\n",
163186
" <th>10</th>\n",
164-
" <td>15</td>\n",
165-
" <td>16</td>\n",
166-
" <td>50.0</td>\n",
187+
" <td>195</td>\n",
188+
" <td>195</td>\n",
189+
" <td>0.0</td>\n",
167190
" </tr>\n",
168191
" <tr>\n",
169192
" <th>11</th>\n",
170-
" <td>179</td>\n",
171-
" <td>134</td>\n",
193+
" <td>195</td>\n",
194+
" <td>196</td>\n",
172195
" <td>40.0</td>\n",
173196
" </tr>\n",
174197
" <tr>\n",
175198
" <th>12</th>\n",
176-
" <td>15</td>\n",
177-
" <td>134</td>\n",
178-
" <td>120.0</td>\n",
199+
" <td>196</td>\n",
200+
" <td>196</td>\n",
201+
" <td>0.0</td>\n",
179202
" </tr>\n",
180203
" <tr>\n",
181204
" <th>13</th>\n",
182-
" <td>15</td>\n",
183-
" <td>177</td>\n",
184-
" <td>50.0</td>\n",
205+
" <td>195</td>\n",
206+
" <td>197</td>\n",
207+
" <td>70.0</td>\n",
185208
" </tr>\n",
186209
" <tr>\n",
187210
" <th>14</th>\n",
188-
" <td>15</td>\n",
189-
" <td>178</td>\n",
190-
" <td>50.0</td>\n",
211+
" <td>196</td>\n",
212+
" <td>197</td>\n",
213+
" <td>30.0</td>\n",
191214
" </tr>\n",
192215
" <tr>\n",
193216
" <th>15</th>\n",
194-
" <td>179</td>\n",
195-
" <td>179</td>\n",
196-
" <td>0.0</td>\n",
217+
" <td>195</td>\n",
218+
" <td>198</td>\n",
219+
" <td>110.0</td>\n",
197220
" </tr>\n",
198221
" <tr>\n",
199222
" <th>16</th>\n",
200-
" <td>15</td>\n",
201-
" <td>179</td>\n",
202-
" <td>80.0</td>\n",
223+
" <td>196</td>\n",
224+
" <td>198</td>\n",
225+
" <td>70.0</td>\n",
203226
" </tr>\n",
204227
" <tr>\n",
205228
" <th>17</th>\n",
206-
" <td>178</td>\n",
207-
" <td>134</td>\n",
208-
" <td>70.0</td>\n",
229+
" <td>198</td>\n",
230+
" <td>198</td>\n",
231+
" <td>0.0</td>\n",
209232
" </tr>\n",
210233
" <tr>\n",
211234
" <th>18</th>\n",
212-
" <td>178</td>\n",
213-
" <td>178</td>\n",
235+
" <td>197</td>\n",
236+
" <td>197</td>\n",
214237
" <td>0.0</td>\n",
215238
" </tr>\n",
216239
" <tr>\n",
217240
" <th>19</th>\n",
218-
" <td>178</td>\n",
219-
" <td>179</td>\n",
220-
" <td>30.0</td>\n",
241+
" <td>197</td>\n",
242+
" <td>198</td>\n",
243+
" <td>40.0</td>\n",
221244
" </tr>\n",
222245
" </tbody>\n",
223246
"</table>\n",
224247
"</div>"
225248
],
226249
"text/plain": [
227250
" sourceNodeId targetNodeId distance\n",
228-
"0 134 134 0.0\n",
229-
"1 16 16 0.0\n",
230-
"2 177 134 110.0\n",
231-
"3 177 177 0.0\n",
232-
"4 177 178 40.0\n",
233-
"5 177 179 70.0\n",
234-
"6 16 134 110.0\n",
235-
"7 16 178 40.0\n",
236-
"8 16 179 70.0\n",
237-
"9 15 15 0.0\n",
238-
"10 15 16 50.0\n",
239-
"11 179 134 40.0\n",
240-
"12 15 134 120.0\n",
241-
"13 15 177 50.0\n",
242-
"14 15 178 50.0\n",
243-
"15 179 179 0.0\n",
244-
"16 15 179 80.0\n",
245-
"17 178 134 70.0\n",
246-
"18 178 178 0.0\n",
247-
"19 178 179 30.0"
251+
"0 96 96 0.0\n",
252+
"1 97 97 0.0\n",
253+
"2 97 196 40.0\n",
254+
"3 97 197 70.0\n",
255+
"4 97 198 110.0\n",
256+
"5 96 97 50.0\n",
257+
"6 96 195 50.0\n",
258+
"7 96 196 50.0\n",
259+
"8 96 197 80.0\n",
260+
"9 96 198 120.0\n",
261+
"10 195 195 0.0\n",
262+
"11 195 196 40.0\n",
263+
"12 196 196 0.0\n",
264+
"13 195 197 70.0\n",
265+
"14 196 197 30.0\n",
266+
"15 195 198 110.0\n",
267+
"16 196 198 70.0\n",
268+
"17 198 198 0.0\n",
269+
"18 197 197 0.0\n",
270+
"19 197 198 40.0"
248271
]
249272
},
250273
"execution_count": 4,

0 commit comments

Comments
 (0)