Cypher variable names in py2neo and flask templates

Probably been learning too much Python today. Excuse the tired newbie question.

Trying to use py2neo with flask templates (which are just WTForms with some extensions, which is using Jinja).

If I do a
mylist = graph.run ("MATCH (n:Things) return n")
render_template ( "somelist.html", mylist = mylist)

Then in the template I have to do
{% for thing in mylist %}

  • {{ thing["n"]["someattribute"] }}
  • I can use the ".data()" function and "n.name as name, n.attribute as attribute" in the Cypher to get rid of the Cypher variable "n". But that is ugly and inflexible if I introduce another attribute to "thing"s.

    Is there a "tidy" or normal way to "lose the cypher variable n"? So that the template doesn't need to know the structure of the Cypher result?

    My queries are actually more complex so "run" is necessary (afaict), and I expect to be returning nodes of different types on some occasions, so it may make sense to lose the cypher variable very late, such as immediately before the "render_template" call. But having decided I want to render a bunch of "thing"s, I want to decouple the template from the Cypher query variable, so I just need to know the attributes of the node type in the template.

    Hi,

    I'm certainly not a Cypher expert. But maybe this will hint you in the right direction graph - How can I return all properties for a node using Cypher? - Stack Overflow

    1 Like

    I think my question is more a py2neo one than a Cypher one, but some of those approaches may work.

    I've been off learning more Python, so worst case I can fix the iteration behaviour in the tools ;)

    Might the py2neo NodeMatcher get you what you are looking for?