Jupyter Black

0.4.0 · active · verified Thu Apr 16

Jupyter Black is an extension for Jupyter Notebook and Jupyter Lab that automatically beautifies Python code in cells using the Black formatter. It aims to provide an uncompromising, consistent code style directly within the interactive notebook environment. The current version is 0.4.0, and the project is actively maintained with updates released as needed to support newer Jupyter and Python versions.

Common errors

Warnings

Install

Imports

Quickstart

To enable jupyter-black, simply import the library and call `jupyter_black.load()` in a code cell. This can be customized with `line_length`, `verbosity`, and `target_version` arguments. Once loaded, code cells will be formatted automatically upon execution. Alternatively, you can use the IPython magic command `%load_ext jupyter_black` for default settings.

import jupyter_black
import black

# Load the extension with default settings or custom configurations
jupyter_black.load(
    line_length=88, # Default Black line length
    fast=True, # Optional: skip AST verification for speed
    target_version=black.TargetVersion.PY311 # Adjust to your Python version
)

# Code in subsequent cells will be automatically formatted on execution
def example_function ( arg1,  arg2 =  None ):
    '''
    A poorly formatted function.
    '''
    if  arg2  is None:
        return arg1
    else:    return arg1 + arg2

view raw JSON →