Skip to main content

World Bank Open Data helpers — Python library + CLI mirroring the Stata wbopendata surface (discovery, data, country-context, multilingual, linewrap).

Project description

wb-api-tools

tests Python 3.11+ PyPI version Stata 16+ License: MIT

Package: wb-api-tools on PyPI — repo: jpazvd/wb-api-repo.

World Bank Open Data helpers in Python (library + CLI) and Stata (wbopendata ado package). Two surfaces over the same WB API v2, with a shared YAML metadata cache so discovery commands stay fast and offline-safe.

Python package: wb-api-tools on PyPIpip install wb-api-tools (live release version shown in the PyPI badge above). Parallel v0.x track to the upstream Stata wbopendata package (Stata Journal lineage, v18.x).

What's here

Surface Entry point Reference
Python library wb_api_tools.{discovery,data,text} (re-exported at package root) docs/PYTHON_USER_GUIDE.md
Python CLI wb-api-tools <subcmd> (after install) or python -m wb_api_tools <subcmd> --help on every subcommand
Stata package src/w/wbopendata.ado (v17.4.0) help wbopendata in Stata, or src/w/wbopendata.sthlp
YAML metadata cache ~/.cache/wbopendata/_wbopendata_{indicators,sources,topics}.yaml (XDG-aware) populated by wb-api-tools sync

Install

From PyPI (once published — see pyproject.toml for the canonical name):

pip install wb-api-tools

From a git checkout (dev mode):

git clone https://github.com/jpazvd/wb-api-repo.git
cd wb-api-repo
pip install -e ".[test]"

Requires Python 3.11+. The Stata package is loaded by adding src/w/ and src/_/ to Stata's adopath (or installed via net install once an SSC release lands).

Quick start

The repo ships with a runnable 7-section walkthrough that exercises the whole Python surface (discovery, live describe, get_data flag matrix, enrich_country_context, wb_text formats):

PYTHONIOENCODING=utf-8 python examples/demo_pr_b_c.py

Captured transcript: docs/PYTHON_DEMO.md.

Python CLI

After pip install, use the wb-api-tools console script (or python -m wb_api_tools if PATH doesn't include scripts). Each subcommand has --help for full flag descriptions.

Subcommand Purpose
countries Fetch country metadata
indicators Fetch indicator metadata (legacy CSV/parquet/yaml dump)
data Fetch indicator data; --no-basic skips country-context auto-merge, --geo adds capital/lat/lon, --language es switches the API path
sources List WB data sources (--all for the full set)
alltopics List all WB topic categories
info <id> Show full metadata for one indicator (from YAML cache)
describe <id> Fetch fresh metadata for one indicator (live API; --language supported)
search [term] Paginated indicator search; --source, --topic, --field, --exact
sync Populate / refresh the YAML metadata cache from the live WB API

Example:

wb-api-tools data \
    --indicators SP.POP.TOTL,NY.GDP.MKTP.CD \
    --countries "BRA;USA;IND" \
    --date 2010:2020 \
    --geo --long --out _data/wb/pop_gdp_long.csv

Output is written to --out (.csv / .parquet / .yaml / .yml) or printed as a preview if --out is omitted.

Python library

After pip install, the package is importable directly — no sys.path hacks needed:

import wb_api_tools as wb

wb.search("poverty headcount", limit=5)
df = wb.get_data(["SP.POP.TOTL"], "BRA;USA;IND", date="2020", geo=True)
wb.wrap("long indicator title ...", width=60, fmt="stack")   # for Stata graph title()

Full reference: docs/PYTHON_USER_GUIDE.md (library + CLI + Stata-parity table).

Stata package

src/w/wbopendata.ado is the v17.4.0 dispatcher; current Phase-0-through-6 surface mirrors the Python library:

  • wbopendata, sources / allsources / alltopics / info / search / describe discovery commands
  • wbopendata, indicator(X) clear data fetch with noBASIC, geo, language(es), cache(days), sync
  • linewrap(W) maxlength(N) linewrapformat(stack|newline|lines|smcl) for graph-title and SMCL formatting

Open src/w/wbopendata.sthlp in Stata's viewer or run help wbopendata once the package is on the adopath. The Python-side docs/PYTHON_USER_GUIDE.md §5 has a row-by-row Stata ↔ Python parity table.

YAML metadata cache

The offline metadata cache lives in a per-user XDG-aware directory (typically ~/.cache/wbopendata/ on POSIX or ~/AppData/Local/wbopendata/ on Windows; override with $WBOPENDATA_YAML_DIR):

  • _wbopendata_indicators.yaml — 29,511 indicators (~18 MB)
  • _wbopendata_sources.yaml — 71 sources
  • _wbopendata_topics.yaml — 21 topics

Discovery commands (info, search, sources, alltopics) read from this cache for microsecond lookups. After pip install, populate it once:

wb-api-tools sync                # download + write all three YAMLs (~10 min first time)
wb-api-tools sync --commit --tag # git-commit + tag (dev mode only)

A semi-monthly GitHub Action (.github/workflows/wb_metadata_nightly.yml — file name is historical; cron runs on the 1st and 15th of every month at 02:17 UTC, so 14–17 days apart depending on month length) keeps the repo-committed cache fresh. Manually triggerable via workflow_dispatch.

Documentation

Development

PYTHONIOENCODING=utf-8 python -m pytest tests/   # 62 cases across discovery, wb_text, wb_api_tools

Useful Makefile targets:

make wb-update-metadata   # refresh YAML cache (v0.1.0 pipeline)
make wb-metadata          # legacy YAML builder (pre-Phase-0)
make wb-metadata-csv      # legacy CSV builder
make wb-config            # batch data pulls from config.yaml

Branch model: feature work on develop; releases tag from main. See the v0.1.0 release notes for the full PR list.

Integration

The Python CLI and library plug into:

  • Makefiles / pipelines (make wb-update-metadata, cron, GitHub Actions)
  • Stata workflows (export CSV → import delimited, or use the Stata package directly)
  • R workflows (readr::read_csv or arrow::read_parquet)
  • Jupyter notebooks for ad-hoc analysis

License

See LICENSE.md. Developed to bridge Stata wbopendata workflows with modern Python pipelines for reproducible UNICEF / World Bank style analytics.

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

wb_api_tools-0.2.1.tar.gz (53.4 kB view details)

Uploaded Source

Built Distribution

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

wb_api_tools-0.2.1-py3-none-any.whl (44.3 kB view details)

Uploaded Python 3

File details

Details for the file wb_api_tools-0.2.1.tar.gz.

File metadata

  • Download URL: wb_api_tools-0.2.1.tar.gz
  • Upload date:
  • Size: 53.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wb_api_tools-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dd9f8ade5b4a27cf15f1db09d9d86cbd4a2ba08093833e9e7d24a3e9d31fe5ee
MD5 115e53b06c7ae36a309709a77883157f
BLAKE2b-256 f32a2ed3b0bee7cee51164ce5be3436910474ece0d7989c3b55741ef1c0c8f3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for wb_api_tools-0.2.1.tar.gz:

Publisher: publish.yml on jpazvd/wb-api-repo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wb_api_tools-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: wb_api_tools-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 44.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wb_api_tools-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d97a0c46cb48ea07707257a8177e70ba03d6085bee6d4687bb7c2852130d396e
MD5 6d03f26c92673d941f24e8677e75c8a1
BLAKE2b-256 43111334e5e132178c21740d74f15ea975d34c1e6cb16dba36eb87145879d32c

See more details on using hashes here.

Provenance

The following attestation bundles were made for wb_api_tools-0.2.1-py3-none-any.whl:

Publisher: publish.yml on jpazvd/wb-api-repo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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