Python wrapper for DHIS2
Project description
Python wrapper for DHIS2.
Common HTTP operations (GET, POST, PUT, PATCH, DELETE)
SQLViews
CSV/JSON file loading
Server-side UID generation
Defaults to JSON, supported GETs: XML, CSV, PDF, XLS
requests as HTTP library
logzero as drop-in logging library
Supported and tested on Python 2.7, 3.4-3.6 and DHIS2 versions >= 2.25
Installation
Simply use pipenv (or pip):
pipenv install dhis2.py --user --upgrade
For instructions on installing python / pip see “The Hitchhiker’s Guide to Python” Installation Guides.
Quickstart
Create an API object:
from dhis2 import Dhis
api = Dhis('play.dhis2.org/demo', 'admin', 'district', api_version=29)
optional arguments:
api_version: DHIS2 API version
user_agent: submit your own User-Agent header
Then run requests on it:
r = api.get('organisationUnits/Rp268JB6Ne4', params={'fields': 'id,name'})
print(r.json())
# { "name": "Adonkia CHP", "id": "Rp268JB6Ne4" }
Get info
print(api.version)
# '2.29'
print(api.version_int)
# 29
print(api.revision)
# '17f7f0b'
print(api.api_url)
# 'https://play.dhis2.org/2.29/api/29'
Load authentication from file
Load from a auth JSON file in order to not store credentials in scripts. Must have the following structure:
{
"dhis": {
"baseurl": "https://play.dhis2.org/demo",
"username": "admin",
"password": "district"
}
}
from dhis2 import Dhis
api = Dhis.from_auth_file('path/to/auth.json', api_version=29, user_agent='myApp/1.0')
If no argument is specified, it tries to find a file called dish.json in:
the DHIS_HOME environment variable
your Home folder
Load a JSON file
from dhis2 import load_json
json_data = load_json('/path/to/file.json')
print(json_data)
# { "id": ... }
Load a CSV file
from dhis2 import load_csv
for row in load_csv('/path/to/file.csv'):
print(row)
# { "id": ... }
# or for a normal list
data = list(load_csv('/path/to/file.csv'))
API paging
Paging for large GET requests (JSON only)
for page in api.get_paged('organisationUnits', page_size=100):
print(page)
# { "organisationUnits": [ {...}, {...} ] } (100 elements each)
SQL Views
Get SQL View data as if you’d open a CSV file, optimized for larger payloads:
# poll a sqlView of type VIEW or MATERIALIZED_VIEW:
for row in api.get_sqlview('YOaOY605rzh', execute=True, criteria={'name': '0-11m'}):
print(row)
# {'code': 'COC_358963', 'name': '0-11m'}
# similarly, poll a sqlView of type QUERY:
for row in api.get_sqlview('qMYMT0iUGkG', var={'valueType': 'INTEGER'}):
print(row)
# again, if you want a list directly:
data = list(api.get_sqlview('qMYMT0iUGkG', var={'valueType': 'INTEGER'}))
Beginning of 2.26 you can also use normal filtering on sqlViews. In that case, it’s recommended to use the stream parameter of the Dhis.get() method.
Generate UIDs
Get server-generated UIDs (not limited to 10000)
from dhis2 import generate_uids
uids = generate_uids(20000)
print(uids)
# ['Rp268JB6Ne4', 'fa7uwpCKIwa', ... ]
GET other content types
Usually defaults to JSON but you can get other file types:
r = api.get('organisationUnits/Rp268JB6Ne4', file_type='xml')
print(r.text)
# <?xml version='1.0' encoding='UTF-8'?><organisationUnit ...
r = api.get('organisationUnits/Rp268JB6Ne4', file_type='pdf')
with open('/path/to/file.pdf', 'wb') as f:
f.write(r.content)
Logging
optional logfile= specifies log file destination
Color output depending on log level
DHIS2 log format
from dhis2 import setup_logger, logger
setup_logger(logfile='/var/log/app.log')
logger.warn(data)
logger.error('hello')
logger.warn('blup')
Exceptions
There should be only two exceptions:
APIException: DHIS2 didn’t like what you requested. See the exception’s code, url and description.
ClientException: Something didn’t work with the client not involving DHIS2.
They both inherit from Dhis2PyException.
Contribute
Feedback welcome!
Add issue
and/or install the dev environment:
pip install pipenv
git clone https://github.com/davidhuser/dhis2.py && cd dhis2.py
pipenv install --dev
pipenv run tests
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dhis2.py-1.3.0.tar.gz.
File metadata
- Download URL: dhis2.py-1.3.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47b994feda21df24f7f57c9a0b5b3c6daa86df1812645fc5e6d45e37c67d627a
|
|
| MD5 |
2536d74f4bc6844ff28e2c02b6f26b6f
|
|
| BLAKE2b-256 |
d990c9c12b189ddc25677806392e953673c257971280f8a745548697b81921ce
|
File details
Details for the file dhis2.py-1.3.0-py2.py3-none-any.whl.
File metadata
- Download URL: dhis2.py-1.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e06516a9ea869ea1badad525c8bf4391bdcb47f9b960c611185926f9710f2c87
|
|
| MD5 |
9f5b8d5b515a721b55fbc618e7ee84fe
|
|
| BLAKE2b-256 |
031f58900bd41b1abc65619939c828e46e481ac1e83ac9f9744aceca65ba84bd
|