Rdflib CONSTRUCT query returns a list of tuples representing a graph. However, template languages usually are most convenient with a tree-like structures of nested mixed dicts and lists (because the structure matches well with tree-like structure of HTML markup). Actually, SELECT is no better in this respect, but denormalized version of the same data.
It's quite easy to come up with some ad hoc transformation, but maybe there is some idiomatic way given the graph and some hints for "pivots", which produces a tree?
For example, if we have a graph, containing Query and ResultVar individuals (with data properties, like labels, etc), then the tree could be a list of Query with ResultVar children:
[
{'name': 'q1', 'uri': '...', 'children':
[{'name': 'x', 'value': '1', ... },
{'name': 'y', 'value': '1', ... },
...
]},
...
]
For this we may hint the method to use Query - ResultVar order. And the result is easy to use with nested "loops", which generate HTML markup in the template.
I'd not liked to reinvent the wheel, and I guess this kind of problem is not unique, but I have not found any solution.
However, I do not want the ORM approach, as it means having schema in the code, and I do not want to hardwire that.
EDIT: To clarify possible misunderstanding, the Query / ResultVar is just an example. I could use Blog / Comment or Calendar / Event instead.
EDIT2 It seems like what is being sought here is object framing, as used in JSON-LD:
Framing is the process of taking a JSON-LD document, which expresses a graph of information, and applying a specific graph layout (called a Frame).
JSON-LD Framing allows developers to query by example and force a specific tree layout to a JSON-LD document.
So, what is wanted here is some way for framing in rdflib, Python. This document ("JSON-LD: Cycle Breaking and Object Framing") gives a popular explanation of what is being sought by my question, but is there something like that for Python?