Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

dcp-tools

Manage and load data to Data Commons Platform instances

PyPI PyPI - Python Version Docs Lint/format: Ruff

A Data Commons Platform instance takes your data as CSVs in a fixed variable-per-row shape, plus a config.json that maps each CSV's columns onto the Data Commons schema. If you're defining your own statistical variables, entities, or properties rather than reusing existing ones, you also need MCF (Meta Content Framework) files describing them. dcp-tools builds and validates that bundle in Python (or from the CLI) and uploads it to Cloud Storage to trigger the platform's ingestion job.

This package was published as bblocks-datacommons-tools until version 0.1.1, and imported as bblocks.datacommons_tools. Installing the old distribution now pulls in dcp-tools and redirects those imports with a DeprecationWarning, so existing code keeps working. Update imports to dcp_tools when convenient.

Install

pip install dcp-tools

While 1.0.0 is still in alpha, this is a pre-release, so pip won't find it unless you ask for pre-releases explicitly: pip install --pre dcp-tools.

Or from GitHub:

pip install git+https://github.com/ONEcampaign/dcp-tools

Quickstart

This builds a config and MCF for a source, a provenance, one input file, and one statistical variable, then exports the bundle to disk.

from pathlib import Path

import pandas as pd
from dcp_tools import CustomDataManager

manager = CustomDataManager()
manager.add_source(dcid="ONEData", url="https://data.one.org")
manager.add_provenance(
    dcid="ONEClimateFinance",
    url="https://datacommons.one.org/data/climate-finance-files",
    source="ONEData",
)

data = pd.DataFrame({
    "country": ["Kenya", "Kenya", "Vietnam"],
    "year": [2022, 2023, 2023],
    "variable": ["climateFinanceProvidedCommitments"] * 3,
    "value": [12.4, 15.1, 8.7],
})
manager.add_input_file(
    file_name="climate_finance/one_cf_provider_commitments.csv",
    provenance="ONEClimateFinance",
    data=data,
    column_mappings={
        "observationAbout": "country",
        "date": "year",
        "variable": "variable",
        "value": "value",
    },
    observation_properties={"unit": "USDollar"},
)

manager.add_variable_to_mcf(
    dcid="climateFinanceProvidedCommitments",
    name="Climate finance commitments (bilateral)",
    description="Funding committed for climate adaptation and mitigation projects",
    stat_type="dcid:measuredValue",
)

manager.add_mcf_file("*.mcf", provenance="ONEClimateFinance")

out_dir = Path("export/climate_finance")
out_dir.mkdir(parents=True, exist_ok=True)
manager.export_all(out_dir)

export_all writes config.json, the CSV, and both MCF files under out_dir. Since we never called set_import_name, config.json defaults importName to the export directory's name, and column mappings and the provenance name are resolved to full dcids:

{
    "importName": "climate_finance",
    "inputFiles": [
        {
            "filename": "climate_finance/one_cf_provider_commitments.csv",
            "provenance": "dcid:provenance/ONEClimateFinance",
            "columnMappings": {
                "dcid:variableMeasured": "variable",
                "dcid:observationDate": "year",
                "dcid:value": "value",
                "dcid:observationAbout": "country"
            },
            "observationProperties": {"unit": "USDollar"},
            "format": "variablePerRow"
        },
        {
            "pattern": "*.mcf",
            "provenance": "dcid:provenance/ONEClimateFinance"
        }
    ]
}

Loading it

Once you have a bundle on disk, dcp_tools.gcp_utilities uploads it and triggers the load:

from dcp_tools.gcp_utilities import get_kg_settings, upload_to_cloud_storage, run_data_load

settings = get_kg_settings(source="env", env_file="customDC.env")
upload_to_cloud_storage(settings=settings, directory="export/climate_finance")
run_data_load(settings=settings)

run_data_load triggers the DCP (Data Commons Platform) ingestion job, which ingests the new data and serves it. There's no separate redeploy step to run. See the loading-data docs for the full settings reference, and the dcp-tools CLI (upload, dataload, pipeline), which wraps this same flow.

Contributing

Contributions are welcome! See CONTRIBUTING for how to get started, report bugs, and submit changes.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dcp_tools-1.0.0a3.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

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

dcp_tools-1.0.0a3-py3-none-any.whl (56.3 kB view details)

Uploaded Python 3

File details

Details for the file dcp_tools-1.0.0a3.tar.gz.

File metadata

  • Download URL: dcp_tools-1.0.0a3.tar.gz
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dcp_tools-1.0.0a3.tar.gz
Algorithm Hash digest
SHA256 91b373ea214e770a651e01d640b7389054740c5542c3885732af0936b0ea4280
MD5 b710ae061ac9c0eaca7a370589c555ea
BLAKE2b-256 a53aa81a050feaa3d29b51dd2c42709acdf13b6333d14a0f1fe8739e8e3cb7ce

See more details on using hashes here.

File details

Details for the file dcp_tools-1.0.0a3-py3-none-any.whl.

File metadata

  • Download URL: dcp_tools-1.0.0a3-py3-none-any.whl
  • Upload date:
  • Size: 56.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dcp_tools-1.0.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 1215a85ef639c88222e54d64590a76ef4a687e7d35eae7c3fa325aa0308d6187
MD5 cf6d42beac626fb44fc61c346ee9fa66
BLAKE2b-256 f081bab7a304fdeaf1812c8baf50f670858263dfaa4e5e58556c4007e488b3f4

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0a3 This release

2 files

Supported by

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