Blog

Adding custom previews to CKAN

  • Dominik Moritz
  • 20 Apr 2021
A new feature in CKAN 2.0 enables you to add custom previews for different file types. CKAN's ability to preview resources gives users a quick way to check if they have the dataset they need - as well as to begin to explore the data. CKAN has built-in previews for certain filetypes, such as images and CSV files. However, if you have resources in another format, users cannot preview them. Custom previews provide a simple way to add previews for more filetypes, or even modify existing preview methods. [caption id="" align="alignleft" width="320"][IMG: preview] Map preview of CSV file in CKAN[/caption]New previews can be build as a CKAN extension with the help of the IResourcePreview interface. This is the same interface as CKAN already uses for built-in previews. (Note that, while most of these are enabled by default, the built-in PDF preview is not. To enable PDF preview, you need to edit the CKAN .ini file and add pdf_preview to ckan.plugins.) A preview extension must implement three methods:
  • can_preview to indicate that it can preview the dataset,
  • preview_template to return the template for the preview, and
  • setup_template_variables to add the data that should be rendered to the template.
If you happen to write an extension that previews files that are fetched via an Ajax call, you should also have a look at the resource proxy extension. This offers a workaround for the same origin policy which normally prevents files being fetched from a domain that is different from the domain of the CKAN site. To get started with your own preview extension, I recommend that you read the extension documentation linked above, and then have a look at the built-in preview extensions at ckanext. If you have any questions, let us know on the mailing list and we'll try to help.