Skip to main content

Dapla Toolbelt

Project description

Dapla Toolbelt

PyPI Status Python Version License

Documentation Tests Coverage Quality Gate Status

pre-commit Black Ruff Poetry

Python module for use within Jupyterlab notebooks, specifically aimed for Statistics Norway's data platform called Dapla. It contains support for authenticated access to Google Services such as Google Cloud Storage (GCS) and custom Dapla services such as Maskinporten Guardian. The authentication process is based on the TokenExchangeAuthenticator for Jupyterhub.

Features

These operations are supported:

  • List contents of a bucket
  • Open a file in GCS
  • Copy a file from GCS into local
  • Load a file (CSV, JSON or XML) from GCS into a pandas dataframe
  • Save contents of a data frame into a file (CSV, JSON, XML) in GCS

When the user gives the path to a resource, they do not need to give the GCS uri, only the path. This just means users don't have to prefix a path with "gs://". It is implicitly understood that all resources accessed with this tool are located in GCS, with the first level of the path being a GCS bucket name.

Requirements

  • Python >3.8 (3.10 is preferred)
  • Poetry, install via curl -sSL https://install.python-poetry.org | python3 -

Installation

You can install Dapla Toolbelt via pip from PyPI:

pip install dapla-toolbelt

Usage

from dapla import FileClient
from dapla import GuardianClient
import pandas as pd

# Load data using the Maskinporten Guardian client
response = GuardianClient.call_api("https://data.udir.no/api/kag", "88ace991-7871-4ccc-aaec-8fb6d78ed04e", "udir:datatilssb")
data_json = response.json()

raw_data_df = pd.DataFrame(data_json)  # create pandas data frame from json
raw_data_df.head()  # show first rows of data frame

FileClient.ls("bucket-name/folder")  # list contents of given folder

# Save data into different formats
path_base = "bucket-name/folder/raw_data"
FileClient.save_pandas_to_json(raw_data_df, f"{path_base}.json")  # generate json from data frame, and save to given path
FileClient.save_pandas_to_csv(raw_data_df, f"{path_base}.csv")  # generate csv from data frame, and save to given path
FileClient.save_pandas_to_xml(raw_data_df, f"{path_base}.xml")  # generate xml from data frame, and save to given path

FileClient.cat(f"{path_base}.json")  # print contents of file

# Load data from different formats
# All these data frames should contain the same data:
df = FileClient.load_json_to_pandas(f"{path_base}.json")  # read json from path and load into pandas data frame
df.head()  # show first rows of data frame
df = FileClient.load_csv_to_pandas(f"{path_base}.csv")  # read csv from path and load into pandas data frame
df.head()  # show first rows of data frame
df = FileClient.load_xml_to_pandas(f"{path_base}.xml")  # read xml from path and load into pandas data frame
df.head()  # show first rows of data frame

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, Dapla Toolbelt is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from Statistics Norway's SSB PyPI Template.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

dapla_toolbelt-3.0.1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

dapla_toolbelt-3.0.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file dapla_toolbelt-3.0.1.tar.gz.

File metadata

  • Download URL: dapla_toolbelt-3.0.1.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.5

File hashes

Hashes for dapla_toolbelt-3.0.1.tar.gz
Algorithm Hash digest
SHA256 d9cd7f86439e04176750d8b1b1f1718b0246b6caed619836ec9827d7f0cd08d1
MD5 4e397e5dfa69a2e62dc1f68a8084fb11
BLAKE2b-256 545b8ec74f155e0cba6578ba1ab672a11ad7ed09fb16310d04f8e78d2edafcf6

See more details on using hashes here.

File details

Details for the file dapla_toolbelt-3.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dapla_toolbelt-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab4f8289966fe530fd84ddcc5a84433994e11e3b5b5fe2f15a26f99861b4df05
MD5 4eb2cde422b397573fdb33ee9310e144
BLAKE2b-256 f2697827836388f3982047f5b0adeb6a8669b6dce6b59f4d3de2fc6de2a6bf73

See more details on using hashes here.

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