My question is probably a simple one: how can I use python variables in a SPARQL query? I'm trying to make the query to recognize something that a user typed.
For example, if the user types "house", the software will run the query with that entry. For now, I've run a test with this code (I'm using Tkinter):
self.queryonto = Entry(self.frame1)
self.queryonto.pack(side=LEFT)
self.btnquery = Button(self.frame1, text='Query ontology')
self.btnquery['command'] = self.makequery
self.btnquery.pack(side=LEFT)
def makequery(self):
self.ontology = World()
self.onto = self.ontology.get_ontology('file://file.owl').load()
self.baseiri = self.onto.base_iri
self.graph = self.ontology.as_rdflib_graph()
self.request = """PREFIX ont: <{}>
SELECT ?class
WHERE {
ont:{} a ?class .
}""".format(self.baseiri, self.queryonto)
self.results = list(self.graph.query(self.request))
print(self.results)
I'm almost sure that my use of .format
is wrong. So, I ask: how to use the variables I created in this query? And, if I can do this with string manipulation, how is the right way to do it in this case?