Before importing data into DHIS2, we have to first prepare the metadata in DHIS2, like the data elements and data sets that we want to import data into. Typically this is done manually through the DHIS2 interface, but in this notebook we show how to do so using the python-client.
Connect to DHIS2¶
First, we connect the python-client to the DHIS2 instance we want to import into. In this case we use one of the public access DHIS2 instances that is continuously reset:
from dhis2_client import DHIS2Client
from dhis2_client.settings import ClientSettings
# Client configuration
cfg = ClientSettings(
base_url="https://play.im.dhis2.org/stable-2-42-3-1",
username="admin",
password="district")
client = DHIS2Client(settings=cfg)
info = client.get_system_info()
# Check if everything is working.
# You should see your current DHIS2 version info.
print("▶ Current DHIS2 version:", info["version"])▶ Current DHIS2 version: 2.42.3.1
Create DHIS2 data elements¶
We also need to create the data elements for importing data into. If you haven’t already created your data elements manually, you can follow the steps below to create the data element using the python-dhis2-client.
Let’s say we want to create two data elements: temperature and precipitation.
First, create the temperature data element:
data_element = {
"name": "2m Temperature (ERA5)",
"shortName": "Temperature (ERA5)",
"valueType": "NUMBER",
"aggregationType": "AVERAGE",
"domainType": "AGGREGATE"
}
temperature_created = client.create_data_element(data_element)
print(f"Data element creation status: {temperature_created['status']} and UID: {temperature_created['response']['uid']}")Data element creation status: OK and UID: BISZg8g0rd4
Next, create the total precipitation data element:
data_element = {
"name": "Total precipitation (ERA5)",
"shortName": "Total precipitation (ERA5)",
"valueType": "NUMBER",
"aggregationType": "SUM",
"domainType": "AGGREGATE"
}
precipitation_created = client.create_data_element(data_element)
print(f"Data element creation status: {precipitation_created['status']} and UID: {precipitation_created['response']['uid']}")Data element creation status: OK and UID: bMoGyfJoH9c
Create DHIS2 Data Sets¶
If we’re planning to import daily data values, we also create and assign our data element to a new dataset for climate variables with Daily period type:
data_set = {
"name": "Daily climate data",
"shortName": "Daily climate data",
"periodType": "Daily",
"dataSetElements": [
{
"dataElement": {"id": temperature_created['response']['uid']},
},
{
"dataElement": {"id": precipitation_created['response']['uid']}
},
]
}
data_set_created = client.create_data_set(data_set)
print(f"Data set creation status: {data_set_created['status']} and UID: {data_set_created['response']['uid']}")Data set creation status: OK and UID: u94ohDJE5Sc
Next steps¶
Now we have created two climate daily data elements that we can import data into. You can reference the data element ids created here for the remainder of the import data notebooks.