Skip to main content

Black for Databricks notebooks

Project description

PyPI version PyPI downloads License Code style: Black

Blackbricks

A formatting tool for your Databricks notebooks.

  • Python cells are formatted with black
  • SQL cells are formatted with sqlparse

Table of Contents

Installation and Usage

Install:

$ pip install blackbricks

Usage:

$ blackbricks --help
usage: blackbricks [-h] [--line-length LINE_LENGTH]
                   [--sql-upper | --sql-lower] [--check | --diff]
                   [--indent-with-two-spaces] [--version]
                   [filenames [filenames ...]]

Formatting tool for Databricks python notebooks. Python cells are formatted
using `black`, and SQL cells are formatted by `sqlparse`.

positional arguments:
  filenames             Path to the notebook(s) to format

optional arguments:
  -h, --help            show this help message and exit
  --line-length LINE_LENGTH
                        How many characters per line to allow. [default: ask
                        black]
  --sql-upper           SQL keywords should be uppercase
  --sql-lower           SQL keywords should be lowercase
  --check               Don't write the files back, just return the status.
                        Return code 0 means nothing would change.
  --diff                Don't write the files back, just output a diff for
                        each file on stdout
  --indent-with-two-spaces
                        Use two spaces for indentation in Python cells instead
                        of Black's default of four.
  --version             Display version information and exit.

Version control integration

Use pre-commit. Add a .pre-commit-config.yaml file to your repo with the following content (changing/removing the args as you wish):

repos:
-   repo: https://github.com/bsamseth/blackbricks
    rev: 0.3.5
    hooks:
    - id: blackbricks
      args: [--line-length=120, --indent-with-two-spaces]

Set the rev attribute to the most recent version of blackbricks. The args are optional and can be used to set any of blackbricks options.

Contributing

If you find blackbricks useful or utterly broken, you are more than welcome to contribute improvements. Please open an issue first to discuss what you want added/fixed. Unless you are just adding tests. In that case your pull request is extremely likely to be merged right away.

FAQ

How do I use blackbricks on my Databricks notebooks?

blackbricks is a command line program, meant to be used on files stored locally. Databricks provides no direct way to run tools on notebooks from within the notebook interface in your browser.

The suggested way to use this is togheter with Git.

  1. Sync your notebooks to a remote repository (through the "revision history" tab in the top right)
  2. Clone the repo locally
  3. Run blackbricks on the desired notebook files from a terminal
  4. Commit the newly formatted notebooks and push to your remote repo.
  5. Sync your notebook again to pick up the new changes.

Is there a more streamlined way to do it?

I'm considering adding an option to modify the Databricks notebooks directly (thorugh an additional commandline option). Something like

blackbricks --remote username:path/to/file  # Not possible (yet).

Click here to indicate interest, and enable watching this repo for new releases:

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

blackbricks-0.3.8-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

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