How to enable API button for CKAN data set?

990 views Asked by At

I have successfully added a data set for the CKAN site. It enables to preview the data set as well.

Below is the URL for the data set

testckan.com:5000/dataset/my-data1/resource/4c2647ca-a4ab-4d20-8119-5c3da9d4ae14

By adding api/rest/ to the URL as below enables to expose data sets as a JSON service.

testckan.com:5000/api/rest/dataset/my-data1/resource/4c2647ca-a4ab-4d20-8119-5c3da9d4ae14

All I want to do is add an button so that user can view the data set as a JSON rather than typing it in the URL which is not user friendly.

I have seen similar button in the

http://data.gov.uk/dataset/bona-vacantia-estates-advertisements

under API & APPS. Please see the image below.

enter image description here

How to enable API button for CKAN data set like above?

2

There are 2 answers

2
Vilsepi On BEST ANSWER

You will have to modify the front-end templating for the package (aka dataset) view. You can either add this change to your own copy of CKAN source code, or like Hendrik mentioned and data.gov.uk people have done, create a CKAN extension to modify the templates.

Your example of the feature is implemented for the data.gov.uk site which is open source and available in GitHub. The specific part is in the read_common.html:

<b>API: </b><a href="${h.url_for(controller='api', register='package', action='show', id=c.pkg.name, ver='2')}">
${h.url_for(controller='api', register='package', action='show', id=c.pkg.name, ver='2')}
</a>

The url_for function is from the ckan.lib.helpers module which "is available for templates as 'h'" according to the CKAN docs, so you should be able to use it directly in the template as in the code above.

1
Hendrik Bunke On

AFAIK that's no feature of CKAN itself, thus it can not be enabled. You can, however, easily add it yourself in an extension.