DHIS2 Web API and Python


(Waka Wafubwa) #1

Hi All,

Just in case you were wondering how to get DHIS2 data from python, see below.


Created on 17/12/2018

@author Charles Waka
"""
#Python code for getting data from DHIS2
import getpass
import numpy as np
import pandas as pd
import requests as rq
from io import StringIO

#DHIS2 username and password variables
dhis_uname="admin"
dhis_pwd="district"
dhis_url="https://play.dhis2.org/dev/api/reportTables/KJFbpIymTAo/data.csv"

#If you need to protect your password, you can use getpass
#response=rq.get(dhis_url, auth=(dhis_uname,getpass.getpass()))
response=rq.get(dhis_url, auth=(dhis_uname,dhis_pwd))

#Read the data to a pandas dataset
dset=pd.DataFrame(pd.read_csv(StringIO(response.text)))

#Get column names of data set
print(dset.columns.values) 

#Print a few rows of the dataset
dset.head(10)

#You can view the data types of your set
dset.dtypes

Regards,
Charles Waka.


(James Omutsani) #2

Thanks for sharing this @Waka_Wafubwa.

Best,
James.


(Isaiah Nyabuto) #3

Hi @charles.waka, thank you for sharing this. It’s nice however, not a good practice to follow. I would discourage storing usernames and passwords openly for security reasons.


(Abumere Ejakhegbe) #4

I just started building an application on Django and good to have a group to talk to.