Skip to content

Commit c01c965

Browse files
committed
added popup_output option to be able to produce svg without popups
1 parent c892e30 commit c01c965

File tree

9 files changed

+73
-100
lines changed

9 files changed

+73
-100
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

doc/why-janitor.py.ttl.dot.svg

Lines changed: 48 additions & 78 deletions
Loading

examples/scripts/why-janitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@
4040
# comment line below to fix spurious apply calls caused by pandas printing implementation
4141
print(df)
4242

43-
pyjviz.save_dot(vertical = True)
43+
pyjviz.save_dot(vertical = True, popup_output = False)

pyjviz/viz.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def uri_to_dot_id(uri):
2121
return str(hash(uri)).replace("-", "d")
2222

23-
def make_table_popup_href(head_html):
23+
def make_table_popup_href(head_html, popup_output):
2424
if head_html is None:
2525
return ""
2626

@@ -30,31 +30,34 @@ def make_table_popup_href(head_html):
3030
with tempfile.NamedTemporaryFile(dir = temp_dir, suffix = '.html', delete = False) as temp_fp:
3131
popup_size = (800, 200)
3232
temp_fp.write(head_html.toPython().replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "'").replace("&#10;", "\n").encode('ascii'))
33-
href = f"""href="javascript:
34-
/* alert(location.pathname.match(/.*\//) + '\n' + '{temp_fp.name}' + '\n' + '{fstriplestore.triple_store.output_dir}'); */
35-
{{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{os.path.basename(temp_fp.name)}', '_blank', 'width={popup_size[0]},height={popup_size[1]}'); }}
33+
if popup_output:
34+
href = f"""href="javascript:
35+
{{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{os.path.basename(temp_fp.name)}', '_blank', 'width={popup_size[0]},height={popup_size[1]}'); }}
3636
"
37-
"""
37+
"""
38+
else:
39+
href = 'href="tmp/' + os.path.basename(temp_fp.name) + '"'
3840

3941
return href
4042

41-
def make_image_popup_href(image_b64):
43+
def make_image_popup_href(image_b64, popup_output):
4244
href = ""
4345
if fstriplestore.triple_store.output_dir:
4446
temp_dir = os.path.join(fstriplestore.triple_store.output_dir, "tmp")
4547
with tempfile.NamedTemporaryFile(dir = temp_dir, suffix = '.html', delete = False) as temp_fp:
4648
popup_size = (900, 500)
4749
temp_fp.write(("<img src='data:image/png;base64," + image_b64.toPython() + "'></img>").encode('ascii'))
48-
href = f"""href="javascript:
49-
/* alert(location.pathname.match(/.*\//) + '\n' + '{temp_fp.name}' + '\n' + '{fstriplestore.triple_store.output_dir}'); */
50-
{{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{os.path.basename(temp_fp.name)}', '_blank', 'width={popup_size[0]},height={popup_size[1]}'); }}
51-
"
52-
"""
50+
if popup_output:
51+
href = f"""href="javascript:
52+
{{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{os.path.basename(temp_fp.name)}', '_blank', 'width={popup_size[0]},height={popup_size[1]}'); }}
53+
"
54+
"""
55+
else:
56+
href = 'href="tmp/' + os.path.basename(temp_fp.name) + '"'
5357

5458
return href
5559

56-
57-
def dump_subgraph(g, cc_uri, out_fd):
60+
def dump_subgraph(g, cc_uri, out_fd, popup_output):
5861
subgraphs = [r for r in g.query("select ?pp ?pl { ?pp rdf:type <CodeBlock>; rdf:label ?pl; <part-of> ?sg }", base = fstriplestore.base_uri, initBindings = {'sg': cc_uri})]
5962
for subgraph, subgraph_label in subgraphs:
6063
if subgraph_label != rdflib.RDF.nil:
@@ -63,7 +66,7 @@ def dump_subgraph(g, cc_uri, out_fd):
6366
label = "{subgraph_label}";
6467
""", file = out_fd)
6568

66-
dump_subgraph(g, subgraph, out_fd)
69+
dump_subgraph(g, subgraph, out_fd, popup_output)
6770

6871
rq = """
6972
select ?obj_state ?version ?obj_type ?obj_uuid {
@@ -100,13 +103,13 @@ def dump_subgraph(g, cc_uri, out_fd):
100103
for shape, head in g.query(glance_rq, base = fstriplestore.base_uri, initBindings = bindings):
101104
shape = shape
102105
node_bgcolor = "#88000022"
103-
href = make_table_popup_href(head)
106+
href = make_table_popup_href(head, popup_output)
104107
elif obj_type.toPython() == "Series":
105108
glance_rq = "select ?shape { ?obj_state_cc <obj-state> ?obj_state; rdf:type ?target_cc_type; <shape> ?shape }"
106109
for shape in g.query(glance_rq, base = fstriplestore.base_uri, initBindings = bindings):
107110
shape = shape[0]
108111
node_bgcolor = "#88000022"
109-
href = make_table_popup_href(None)
112+
href = make_table_popup_href(None, popup_output)
110113
else:
111114
raise Exception(f"unknown obj_type {obj_type}")
112115
elif target_cc_type == cc_basic_plot_uri:
@@ -115,7 +118,7 @@ def dump_subgraph(g, cc_uri, out_fd):
115118
for shape, plot_im in g.query(basic_plot_rq, base = fstriplestore.base_uri, initBindings = bindings):
116119
shape = shape
117120
node_bgcolor = "#44056022"
118-
href = make_image_popup_href(plot_im)
121+
href = make_image_popup_href(plot_im, popup_output)
119122
else:
120123
raise Exception(f"unknown cc_type {target_cc_type}")
121124

@@ -165,7 +168,7 @@ def dump_subgraph(g, cc_uri, out_fd):
165168
print(f"}}", file = out_fd)
166169

167170

168-
def dump_dot_code(g, vertical, show_objects):
171+
def dump_dot_code(g, vertical, show_objects, popup_output):
169172
#ipdb.set_trace()
170173

171174
out_fd = StringIO()
@@ -191,7 +194,7 @@ def dump_dot_code(g, vertical, show_objects):
191194
""".replace("{rankdir}", rankdir), file = out_fd)
192195

193196

194-
dump_subgraph(g, rdflib.RDF.nil, out_fd)
197+
dump_subgraph(g, rdflib.RDF.nil, out_fd, popup_output)
195198

196199
#for subgraph, subgraph_label in subgraphs:
197200
if 1:
@@ -304,7 +307,7 @@ def print_dot(vertical = False, show_objects = False):
304307
g = fstriplestore.triple_store.get_graph()
305308
print(dump_dot_code(g, vertical = vertical, show_objects = show_objects))
306309

307-
def save_dot(dot_output_fn = None, vertical = False, show_objects = False):
310+
def save_dot(dot_output_fn = None, vertical = False, show_objects = False, popup_output = False):
308311
ts = fstriplestore.triple_store
309312
if dot_output_fn is None:
310313
if hasattr(ts, 'output_fn') and ts.output_fn is not None:
@@ -314,7 +317,7 @@ def save_dot(dot_output_fn = None, vertical = False, show_objects = False):
314317
raise Exception("can't guess dot_output_fn")
315318

316319
g = ts.get_graph()
317-
dot_code = dump_dot_code(g, vertical = vertical, show_objects = show_objects)
320+
dot_code = dump_dot_code(g, vertical = vertical, show_objects = show_objects, popup_output = popup_output)
318321
gvz = graphviz.Source(dot_code)
319322
gvz.render(dot_output_fn, format = 'svg', engine = 'dot')
320323

0 commit comments

Comments
 (0)