Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Commit
•
aaa3afe
1
Parent(s):
193b600
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,89 @@
|
|
1 |
-
import streamlit
|
2 |
-
from
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from SPARQLWrapper import SPARQLWrapper, JSON
|
3 |
+
from streamlit_agraph import agraph, TripleStore, Node, Edge, Config
|
4 |
+
from layout import footer
|
5 |
+
import json
|
6 |
+
|
7 |
+
def get_inspired():
|
8 |
+
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
|
9 |
+
|
10 |
+
query_string = """
|
11 |
+
SELECT ?name_pe1_en ?rel_en ?name_pe2_en
|
12 |
+
WHERE {
|
13 |
+
{
|
14 |
+
SELECT ?name_p1 ?rel ?name_p2
|
15 |
+
WHERE {
|
16 |
+
?p1 a foaf:Person .
|
17 |
+
?p1 dbo:influencedBy ?p2 .
|
18 |
+
?p2 a foaf:Person .
|
19 |
+
?p1 foaf:name ?name_p1 .
|
20 |
+
?p2 foaf:name ?name_p2 .
|
21 |
+
dbo:influencedBy rdfs:label ?rel .
|
22 |
+
}
|
23 |
+
LIMIT 100
|
24 |
+
}
|
25 |
+
UNION
|
26 |
+
{
|
27 |
+
SELECT ?name_p1 ?rel ?name_p2
|
28 |
+
WHERE {
|
29 |
+
?p1 a foaf:Person .
|
30 |
+
?p1 dbo:influenced ?p2 .
|
31 |
+
?p2 a foaf:Person .
|
32 |
+
?p1 foaf:name ?name_p1 .
|
33 |
+
?p2 foaf:name ?name_p2 .
|
34 |
+
dbo:influenced rdfs:label ?rel .
|
35 |
+
}
|
36 |
+
LIMIT 100
|
37 |
+
}
|
38 |
+
FILTER ( LANG(?name_p1) = "en" && LANG(?rel) = "en" && LANG(?name_p2) = "en" )
|
39 |
+
BIND ( STR(?name_p1) AS ?name_pe1_en )
|
40 |
+
BIND ( STR(?rel) AS ?rel_en )
|
41 |
+
BIND ( STR(?name_p2) AS ?name_pe2_en )
|
42 |
+
}
|
43 |
+
"""
|
44 |
+
|
45 |
+
sparql.setQuery(query_string)
|
46 |
+
sparql.setReturnFormat(JSON)
|
47 |
+
results = sparql.query().convert()
|
48 |
+
store = TripleStore()
|
49 |
+
for result in results["results"]["bindings"]:
|
50 |
+
node1 = result["name_pe1_en"]["value"]
|
51 |
+
link = result["rel_en"]["value"]
|
52 |
+
node2 = result["name_pe2_en"]["value"]
|
53 |
+
store.add_triple(node1, link, node2)
|
54 |
+
return store
|
55 |
+
|
56 |
+
def app():
|
57 |
+
footer()
|
58 |
+
st.title("Graph Example")
|
59 |
+
st.sidebar.title("Welcome")
|
60 |
+
query_type = st.sidebar.selectbox("Query Tpye: ", ["Inspirationals", "Marvel"]) # could add more stuff here later on or add other endpoints in the sidebar.
|
61 |
+
config = Config(height=600, width=700, nodeHighlightBehavior=True, highlightColor="#F7A7A6", directed=True,
|
62 |
+
collapsible=True)
|
63 |
+
|
64 |
+
if query_type=="Inspirationals":
|
65 |
+
st.subheader("Inspirationals")
|
66 |
+
with st.spinner("Loading data"):
|
67 |
+
store = get_inspired()
|
68 |
+
st.write("Nodes loaded: " + str(len(store.getNodes())))
|
69 |
+
st.success("Done")
|
70 |
+
agraph(list(store.getNodes()), (store.getEdges() ), config)
|
71 |
+
|
72 |
+
if query_type=="Marvel":
|
73 |
+
#based on http://marvel-force-chart.surge.sh/
|
74 |
+
with open("data/marvel.json", encoding="utf8") as f:
|
75 |
+
marvel_file = json.loads(f.read())
|
76 |
+
marvel_store = TripleStore()
|
77 |
+
for sub_graph in marvel_file["children"]:
|
78 |
+
marvel_store.add_triple(marvel_file["name"], "has_subgroup", sub_graph["name"], picture=marvel_file["img"])
|
79 |
+
for node in sub_graph["children"]:
|
80 |
+
node1 = node["hero"]
|
81 |
+
link = "blongs_to"
|
82 |
+
node2 = sub_graph["name"]
|
83 |
+
pic = node["img"]
|
84 |
+
marvel_store.add_triple(node1, link, node2, picture=pic)
|
85 |
+
agraph(list(marvel_store.getNodes()), (marvel_store.getEdges()), config)
|
86 |
+
|
87 |
+
|
88 |
+
if __name__ == '__main__':
|
89 |
+
app()
|