File size: 3,637 Bytes
aaa3afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fbc894
aaa3afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017d045
 
 
 
 
 
 
 
ea42a66
017d045
 
 
 
 
 
 
aaa3afe
 
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
import streamlit as st
from SPARQLWrapper import SPARQLWrapper, JSON
from streamlit_agraph import agraph, TripleStore, Node, Edge, Config
from layout import footer
import json

def get_inspired():
  sparql = SPARQLWrapper("http://dbpedia.org/sparql")

  query_string = """
  SELECT ?name_pe1_en ?rel_en ?name_pe2_en
  WHERE {
    {
         SELECT ?name_p1 ?rel ?name_p2
         WHERE {
             ?p1 a foaf:Person .
             ?p1 dbo:influencedBy ?p2 .
             ?p2 a foaf:Person .
             ?p1 foaf:name ?name_p1 .
             ?p2 foaf:name ?name_p2 .
            dbo:influencedBy rdfs:label ?rel .
            }
         LIMIT 100
    }
    UNION
    {
         SELECT ?name_p1 ?rel ?name_p2
         WHERE {
            ?p1 a foaf:Person .
            ?p1 dbo:influenced ?p2 .
            ?p2 a foaf:Person .
            ?p1 foaf:name ?name_p1 .
            ?p2 foaf:name ?name_p2 .
            dbo:influenced rdfs:label ?rel .
        }
     LIMIT 100
    }
    FILTER ( LANG(?name_p1) = "en" && LANG(?rel) = "en" && LANG(?name_p2) = "en" )
    BIND ( STR(?name_p1) AS ?name_pe1_en )
    BIND ( STR(?rel) AS ?rel_en )
    BIND ( STR(?name_p2) AS ?name_pe2_en )
  }
  """

  sparql.setQuery(query_string)
  sparql.setReturnFormat(JSON)
  results = sparql.query().convert()
  store = TripleStore()
  for result in results["results"]["bindings"]:
    node1 = result["name_pe1_en"]["value"]
    link = result["rel_en"]["value"]
    node2 = result["name_pe2_en"]["value"]
    store.add_triple(node1, link, node2)
  return store

def app():
  footer()
  st.title("Graph Example")
  st.sidebar.title("Welcome")
  query_type = st.sidebar.selectbox("Query Tpye: ", ["Inspirationals", "Marvel", "Fraud"]) # could add more stuff here later on or add other endpoints in the sidebar.
  config = Config(height=600, width=700, nodeHighlightBehavior=True, highlightColor="#F7A7A6", directed=True,
                  collapsible=True)

  if query_type=="Inspirationals":
    st.subheader("Inspirationals")
    with st.spinner("Loading data"):
      store = get_inspired()
      st.write("Nodes loaded: " + str(len(store.getNodes())))
    st.success("Done")
    agraph(list(store.getNodes()), (store.getEdges() ), config)

  if query_type=="Marvel":
    #based on http://marvel-force-chart.surge.sh/
    with open("data/marvel.json", encoding="utf8") as f:
      marvel_file = json.loads(f.read())
      marvel_store = TripleStore()
      for sub_graph in marvel_file["children"]:
        marvel_store.add_triple(marvel_file["name"], "has_subgroup", sub_graph["name"], picture=marvel_file["img"])
        for node in sub_graph["children"]:
          node1 = node["hero"]
          link = "blongs_to"
          node2 = sub_graph["name"]
          pic = node["img"]
          marvel_store.add_triple(node1, link, node2, picture=pic)
      agraph(list(marvel_store.getNodes()), (marvel_store.getEdges()), config)


  if query_type=="Fraud":
    #based on http://marvel-force-chart.surge.sh/
    with open("data/fraud.json", encoding="utf8") as f:
      marvel_file = json.loads(f.read())
      marvel_store = TripleStore()
      for sub_graph in marvel_file["children"]:
        marvel_store.add_triple(marvel_file["name"], "has_subgroup", sub_graph["name"], picture=marvel_file["img"])
        for node in sub_graph["children"]:
          node1 = node["role"]
          link = "blongs_to"
          node2 = sub_graph["name"]
          pic = node["img"]
          marvel_store.add_triple(node1, link, node2, picture=pic)
      agraph(list(marvel_store.getNodes()), (marvel_store.getEdges()), config)


if __name__ == '__main__':
    app()