Extract datasets from KoBoToolbox
Project description
This Python package provides a wrapper around part of the KoBoToolbox kpi API, with the main goal being to ease the downloading of survey responses. It provides methods to download data from the KoBoToolbox kpi API (e.g. https://kf.kobotoolbox.org/ or https://kc.humanitarianresponse.info/) and to rearrange this data into useable structures.
Installation
KoboExtractor requires Python 3.6+.
Simply install from PyPI with:
pip3 install koboextractor
Example usage
In this example, response data is downloaded from KoBoToolbox and arranged in a form that is convenient for further processing, e.g. for storing in a different database or uploading to Google Sheets.
Import and initialise the KoboExtractor:
from koboextractor import KoboExtractor
kobo = KoboExtractor(KOBO_TOKEN, 'https://kf.kobotoolbox.org/api/v2', debug=debug)
Get the unique ID of the first asset in your KoBoToolbox account:
assets = kobo.list_assets()
asset_uid = assets['results'][0]['uid']
Information on the questions and choices in your survey can be obtained with:
asset = kobo.get_asset(asset_uid)
choice_lists = kobo.get_choices(asset)
questions = kobo.get_questions(asset=asset, unpack_multiples=True)
questions is a dictionary of the form:
{
'groups': {
GROUP_CODE: {
'label': GROUP_LABEL,
'sequence': SEQUENCE_NUMBER,
'repeat': True/False,
'questions': {
QUESTION_CODE: {
'type': QUESTION_TYPE,
'sequence': SEQUENCE_NUMBER,
'label': QUESTION_LABEL,
'list_name': CHOICE_LIST_NAME,
'choices': {
CHOICE_CODE: {
'label': CHOICE_LABEL,
'type': 'select_multiple_option',
'sequence': SEQUENCE_NUMBER
}
},
'other': {
'type': '_or_other',
'label': 'Other',
'sequence': SEQUENCE_NUMBER
}
}
},
'groups': {
GROUP_CODE: {
...
}
}
},
'questions': {
QUESTION_CODE: {
...
}
}
choices is a dictionary of the form:
{
LIST_NAME: {
'label': CHOICE_LABEL,
'sequence': SEQUENCE_NUMBER
}
}
Download all responses submitted after a certain point in time:
new_data = kobo.get_data(asset_uid, submitted_after='2020-05-20T17:29:30')
The number of downloaded results is available in new_data['count'].
new_data will be an unordered list of form submissions. We can sort this list by submission time by calling:
new_results = kobo.sort_results_by_time(new_data['results'])
Each response (list item) is a dict with several metadata keys (such as ‘_submission_time’) and key/value pairs for each answered question in the form of ‘GROUP_CODE/QUESTION_CODE’: ‘ANSWER_CODE’. Map the question and answer labels from your survey onto the coded answers in the responses:
labeled_results = []
for result in new_results: # new_results is a list of list of dicts
# Unpack answers to select_multiple questions
labeled_results.append(kobo.label_result(unlabeled_result=result, choice_lists=choice_lists, questions=questions, unpack_multiples=True))
Documentation
The full documentation is available at https://koboextractor.readthedocs.io .
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file koboextractor-0.2.1.tar.gz
.
File metadata
- Download URL: koboextractor-0.2.1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77c1288eb5f6d317c6e0db424a0b77795d558a38632f78a14ffa94263100c459 |
|
MD5 | a7b3ce582b50761f7f8575981217f3f1 |
|
BLAKE2b-256 | 8e4f0e77db0a4f7fdb4a9de376a37e554eb5b6c2ce73775d41297cfccfc21dfd |
File details
Details for the file koboextractor-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: koboextractor-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 040bdb93a546342a93af1dadf2ce0388f68d58d52ba7119e02bae89cb0de6cd8 |
|
MD5 | 8d297b57ac26ed306eaac06abcee9096 |
|
BLAKE2b-256 | 0a718dba2d9928b5102de0ed4378b947dcf6a78f40e58858a1c056202933b6f8 |