Skip to main content

Reproducible DHIS2 Python SDK for LMIC scenarios

Project description

pydhis2 logo

Total Downloads

PyPI Python CI License Ruff in submission

Quite possibly the most complete python toolkit for DHIS2
โ€” a modern SDK designed for reproducible workflows in LMIC scenarios.


๐ŸŽ‰ pydhis2 is officially released!

๐Ÿš€ Getting Started

1. Installation

pip install pydhis2

2. Run the Quick Demo

Use the CLI to run a quick demo that connects to a live DHIS2 server and fetches some data. This is the best way to verify your installation.

# Check installation
py -m pydhis2 version

# Run quick demo
py -m pydhis2 demo quick

You should see output confirming a successful connection and data retrieval:

============================================================
pydhis2 Quick Demo
============================================================
=== Testing: https://demos.dhis2.org/dq ===
   Found working API endpoint!
   System: Data Quality
   Version: 2.38.4.3
Found working server: https://demos.dhis2.org/dq

2. Querying Analytics data...
Retrieved 1 data records
...
Demo completed successfully!
๐Ÿ“– Basic Usage

Here's how to use pydhis2 in your own script. Create a file named examples/my_analysis.py:

import asyncio
import sys
from pydhis2 import get_client, DHIS2Config
from pydhis2.core.types import AnalyticsQuery

# Get client classes
AsyncDHIS2Client, SyncDHIS2Client = get_client()

async def main():
    # 1. Configure connection (using a working demo server)
    config = DHIS2Config(
        base_url="https://demos.dhis2.org/dq",
        auth=("demo", "District1#")
    )
  
    async with AsyncDHIS2Client(config) as client:
        # 2. Define query parameters
        query = AnalyticsQuery(
            dx=["b6mCG9sphIT"],   # Data element: ANC 1 Outlier Threshold
            ou="qzGX4XdWufs",    # Org unit: A-1 District Hospital
            pe="2023"            # Period: Year 2023
        )

        # 3. Fetch data and convert to a Pandas DataFrame
        df = await client.analytics.to_pandas(query)

        # 4. Analyze and display
        print("Data fetched successfully!")
        print(f"Retrieved {len(df)} records.")
        print("\n--- Data Preview ---")
        print(df.head())

if __name__ == "__main__":
    # Fix for asyncio on Windows
    if sys.platform == 'win32':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(main())

Run your script from the terminal:

py examples/my_analysis.py
๐Ÿ”ง DHIS2 Server Configuration

The examples use public DHIS2 demo servers by default. To connect to your own server, you can configure it in one of the following ways:

1. Environment Variables (Recommended)

set DHIS2_URL=https://your-dhis2-server.com
set DHIS2_USERNAME=your_username
set DHIS2_PASSWORD=your_password

# Then run any script
py examples/my_analysis.py

2. In Your Script

config = DHIS2Config(
    base_url="https://your-dhis2-server.com",  
    auth=("your_username", "your_password")
)

3. Using the CLI

py -m pydhis2 config --url "https://your-dhis2-server.com" --username "your_username"
๐Ÿ“š More Examples

The repository includes several scripts demonstrating different use cases:

Script Description
quick_demo.py Basic functionality and connection testing
demo_test.py Comprehensive API testing with HTML reports
real_health_data_demo.py Health data analysis with quality metrics
my_analysis.py A template for your own custom analysis

You can run any of them using the CLI or as standalone scripts:

# Run a specific demo via CLI
py -m pydhis2 demo health

# Or run the Python script directly
py examples/real_health_data_demo.py
๐Ÿ–ฅ๏ธ Command Line Interface

pydhis2 provides a powerful CLI for common data operations.

Data Operations (Implementation in Progress):

# Pull analytics data
py -m pydhis2 analytics pull --dx "b6mCG9sphIT" --ou "qzGX4XdWufs" --pe "2023" --out analytics.parquet

# Pull tracker events
py -m pydhis2 tracker events --program "program_id" --out events.parquet

# Run data quality review
py -m pydhis2 dqr analyze --input analytics.parquet --html dqr_report.html --json dqr_summary.json

For detailed CLI usage, run py -m pydhis2 --help.

๐Ÿš€ Reproducible Workflow: Using Project Templates

Beyond being a library, pydhis2 promotes a standardized and reproducible workflow crucial for scientific research. To jumpstart your analysis, we provide a project template powered by Cookiecutter.

Why use the template?

  • Standardization: Every project starts with the same clean, logical structure. No more guessing where configs or scripts are.
  • Rapid Start: Generate a fully functional project skeleton with a single command.
  • Best Practices: The template includes pre-configured settings for DHIS2 connection, data quality pipelines, and environment management.
  • Focus on Analysis: Spend less time on boilerplate setup and more time on your research.

Usage

  1. Install Cookiecutter:

    pip install cookiecutter
    
  2. Generate your project: Run Cookiecutter and point it to the pydhis2 template. It will ask you a few questions to personalize your new project.

    # Run from the root of the pydhis2 repository
    cookiecutter pydhis2/templates
    

    You'll be prompted for details like your project name and author info:

    project_name [My DHIS-2 Analysis Project]: Malaria Analysis Malawi
    project_slug [malaria_analysis_malawi]:
    author_name [Your Name]: Dr. Evans
    author_email [your.email@example.com]: evans@who.int
    
  3. Get a complete, ready-to-use project structure:

    malaria-analysis-malawi/
    โ”œโ”€โ”€ configs/          # DHIS-2 & DQR configurations
    โ”œโ”€โ”€ data/             # For raw and processed data
    โ”œโ”€โ”€ pipelines/        # Your analysis pipeline definitions
    โ”œโ”€โ”€ scripts/          # Runner scripts
    โ”œโ”€โ”€ .env.example      # Environment variable template
    โ””โ”€โ”€ README.md         # A dedicated README for your new project
    

Now you can cd into your new project directory and start your analysis immediately!


๐Ÿ“Š Supported Endpoints

Endpoint Read Write DataFrame Pagination Streaming
Analytics โœ… - โœ… โœ… โœ…
DataValueSets โœ… โœ… โœ… โœ… โœ…
Tracker Events โœ… โœ… โœ… โœ… โœ…
Metadata โœ… โœ… โœ… - -

๐Ÿ“‹ Compatibility

  • Python: โ‰ฅ 3.9
  • DHIS2: โ‰ฅ 2.36
  • Platforms: Windows, Linux, macOS

๐Ÿ“„ License

Apache License 2.0 - see the LICENSE file for details.


๐Ÿค Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details. Also, have a look at our Code of Conduct.


๐Ÿ“ž Support

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

pydhis2-0.2.0.tar.gz (314.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydhis2-0.2.0-py3-none-any.whl (89.2 kB view details)

Uploaded Python 3

File details

Details for the file pydhis2-0.2.0.tar.gz.

File metadata

  • Download URL: pydhis2-0.2.0.tar.gz
  • Upload date:
  • Size: 314.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for pydhis2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 73718dff47f3daec216b9a46491f7fcb6ee3ef169abf4fb344a190dd9df7f577
MD5 852330a1fcd954afd7479348e6e6b7ee
BLAKE2b-256 d2ff78a5d7881a2c3a68ec9c44683e9444e662f912583e5c22ce8ec48bc9983b

See more details on using hashes here.

File details

Details for the file pydhis2-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pydhis2-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 89.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for pydhis2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d352e7df8c6f3b27d41f782a44b6c0651f993385d7ec1e03f15176fa8019de0d
MD5 814a909c8a6c62368a5d5d3ffdbb71de
BLAKE2b-256 2199cdfc7cf60e24da4c3adfdf91dc39d284d72d63dba6dfb68dda30b7221a08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page