-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
381 lines (308 loc) · 16.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import html
# because dominate will stop on html
pyhtml = html
import os
import re
#import urllib.parse
#import urllib.request
import dominate
from dominate.tags import *
from bs4 import BeautifulSoup
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect, url_for, after_this_request
import markdown
import rdflib
ns = {"dcterms" : "http://purl.org/dc/terms/",
"owl" : "http://www.w3.org/2002/07/owl#",
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs" : "http://www.w3.org/2000/01/rdf-schema#" ,
"p-lod" : "http://p-lod.umasscreate.net/vocabulary#",
"p-lod-v" : "http://digitalhumanities.umass.edu/p-lod/vocabulary/",
"p-lod-e" : "http://digitalhumanities.umass.edu/p-lod/entities/" }
app = Flask(__name__)
g = rdflib.Graph()
result = g.parse("google-lod-triples.ttl", format="turtle")
def plodheader(doc, plod = ''):
# doc['xmlns'] = "http://www.w3.org/1999/xhtml" # Dominate doesn't produce closed no-content tags
doc.head += meta(charset="utf-8")
doc.head += meta(http_equiv="X-UA-Compatible", content="IE=edge")
doc.head += meta(name="viewport", content="width=device-width, initial-scale=1")
doc.head += link(rel='stylesheet', href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css",integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u",crossorigin="anonymous")
doc.head += link(rel="stylesheet", href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css", integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp", crossorigin="anonymous")
doc.head += script(src="http://code.jquery.com/jquery-3.1.1.min.js")
doc.head += script(src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js",integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa",crossorigin="anonymous")
doc.head += style("body { padding-top: 60px; }")
doc.head += meta(name="DC.title",lang="en",content="%s" % (plod) )
doc.head += meta(name="DC.identifier", content="http://digitalhumanities.umass.edu/p-lod/%s" % plod)
@app.route('/p-lod/entities/<path:entity>')
def entities(entity):
# @after_this_request
# def add_header(response):
# response.headers['Content-Type'] = 'application/xhtml+xml; charset=utf-8'
# return response
eresult = g.query(
"""SELECT ?p ?o ?plabel ?prange ?olabel ?sortorder
WHERE {
p-lod:%s ?p ?o .
OPTIONAL { ?p rdfs:label ?plabel }
OPTIONAL { ?p rdfs:range ?prange }
OPTIONAL { ?p p-lod:sort-order ?sortorder }
OPTIONAL { ?o rdfs:label ?olabel }
} ORDER BY ?sortorder ?plabel""" % (entity), initNs = ns)
elabel = g.query(
"""SELECT ?slabel
WHERE {
p-lod:%s dcterms:title ?slabel
}""" % (entity), initNs = ns)
eparts = g.query(
"""SELECT ?part ?label ?vfile ?sortorder
WHERE {
?part ?sub_property p-lod:%s .
?sub_property rdfs:subPropertyOf p-lod:is-part-of .
OPTIONAL { ?part dcterms:title ?label . }
?part rdf:type ?type .
OPTIONAL { ?part p-lod:visual-documentation-file ?vfile }
OPTIONAL { ?part p-lod:sort-order ?sortorder }
} ORDER BY ?type ?sortorder ?part""" % (entity), initNs = ns)
esameas = g.query(
"""SELECT ?url ?label
WHERE {
?url owl:sameAs p-lod-e:%s .
?url rdfs:label ?label .
}""" % (entity), initNs = ns)
eobjects = g.query(
"""SELECT ?s ?p ?slabel ?plabel
WHERE {
?s ?p p-lod-e:%s .
OPTIONAL { ?s rdfs:label ?slabel }
OPTIONAL { ?p rdfs:label ?plabel }
FILTER ( ?p != p-lod:next )
FILTER ( ?p != p-lod:is-part-of )
FILTER ( ?p != owl:sameAs )
} ORDER BY ?s LIMIT 1000""" % (entity), initNs = ns)
edoc = dominate.document(title="Linked Open Data for Pompeii: %s" % (entity))
plodheader(edoc, entity)
edoc.body['prefix'] = "bibo: http://purl.org/ontology/bibo/ cc: http://creativecommons.org/ns# dcmitype: http://purl.org/dc/dcmitype/ dcterms: http://purl.org/dc/terms/ foaf: http://xmlns.com/foaf/0.1/ nm: http://nomisma.org/id/ owl: http://www.w3.org/2002/07/owl# rdfs: http://www.w3.org/2000/01/rdf-schema# rdfa: http://www.w3.org/ns/rdfa# rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# skos: http://www.w3.org/2004/02/skos/core#"
with edoc:
with nav(cls="navbar navbar-default navbar-fixed-top"):
with div(cls="container-fluid"):
with div(cls="navbar-header"):
a("P-LOD Linked Open Data for Pompeii: Entity", href="/p-lod/entities/pompeii",cls="navbar-brand")
with ul(cls="nav navbar-nav"):
with li(cls="dropdown"):
a("Browse", href="#",cls="dropdown-toggle", data_toggle="dropdown")
with ul(cls="dropdown-menu", role="menu"):
li(a('Go to Pompeii', href="/p-lod/entities/pompeii"))
li(a('All Classes', href="/p-lod/vocabulary/entity"))
li(a('All Properties', href="/p-lod/vocabulary/vocabulary-item"))
with div(cls="container", about="/p-lod/%s" % (entity)):
with dl(cls="dl-horizontal"):
unescapehtml = False
dt()
for row in elabel:
dd(strong(str(row.slabel), cls="large"))
for row in eresult:
if str(row.p) == 'http://www.w3.org/2000/01/rdf-schema#label':
continue
elif str(row.plabel) != 'None':
dt(str(row.plabel))
else:
dt(i(str(row.p)))
with dd():
if str(row.olabel) != "None":
olabel = str(row.olabel)
else:
olabel = str(row.o)
if re.search(r'(\.png|\.jpg)$', row.o, flags= re.I):
img(src=row.o,style="max-width:350px")
elif str(row.o)[0:4] == 'http':
if 'http://p-lod.umasscreate.net/vocabulary#' in str(row.o):
a(olabel, title = str(row.o),href = str(row.o).replace('http://p-lod.umasscreate.net/vocabulary#',''))
else:
a(str(row.o),href = str(row.o).replace('http://p-lod.umasscreate.net/vocabulary#',''))
else:
if str(row.prange) == 'http://digitalhumanities.umass.edu/p-lod/vocabulary/markdown-literal':
unescapehtml = True
span( markdown.markdown(olabel),cls="unescapehtml")
else:
span(olabel)
if len(esameas) > 0:
for row in esameas:
dt("Alternate identifier")
dd(str(row.url))
# dt("Future permalink")
# dd("http://digitalhumanities.umass.edu/p-lod/entities/%s" % (entity) )
if len(eparts) > 0:
dt('Has parts')
with dd():
first = 0
curlabel = ''
for part in eparts:
label = str(part.label)
if curlabel != label:
curlabel = label
if first == 1:
first = 0
pstyle = ''
else:
pstyle = 'border-top: thin dotted #aaa;width:25%'
p(a(label, rel="dcterms:hasPart", href = str(part.part).replace('http://p-lod.umasscreate.net/vocabulary#','')), style=pstyle)
if str(part.vfile) != "None":
thumb = str(part.vfile)
a(img(style="margin-left:1em;margin-bottom:15px;max-width:150px;max-height:150px",src=thumb),href=str(part.part).replace('http://p-lod.umasscreate.net/vocabulary#',''))
objlength = len(eobjects)
if objlength > 0:
lenstr = ''
if objlength == 1000:
lenstr = '(first 1000)'
dt("Property of %s" % (lenstr))
with dd():
for s_p in eobjects:
a(str(s_p.slabel), href= str(s_p.s).replace('http://digitalhumanities.umass.edu',''))
span(" via ")
span(str(s_p.plabel))
br()
with footer(cls="footer"):
with div(cls="container"):
with p(cls="text-muted"):
span("P-LOD/PALP is under construction.")
a(" Github", href = "https://github.com/p-lod/p-lod")
# span(". Parse ")
# a('RDFa', href="http://www.w3.org/2012/pyRdfa/extract?uri=http://p-lod.herokuapp.com/p-lod/entities/%s" % (entity))
# span(".")
if unescapehtml == True:
soup = BeautifulSoup(edoc.render(), "html.parser")
for each_div in soup.find_all("span", class_="unescapehtml"):
asoup = BeautifulSoup(pyhtml.unescape(str(each_div)),'html.parser')
each_div.replace_with(asoup)
return str(soup)
else:
return edoc.render()
@app.route('/p-lod/vocabulary/<path:vocab>')
def vocabulary(vocab):
vresult = g.query(
"""SELECT ?p ?o ?plabel ?olabel
WHERE {
p-lod-v:%s ?p ?o .
OPTIONAL { ?p rdfs:label ?plabel }
OPTIONAL { ?o rdfs:label ?olabel }
} ORDER BY ?plabel""" % (vocab), initNs = ns)
vlabel = g.query(
"""SELECT ?slabel
WHERE {
p-lod-v:%s rdfs:label ?slabel
}""" % (vocab), initNs = ns)
vinstances = g.query(
"""SELECT ?instance ?label ?vfile
WHERE {
?instance rdf:type p-lod-v:%s .
?instance rdfs:label ?label .
OPTIONAL { ?instance p-lod-v:visual-documentation-file ?vfile }
} ORDER BY ?instance""" % (vocab), initNs = ns)
vsubs = g.query(
"""SELECT ?sub ?label
WHERE {
?sub rdfs:subClassOf|rdfs:subPropertyOf p-lod-v:%s .
?sub rdfs:label ?label .
} ORDER BY ?label""" % (vocab), initNs = ns)
vusage = g.query(
"""SELECT ?s ?o ?slabel ?olabel
WHERE {
?s p-lod-v:%s ?o .
OPTIONAL { ?s rdfs:label ?slabel }
OPTIONAL { ?o rdfs:label ?olabel }
} ORDER BY ?s ?o LIMIT 2000""" % (vocab), initNs = ns)
vdoc = dominate.document(title="Pompeii LOD: %s" % (vocab))
plodheader(vdoc, vocab)
with vdoc:
with nav(cls="navbar navbar-default navbar-fixed-top"):
with div(cls="container-fluid"):
with div(cls="navbar-header"):
a("P-LOD Linked Open Data for Pompeii: Vocabulary", href="/p-lod/entities/pompeii",cls="navbar-brand")
with ul(cls="nav navbar-nav"):
with li(cls="dropdown"):
a("Browse", href="#",cls="dropdown-toggle", data_toggle="dropdown")
with ul(cls="dropdown-menu", role="menu"):
li(a('Go to Pompeii', href="/p-lod/entities/pompeii"))
li(a('All Classes', href="/p-lod/vocabulary/entity"))
li(a('All Properties', href="/p-lod/vocabulary/vocabulary-item"))
with div(cls="container"):
with dl(cls="dl-horizontal"):
dt()
for row in vlabel:
dd(strong(str(row.slabel), cls="large"))
for row in vresult:
if str(row.p) == 'http://www.w3.org/2000/01/rdf-schema#label':
continue
elif str(row.plabel) != 'None':
dt(str(row.plabel)+":")
else:
dt(i(str(row.p)), style = "margin-left:.25em")
with dd():
if str(row.olabel) != "None":
olabel = str(row.olabel)
else:
olabel = str(row.o)
if str(row.o)[0:4] == 'http':
a(olabel,href = str(row.o).replace('http://digitalhumanities.umass.edu',''))
else:
span(olabel)
if len(vinstances) > 0:
dt('Entities')
with dd():
first = 0
curlabel = ''
for instance in vinstances:
label = str(instance.label)
if curlabel != label:
curlabel = label
if first == 1:
first = 0
pstyle = ''
else:
pstyle = 'border-top: thin dotted #aaa;width:25%'
p(a(label, href = str(instance.instance).replace('http://digitalhumanities.umass.edu','')),style = pstyle)
if str(instance.vfile) != "None":
thumb = str(instance.vfile)
a(img(style="margin-left:1em;margin-bottom:15px;max-width:150px;max-height:150px",src=thumb),href=str(instance.instance).replace('http://digitalhumanities.umass.edu',''))
if len(vsubs) > 0:
dt('Hierarchy')
with dd():
for sub in vsubs:
p(a(str(sub.label), href = str(sub.sub).replace('http://digitalhumanities.umass.edu','')))
if len(vusage) > 0:
dt('Used by')
with dd():
for use in vusage:
with p():
a(str(use.slabel), href = str(use.s).replace('http://digitalhumanities.umass.edu',''))
span(" ⇒ ", style="color:gray")
if str(use.olabel) != 'None':
label = str(use.olabel)
else:
label = str(use.o).replace('http://digitalhumanities.umass.edu','')
a(str(label), href = str(use.o).replace('http://digitalhumanities.umass.edu',''))
hr()
with p():
span("P-LOD is under construction and is overseen by Steven Ellis, Sebastian Heath and Eric Poehler. Data available on ")
a("Github", href = "https://github.com/p-lod/p-lod-data")
span(".")
return vdoc.render()
@app.route('/api/geojson/<path:entity>')
def geojson_entity(entity):
geojsonr = g.query(
"""SELECT ?lat ?long ?geojson
WHERE {
OPTIONAL { p-lod-e:%s p-lod-v:latitude ?lat ;
p-lod-v:longitude ?long .
}
OPTIONAL { p-lod-e:%s p-lod-v:geojson ?geojson }
}""" % (entity, entity), initNs = ns)
if len(geojsonr) > 0:
for row in geojsonr:
pass
@app.route('/')
def index():
return redirect("/p-lod/entities/pompeii", code=302)