This extension adds importing and exporting of Data Packages to CKAN datasets.
- CKAN >= 2.8
To install ckanext-datapackager
into a CKAN instance, do:
-
If you're using a virtual environment for CKAN, activate it doing, for example:
source /usr/lib/ckan/default/bin/activate
-
Install the extension:
git clone https://github.com/frictionlessdata/ckanext-datapackager.git cd ckanext-datapackager python setup.py develop pip install -r requirements.txt
-
Add
datapackager
to theckan.plugins
setting in your CKAN config file; -
Restart CKAN.
- Visit the Dataset list page (e.g.
http://your-ckan-address.com/dataset
) - Click on
Import Data Package
button; - Upload or link to a Data Package JSON or ZIP file;
- Depending on your CKAN configuration, you might also need to define the dataset's organization and visibility here.
- Review the created dataset.
- Go to the dataset's page;
- Click on
Download Data Package
button.
The extension provides two API actions for importing (package_create_from_datapackage
) and exporting (package_show_as_datapackage
) Data Packages on CKAN.
For more information on their parameters and return values, check the docstrings inside the files at ckanext/datapackager/logic/action.
If the Data Package (either the datapackage.json
file or a zip file with the datapackage.json
and the data files) is reachable through an URL, you can do a request to package_create_from_datapackage
as such:
curl -X POST \
-H 'Authorization: YOUR_CKAN_API_KEY' \
-d '{"url": "https://link.to/datapackage.json"}' \
http://CKAN_HOST/api/action/package_create_from_datapackage
You can also use ckanapi:
ckanapi action package_create_from_datapackage url=URL_TO_DATAPACKAGE owner_org=OWNER_ORGANIZATION_ID -r http://CKAN_HOST
For uploading the Data Package, check the documentation on uploading files using ckanapi or check this example using requests:
import requests
r = requests.post('http://CKAN_HOST/api/action/package_create_from_datapackage',
headers={'Authorization': YOUR_CKAN_API_KEY},
files=[('upload', file('/path/to/datapackage.json/or/file.zip'))])
For exporting a dataset as a datapackage.json
just call package_show_as_datapackage
with the relevant dataset id:
curl 'http://CKAN_HOST/api/action/package_show_as_datapackage?id=940a5fe0-0c72-41c4-8a28-8c794f399036'
{"help": "http://CKAN_HOST/api/3/action/help_show?name=package_show_as_datapackage",
"success": true,
"result": {
"name": "bond-yields-uk-2-7334836228",
"title": "Test Data Package",
"resources": [
{"url": "http://some.file",
"format": "CSV"}
]
}
}
Or if using ckanapi:
ckanapi action package_show_as_datapackage id=PACKAGE_ID -r http://CKAN_URL
Note that this returns the standard CKAN API output where the datapackage.json
file is returned under the result
key.
If you would rather like to get the datapackage.json
file directly you can use this direct endpoint:
http://CKAN_HOST/dataset/DATASET_NAME_OR_ID/datapackage.json
For instance
curl http://CKAN_HOST/dataset/bond-yields-uk-10y/datapackage.json
You'll need to install the dev requirements to run the tests:
To run the tests on CKAN >= 2.9, do:
pytest --ckan-ini=test.ini ckanext/dcat/tests
To run the tests on CKAN <= 2.8, do:
nosetests --nologcapture --ckan --with-pylons=test-nose.ini ckanext/dcat/tests/nose
Note that ckanext-datapackager's test.ini
file assumes that the relative path from it
to CKAN's test-core.ini
file is ../ckan/test-core.ini
, i.e. that you have
CKAN and ckanext-datapackager installed next to each other in the same directory. This
would normally be the case if you've done development installs of CKAN and
ckanext-datapackager.
The Open Knowledge Data Packager was written for an old CKAN version (2.2), and is now deprecated. This extension implements parts of its functionality and improves them, supporting the current CKAN version (2.4).
If you still need the old Data Packager, checkout this repository's commit 57cff1f.