EdBianchi commited on
Commit
cc0aa37
1 Parent(s): 9d736cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -20,20 +20,20 @@ def importRDF(filename, format):
20
  with st.spinner('Loading all the stuffs...'):
21
  graph = importRDF("rdf-dataset.ttl", "ttl")
22
 
23
- # MOTHOD TO CONVERT THE QUERY RESULT INTO A DATAFRAME
24
  def sparql_results_to_df(results):
25
  return pd.DataFrame(
26
  data=([None if x is None else x.toPython() for x in row] for row in results),
27
  columns=[str(x) for x in results.vars],
28
  )
29
 
30
- # METHOD TO EXECUTE A GENERIC QUERY (and return a pandas dataframe)
31
  def computeQuery(query, executor):
32
  result = executor.query(query)
33
  res_df = sparql_results_to_df(result)
34
  return res_df
35
 
36
- # METHOD TO EXECUTE A PARAMETRIC QUERY (select all the accidents description and manufacturer of a user-selected ride)
37
  def rideAccidentDescription(ride_name, executor):
38
  ride_name = Literal(ride_name)
39
  query = """
@@ -84,8 +84,11 @@ def display():
84
  option = st.selectbox("Select a Ride", options=ride_names)
85
  res, query = rideAccidentDescription(option, graph)
86
  res_count = res.count()[0]
87
- limit = st.slider("Num. of Accidents to Visualize", 1, int(res_count), 5, 1)
88
- st.table(res[:limit])
 
 
 
89
  with st.expander("Show query"):
90
  st.code(query, language="sparql")
91
  st.markdown("---")
@@ -266,10 +269,13 @@ pers_query = st.text_area('', """
266
  ?ride ride:manufacturer "Vekoma" ;
267
  ride:name ?name
268
  }
269
- """, height=200)
270
  with st.container():
271
- res = computeQuery(pers_query, graph)
272
- st.table(res)
 
 
 
273
  st.markdown("---")
274
 
275
  # SIDEBAR
 
20
  with st.spinner('Loading all the stuffs...'):
21
  graph = importRDF("rdf-dataset.ttl", "ttl")
22
 
23
+ # METHOD TO CONVERT THE QUERY RESULT INTO A DATAFRAME
24
  def sparql_results_to_df(results):
25
  return pd.DataFrame(
26
  data=([None if x is None else x.toPython() for x in row] for row in results),
27
  columns=[str(x) for x in results.vars],
28
  )
29
 
30
+ # METHOD TO EXECUTE A GENERIC QUERY
31
  def computeQuery(query, executor):
32
  result = executor.query(query)
33
  res_df = sparql_results_to_df(result)
34
  return res_df
35
 
36
+ # METHOD TO EXECUTE A PARAMETRIC QUERY
37
  def rideAccidentDescription(ride_name, executor):
38
  ride_name = Literal(ride_name)
39
  query = """
 
84
  option = st.selectbox("Select a Ride", options=ride_names)
85
  res, query = rideAccidentDescription(option, graph)
86
  res_count = res.count()[0]
87
+ if (res_count < 3):
88
+ st.table(res)
89
+ else:
90
+ limit = st.slider("Num. of Accidents to Visualize", 1, int(res_count), 2, 1)
91
+ st.table(res[:limit])
92
  with st.expander("Show query"):
93
  st.code(query, language="sparql")
94
  st.markdown("---")
 
269
  ?ride ride:manufacturer "Vekoma" ;
270
  ride:name ?name
271
  }
272
+ """, height=200)
273
  with st.container():
274
+ try:
275
+ res = computeQuery(pers_query, graph)
276
+ st.table(res)
277
+ except:
278
+ st.error("Ooops! Check you query syntax...")
279
  st.markdown("---")
280
 
281
  # SIDEBAR