[ckan-dev] Custom function for custom extension

Jorge Pantoja jorgepantojam at gmail.com
Mon Jul 8 22:24:49 UTC 2013


Hi everyone,

Im trying to do a custom extension to create a sparql interface in order to
communicate with a sparql point I have installed (since it is in a local
network that cannot be accessed from outside). Im using sparql wrapper (
http://sparql-wrapper.sourceforge.net/) to do that.

I need to call a python function in the jinja template in order to send the
query and display the results in the screen.

However, I dont know how to setup the function correctly in the ckan jinja
environment, so I can use it in the template.

Can you help me telling me how to do that correctly?

Until now, I was trying to do this, but its not working:


*import ckan.plugins as p*
*from ckan.lib.base import BaseController, render, config*
*import ckan.lib.helpers as h*
*import sparql as sparql_lib*
*from SPARQLWrapper import SPARQLWrapper, JSON*
*
*
*class SparqlController(BaseController):*
*
*
*    def sparql_query_function(query):*
*
*
*        c = p.toolkit.c*
*        queryString = "SELECT * WHERE { ?s ?p ?o. } LIMIT 10"*
*        sparql = SPARQLWrapper("http://data2.b.upf.edu:8890/sparql")*
*        sparql.setQuery(queryString)*
*        sparql.setReturnFormat(JSON)*
*        results = sparql.query().convert()*
*        c.sparql = results*
*
*
*        return query*
*
*
*    def index(self):*
*
*
*        ckan_env = config['pylons.app_globals'].jinja_env*
*        ckan_env.filters['sparql_query_function'] = sparql_query_function*
*
*
*        return render('ckanext/sparql/index.html')*


Thanks again,

Jorge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20130709/6797536c/attachment.html>


More information about the ckan-dev mailing list