davidhuser / dhis2.py Goto Github PK
View Code? Open in Web Editor NEWGeneric and lightweight Python wrapper for the DHIS2 API using requests
License: MIT License
Generic and lightweight Python wrapper for the DHIS2 API using requests
License: MIT License
dhis2.py 2.0.2 -- 2.1.2
from dhis2 import Api
PROGRAM_UID = 'IpHINAT79UW'
def get_data_no_paging(api, params):
result = api.get(f'analytics/events/query/{PROGRAM_UID}', params=params).json()
print(result['rows'][0])
def get_data_paging(api, params):
for page in api.get_paged(f'analytics/events/query/{PROGRAM_UID}', params=params, page_size=100):
print(page['rows'][0])
def main():
api = Api('play.dhis2.org/demo', 'admin', 'district')
# doesn't work with this type of params
#params = [
# ('dimension', 'pe:LAST_12_MONTHS'),
# ('dimension', 'ou:ImspTQPwCqd'),
# ('dimension', 'A03MvHHogjR.a3kGcGDCuk6'),
# ('stage', 'A03MvHHogjR'),
# ('outputType', 'EVENT')
#]
params = {
'dimension': [
'pe:LAST_12_MONTHS',
'ou:ImspTQPwCqd',
'A03MvHHogjR.a3kGcGDCuk6'
],
'stage': 'A03MvHHogjR',
'outputType': 'EVENT'
}
get_data_no_paging(api, params)
get_data_paging(api, params)
if __name__ == '__main__':
main()
results in:
['q0Hcikut16c', 'A03MvHHogjR', '2019-11-12 00:00:00.0', '2020-11-12 01:00:00.0', '2020-11-12 01:00:00.0', '', '0.0', '0.0', 'Ngelehun CHC', 'OU_559', 'DiszpKrYNg8', '5.0']
Traceback (most recent call last):
File "main.py", line 41, in <module>
main()
File "main.py", line 38, in main
get_data_paging(api, params)
File "main.py", line 11, in get_data_paging
for page in api.get_paged(f'analytics/events/query/{PROGRAM_UID}', params=params, page_size=100):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/dhis2/api.py", line 409, in page_generator
page_count = page["pager"]["pageCount"]
KeyError: 'pager'
minor bug: Remove assertion statement:
... used assert to enforce interface constraints. However, assert is removed with compiling to optimised byte code (python -o producing *.pyo files). This caused various protections to be removed.
Test results:
>> Issue: [B101:assert_used] Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
Severity: Low Confidence: High
Location: dhis2.py/dhis2/api.py:176
More Info: https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html
175 password = section["password"]
176 assert all([baseurl, username, password])
177 except (KeyError, AssertionError):
Retry on failure for HTTP requests. e.g. with a function decorator with arguments for #retries / backoff factor
Thanks for the lovely library!
I found a limitation that should be simple to fix.
Many DHIS2 API endpoints allow repeated parameters, but this library does not.
For example, table 1.29
in the 1.17.6. Reading data values
section of DHIS2 API docs describes several parameters that "Can be repeated any number of times." (such as dataSet
, orgUnit
, period
, etc)
Luckily, the requests
library supports repeated parameters if the caller provides as a list of tuples: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests
However, the _validate_request method raises if params
is not a dict
When querying a dhis2 instance with ~4200 organisation units, I wanted to find the root org unit, namely the single one with level = 1. Using the get_paged function (even though not necessary), the get_paged function returns the single root orgunit info multiple times, base on the number of pages needed for the full org unit tree - in other words, if I set the page_size to 50, I get 85 copies of the root (~4200/50) and if I set it 500, I get 9 copies, (~4200/500)
implement optional parameter for https://requests.readthedocs.io/en/master/user/quickstart/#timeouts
You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely.
at https://github.com/davidhuser/dhis2.py/blob/master/dhis2/api.py#L202
I am seeing this on DHIS2 2.40.0:
import_response_ok
assumes that the number of imports can be found through response['importCount']
Unfortunately, this has apparently been changed to response['response']['importCount']
.
(There two responses, the parameter-name of import_response_ok
and the element returned by the api ....)
With Pygments so that a JSON string appears pretty.
http://pygments.org
pipenv install dhis2.py[pandas]
Cannot remove dataElements from dataElementGroups as in DHIS2 dev guide, section:
1.9.4.2. Adding or removing multiple objects
Here you are required to DELETE with a payload of the dataElements in the group you wish to remove.
Attempting a DELETE with payload parameter of either json=
or data=
fails as this is not supported currently in dhis2.py.
Could you please add this feature in the next release?
Many thanks,
Pete
The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.