Skip to content

Commit af09c99

Browse files
author
Mark Needham
committed
make js configurable
1 parent 5f8d56a commit af09c99

File tree

3 files changed

+56
-187
lines changed

3 files changed

+56
-187
lines changed

generate_notebook.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,31 @@ def find_tag(file, tag):
120120
</style>
121121
'''
122122

123+
cypher_read_query = "MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *"
124+
125+
labels_json = {
126+
"Page": {
127+
"caption": "name",
128+
"size": "pagerank"
129+
}
130+
}
131+
132+
relationships_json = {
133+
"LINKS": {
134+
"thickness": "weight",
135+
"caption": False
136+
}
137+
}
138+
123139
viz = '''\
124140
from scripts.vis import generate_vis
125-
generate_vis(host, user, password, "MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *")
126-
'''
141+
142+
query = "%s"
143+
labels_json = %s
144+
relationships_json = %s
145+
146+
generate_vis(host, user, password, query, labels_json, relationships_json)
147+
''' % (cypher_read_query, labels_json, relationships_json)
127148

128149
nb = nbf.v4.new_notebook()
129150
nb['cells'] = [nbf.v4.new_markdown_cell(heading_text),

notebooks/PageRank.ipynb

Lines changed: 17 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
{
2121
"cell_type": "code",
22-
"execution_count": 1,
22+
"execution_count": null,
2323
"metadata": {},
2424
"outputs": [],
2525
"source": [
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"cell_type": "code",
40-
"execution_count": 2,
40+
"execution_count": null,
4141
"metadata": {},
4242
"outputs": [],
4343
"source": [
@@ -56,17 +56,9 @@
5656
},
5757
{
5858
"cell_type": "code",
59-
"execution_count": 3,
59+
"execution_count": null,
6060
"metadata": {},
61-
"outputs": [
62-
{
63-
"name": "stdout",
64-
"output_type": "stream",
65-
"text": [
66-
"Stats: {}\n"
67-
]
68-
}
69-
],
61+
"outputs": [],
7062
"source": [
7163
"create_graph_query = '''\n",
7264
"MERGE (home:Page {name:'Home'})\n",
@@ -108,96 +100,9 @@
108100
},
109101
{
110102
"cell_type": "code",
111-
"execution_count": 4,
103+
"execution_count": null,
112104
"metadata": {},
113-
"outputs": [
114-
{
115-
"data": {
116-
"text/html": [
117-
"<div>\n",
118-
"<style scoped>\n",
119-
" .dataframe tbody tr th:only-of-type {\n",
120-
" vertical-align: middle;\n",
121-
" }\n",
122-
"\n",
123-
" .dataframe tbody tr th {\n",
124-
" vertical-align: top;\n",
125-
" }\n",
126-
"\n",
127-
" .dataframe thead th {\n",
128-
" text-align: right;\n",
129-
" }\n",
130-
"</style>\n",
131-
"<table border=\"1\" class=\"dataframe\">\n",
132-
" <thead>\n",
133-
" <tr style=\"text-align: right;\">\n",
134-
" <th></th>\n",
135-
" <th>page</th>\n",
136-
" <th>score</th>\n",
137-
" </tr>\n",
138-
" </thead>\n",
139-
" <tbody>\n",
140-
" <tr>\n",
141-
" <th>0</th>\n",
142-
" <td>Home</td>\n",
143-
" <td>3.233877</td>\n",
144-
" </tr>\n",
145-
" <tr>\n",
146-
" <th>1</th>\n",
147-
" <td>About</td>\n",
148-
" <td>1.060435</td>\n",
149-
" </tr>\n",
150-
" <tr>\n",
151-
" <th>2</th>\n",
152-
" <td>Product</td>\n",
153-
" <td>1.060435</td>\n",
154-
" </tr>\n",
155-
" <tr>\n",
156-
" <th>3</th>\n",
157-
" <td>Links</td>\n",
158-
" <td>1.060435</td>\n",
159-
" </tr>\n",
160-
" <tr>\n",
161-
" <th>4</th>\n",
162-
" <td>Site A</td>\n",
163-
" <td>0.329035</td>\n",
164-
" </tr>\n",
165-
" <tr>\n",
166-
" <th>5</th>\n",
167-
" <td>Site B</td>\n",
168-
" <td>0.329035</td>\n",
169-
" </tr>\n",
170-
" <tr>\n",
171-
" <th>6</th>\n",
172-
" <td>Site C</td>\n",
173-
" <td>0.329035</td>\n",
174-
" </tr>\n",
175-
" <tr>\n",
176-
" <th>7</th>\n",
177-
" <td>Site D</td>\n",
178-
" <td>0.329035</td>\n",
179-
" </tr>\n",
180-
" </tbody>\n",
181-
"</table>\n",
182-
"</div>"
183-
],
184-
"text/plain": [
185-
" page score\n",
186-
"0 Home 3.233877\n",
187-
"1 About 1.060435\n",
188-
"2 Product 1.060435\n",
189-
"3 Links 1.060435\n",
190-
"4 Site A 0.329035\n",
191-
"5 Site B 0.329035\n",
192-
"6 Site C 0.329035\n",
193-
"7 Site D 0.329035"
194-
]
195-
},
196-
"execution_count": 4,
197-
"metadata": {},
198-
"output_type": "execute_result"
199-
}
200-
],
105+
"outputs": [],
201106
"source": [
202107
"streaming_query = \"\"\"\n",
203108
"CALL algo.pageRank.stream('Page', 'LINKS', {iterations:20, dampingFactor:0.85})\n",
@@ -223,31 +128,9 @@
223128
},
224129
{
225130
"cell_type": "code",
226-
"execution_count": 5,
131+
"execution_count": null,
227132
"metadata": {},
228-
"outputs": [
229-
{
230-
"data": {
231-
"text/html": [
232-
"<style>\n",
233-
".output_wrapper, .output {\n",
234-
" height:auto !important;\n",
235-
" max-height:600px;\n",
236-
"}\n",
237-
".output_scroll {\n",
238-
" box-shadow:none !important;\n",
239-
" webkit-box-shadow:none !important;\n",
240-
"}\n",
241-
"</style>"
242-
],
243-
"text/plain": [
244-
"<IPython.core.display.HTML object>"
245-
]
246-
},
247-
"metadata": {},
248-
"output_type": "display_data"
249-
}
250-
],
133+
"outputs": [],
251134
"source": [
252135
"%%html\n",
253136
"<style>\n",
@@ -264,56 +147,21 @@
264147
},
265148
{
266149
"cell_type": "code",
267-
"execution_count": 8,
150+
"execution_count": null,
268151
"metadata": {},
269-
"outputs": [
270-
{
271-
"data": {
272-
"text/html": [
273-
"\n",
274-
" <iframe\n",
275-
" width=\"100%\"\n",
276-
" height=\"300px\"\n",
277-
" src=\"figure/graph-42f172d7-37c2-4d64-bf22-122fad03b596.html\"\n",
278-
" frameborder=\"0\"\n",
279-
" allowfullscreen\n",
280-
" ></iframe>\n",
281-
" "
282-
],
283-
"text/plain": [
284-
"<IPython.lib.display.IFrame at 0x1016ee940>"
285-
]
286-
},
287-
"execution_count": 8,
288-
"metadata": {},
289-
"output_type": "execute_result"
290-
}
291-
],
152+
"outputs": [],
292153
"source": [
293154
"from scripts.vis import generate_vis\n",
294-
"generate_vis(host, user, password, \"MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *\")\n"
155+
"\n",
156+
"query = \"MATCH (p1:Page)-[r:LINKS]->(p2:Page) RETURN *\"\n",
157+
"labels_json = {'Page': {'caption': 'name', 'size': 'pagerank'}}\n",
158+
"relationships_json = {'LINKS': {'thickness': 'weight', 'caption': False}}\n",
159+
"\n",
160+
"generate_vis(host, user, password, query, labels_json, relationships_json)\n"
295161
]
296162
}
297163
],
298-
"metadata": {
299-
"kernelspec": {
300-
"display_name": "Python 3",
301-
"language": "python",
302-
"name": "python3"
303-
},
304-
"language_info": {
305-
"codemirror_mode": {
306-
"name": "ipython",
307-
"version": 3
308-
},
309-
"file_extension": ".py",
310-
"mimetype": "text/x-python",
311-
"name": "python",
312-
"nbconvert_exporter": "python",
313-
"pygments_lexer": "ipython3",
314-
"version": "3.6.0"
315-
}
316-
},
164+
"metadata": {},
317165
"nbformat": 4,
318166
"nbformat_minor": 2
319167
}

notebooks/scripts/vis.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import json
33
import uuid
44

5-
def generate_vis(host, user, password, cypher):
5+
6+
def generate_vis(host, user, password, cypher, labels_json, relationships_json):
67
html = """\
78
<html>
89
<head>
@@ -14,7 +15,7 @@ def generate_vis(host, user, password, cypher):
1415
1516
#viz {{
1617
width: 300px;
17-
height: 400px;
18+
height: 350px;
1819
font: 22pt arial;
1920
}}
2021
</style>
@@ -35,18 +36,8 @@ def generate_vis(host, user, password, cypher):
3536
server_url: "{host}",
3637
server_user: "{user}",
3738
server_password: "{password}",
38-
labels: {{
39-
"Page": {{
40-
"caption": "name",
41-
"size": "pagerank"
42-
}}
43-
}},
44-
relationships: {{
45-
"LINKS": {{
46-
"thickness": "weight",
47-
"caption": false
48-
}}
49-
}},
39+
labels: {labels},
40+
relationships: {relationships},
5041
initial_cypher: "{cypher}"
5142
}};
5243
@@ -72,9 +63,18 @@ def generate_vis(host, user, password, cypher):
7263
7364
</html>
7465
"""
75-
html = html.format(host = host, user=user, password=password, cypher=cypher)
76-
unique_id = str(uuid.uuid4())
7766

67+
html = html.format(
68+
host=host,
69+
user=user,
70+
password=password,
71+
cypher=cypher,
72+
labels = json.dumps(labels_json),
73+
relationships=json.dumps(relationships_json)
74+
# relationships=json.dumps(relationships).replace("{", "{{").replace("}", "}}")
75+
)
76+
77+
unique_id = str(uuid.uuid4())
7878
filename = "figure/graph-{}.html".format(unique_id)
7979
with open(filename, "w") as f:
8080
f.write(html)

0 commit comments

Comments
 (0)