- Generating a HTML Table from RDF DESCRIBE Results (I'm actually using a construct to get the predicates and objects concerning a subject)
colspan makes the entire process painful...
Generating a HTML Table from RDF DESCRIBE Results
1 from django.template import Context, Template
2
3 table = Template("""{% autoescape off %}
4 <table>
5 <thead><th>Predicate</th><th>Object</th></thead>
6 <tbody>{{tbody}}</tbody>
7 </table>
8 {% endautoescape %}""")
9
10 tbody = ""
11
12 merged_row = Template("""{% autoescape off %}<tr><td colspan="{{predicate_colspan}}">{{predicate}}</td><td>{{first_object}}</td>{{other_object_cells}}</tr>{% endautoescape %}""")
13
14 object_only_row = Template("""{% autoescape off %}<tr><td>{{object}}</td></tr>{% endautoescape %}""")
15
16 previous_line = ("","",)
17 c=Context({})
18
19 colspan = 0
20 other_object_cells = ""
21 predicate = ""
22 first_object = ""
23
24 for line in results:
25 if line[0] != previous_line[0]:#if the predicate's new
26 if previous_line: #if the previous line has been set its not our first pass
27 c = Context({"colspan": colspan, "predicate": predicate, "first_object": first_object, "other_object_cells": other_object_cells})#render the merged row
28 tbody+=merged_row.render(c) #append the merged row to the tbody
29 colspan = 1
30 other_object_cells = ""
31 predicate = line[0]
32 first_object = line[1]
33 else: #old predicate
34 colspan += 1 #increment colspan
35 object_only_context = Context({"object": line[1]})
36 other_object_cells+=object_only_row.render(object_only_context) #add object only row
37 line = previous_line #store the current line as previous line (so we can compare predicates)
38
39 tableContext = Context({"tbody": tbody})
40 htmlTable = table.render(tableContext)
Discussion
It all seems rather convoluted, there's another snippet of mine that parses the results in to a dictionary of lists before generating the table - i had thought that it was rather ugly and obfuscated - this certainly doesn't look too intuitive either
Perhaps it would be better to create a table, and modify it (using dom) as we parse the results
Well I suppose that's for another day
Comments
-
Sweet Burlap 2012-05-18
The templating in Django makes it rather difficult to encode links to hash URIs using this method (we need to autoescape the hash uri, but not all the <> symbols - guess that's another argument for using BeautifulSoup)
-
Sweet Burlap 2012-05-26
sorry - not hashes aren't autoescaped - but we can use the urlencode filter to substitute %23 for hashes
Sign in to leave a comment.

