Skip to main content

SDK for the COVID-19 API (Coronavirus Dashboard in the UK)

Project description

PyPi_Version PyPi_Status Format Supported_versions_of_Python lgtm License

Software Development Kit (SDK) for Python

This is a Python SDK for the COVID-19 API, as published by Public Health England on Coronavirus (COVID-19) in the UK.

The API supplies the latest data for the COVID-19 outbreak in the United Kingdom.

The endpoint for the data provided using this SDK is:

https://api.coronavirus.data.gov.uk/v1/data

The SDK is also available for JavaScript and R.

Pagination

Using this SDK will bypass the pagination process. You will always download the entire dataset unless the latest_by argument is defined.

Installation

Python 3.7+ is required to install and use this library.

To install, please run:

python -m pip install uk-covid19

and import it in Python as follows:

from uk_covid19 import Cov19API

Note that you must use uk_covid19 (with an underscore, not a hyphen) to import the library in Python.

Example

We would like to extract the number of new cases, cumulative cases, new deaths and cumulative deaths for England using the API.

We start off by importing the library into our workspace:

from uk_covid19 import Cov19API

Next, we construct the value of the filters parameter:

england_only = [
    'areaType=nation',
    'areaName=England'
]

Next step is to construct the value of the structure parameter. To do so, we need to find out the name of the metric in which we are interested. You can find this information in the Developer’s Guide on the Coronavirus Dashboard website.

In the case of this example, the metrics are as follows:

  • newCasesByPublishDate: New cases (by publish date)

  • cumCasesByPublishDate: Cumulative cases (by publish date)

  • newDeathsByDeathDate: New deaths (by death date)

  • cumDeathsByDeathDate: Cumulative deaths (by death date)

In its simplest form, we construct the structure as follows:

cases_and_deaths = {
    "date": "date",
    "areaName": "areaName",
    "areaCode": "areaCode",
    "newCasesByPublishDate": "newCasesByPublishDate",
    "cumCasesByPublishDate": "cumCasesByPublishDate",
    "newDeathsByDeathDate": "newDeathsByDeathDate",
    "cumDeathsByDeathDate": "cumDeathsByDeathDate"
}

Now, we may use filters and structure to initialise the Covid19API object:

api = Cov19API(filters=england_only, structure=cases_and_deaths)
data = api.get_json()  # Returns a dictionary
print(data)

You may also use data.get_xml() or data.get_csv() to download the data in other available formats.

{
    'data': [
        {
            'date': '2020-07-28',
            'areaName': 'England',
            'areaCode': 'E92000001',
            'newCasesByPublishDate': 547,
            'cumCasesByPublishDate': 259022,
            'newDeathsByDeathDate': None,
            'cumDeathsByDeathDate': None
        },
        {
            'date': '2020-07-27',
            'areaName': 'England',
            'areaCode': 'E92000001',
            'newCasesByPublishDate': 616,
            'cumCasesByPublishDate': 258475,
            'newDeathsByDeathDate': 20,
            'cumDeathsByDeathDate': 41282
        },
        ...
    ],
    'lastUpdate': '2020-07-28T15:34:31.000000Z',
    'length': 162,
    'totalPages': 1
}

To see the timestamp for the last update, run:

print(api.last_update)
2020-07-28T15:34:31.000000Z

To get the latest data by a specific metric, run:

all_nations = [
    "areaType=nation"
]

api = Cov19API(
    filters=all_nations,
    structure=cases_and_deaths,
    latest_by="newCasesByPublishDate"
)

data = api.get_json()

print(data)
{
    "data": [
        {
            "date": "2020-07-28",
            "areaName": "England",
            "areaCode": "E92000001",
            "newCasesByPublishDate": 547,
            "cumCasesByPublishDate": 259022,
            "newDeathsByDeathDate": None,
            "cumDeathsByDeathDate": None
        },
        {
            "date": "2020-07-28",
            "areaName": "Northern Ireland",
            "areaCode": "N92000002",
            "newCasesByPublishDate": 9,
            "cumCasesByPublishDate": 5921,
            "newDeathsByDeathDate": None,
            "cumDeathsByDeathDate": None
        },
        {
            "date": "2020-07-28",
            "areaName": "Scotland",
            "areaCode": "S92000003",
            "newCasesByPublishDate": 4,
            "cumCasesByPublishDate": 18558,
            "newDeathsByDeathDate": None,
            "cumDeathsByDeathDate": None
        },
        {
            "date": "2020-07-28",
            "areaName": "Wales",
            "areaCode": "W92000004",
            "newCasesByPublishDate": 21,
            "cumCasesByPublishDate": 17191,
            "newDeathsByDeathDate": None,
            "cumDeathsByDeathDate": None
        }
    ],
    "lastUpdate": "2020-07-28T15:34:31.000000Z",
    "length": 4,
    "totalPages": 1
}

Set the save_as input argument to a path to save the data in a file. This functionality is only available for .get_json(), .get_xml() and .get_csv() methods.

Note that the save_as argument must be set to a file name with the correct extension; that is, .json for JSON data, .xml for XML data, and .csv for CSV data. It is assumed that the directory in which you wish to save the file already exists.

You may use relative or absolute paths.

path = "data.csv"

api.get_csv(save_as="some_existing_directory/data.csv")

This will create a file entitled data.csv under some_existing_directory. The contents of the file would be as follows:

date,areaName,areaCode,newCasesByPublishDate,cumCasesByPublishDate,newDeathsByDeathDate,cumDeathsByDeathDate
2020-07-28,England,E92000001,547,259022,,
2020-07-28,Northern Ireland,N92000002,9,5921,,
2020-07-28,Scotland,S92000003,4,18558,,
2020-07-28,Wales,W92000004,21,17191,,

Set the as_string input argument to True for the .get_json() method if you wish to receive the result as a JSON string instead of a dict object:

data = api.get_json(as_string=True)
print(data)
{"data":[{"date":"2020-07-28","areaName":"England","areaCode":"E92000001","newCasesByPublishDate":547,"cumCasesByPublishDate":259022,"newDeathsByDeathDate":null,"cumDeathsByDeathDate":null},{"date":"2020-07-28","areaName":"Northern Ireland","areaCode":"N92000002","newCasesByPublishDate":9,"cumCasesByPublishDate":5921,"newDeathsByDeathDate":null,"cumDeathsByDeathDate":null},{"date":"2020-07-28","areaName":"Scotland","areaCode":"S92000003","newCasesByPublishDate":4,"cumCasesByPublishDate":18558,"newDeathsByDeathDate":null,"cumDeathsByDeathDate":null},{"date":"2020-07-28","areaName":"Wales","areaCode":"W92000004","newCasesByPublishDate":21,"cumCasesByPublishDate":17191,"newDeathsByDeathDate":null,"cumDeathsByDeathDate":null}],"lastUpdate":"2020-07-28T15:34:31.000000Z","length":4,"totalPages":1}

Developed and maintained by Public Health England.

Copyright (c) 2020, Public Health England.

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

uk_covid19-1.1.5.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

uk_covid19-1.1.5-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file uk_covid19-1.1.5.tar.gz.

File metadata

  • Download URL: uk_covid19-1.1.5.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for uk_covid19-1.1.5.tar.gz
Algorithm Hash digest
SHA256 e371be0cf047f1ce180e63459a037c663ba6b5a6c17a5a86b557b92e8fc06b2b
MD5 6f930011a4db5a4f8b32e61c0c614758
BLAKE2b-256 bb878c4381792894f49b85121c8f500b0e39c52184280ad31750c1dc7b6bc651

See more details on using hashes here.

Provenance

File details

Details for the file uk_covid19-1.1.5-py3-none-any.whl.

File metadata

  • Download URL: uk_covid19-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for uk_covid19-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b1b3c336c2b8904be060daeb26107ce156bfc974fd4790d49db0864b384c0b52
MD5 5ca5a5473fb109d8744b18976a81ceef
BLAKE2b-256 7b0bead7c70d97562241fd9418e105e113907d826a4a5f25437cd5a4d57ff27f

See more details on using hashes here.

Provenance

Supported by

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