Skip to content

erictleung/pyblack

Repository files navigation

pyblack

R-CMD-check Project Type Toy Badge

RStudio addin to style Python code blocks with black.

Demo of the pyblack RStudio addin to help format Python code in RMarkdown

Installation

remotes::install_github("erictleung/pyblack")

You must have black installed and accessible in your path for this addin to work.

pip install black

Usage

This addin supports styling with black by:

  • highlighted selected code
  • entire active file in RStudio

For select code, in your text editor, first highlight the relevant Python code you wish to format. Then navigate in RStudio to Addins > PYBLACK > Style selection with black.

For formatting an entire active document, in your text editor, have your cursor in an open document you want to format. Then navigate in RStudio to Addins > PYBLACK > Style active file with black.

There is a third option available to change indentation from 4 spaces to 2 spaces. This does not require or use black to work.

Configuration

In your working directory, you can have a pyproject.toml file that black will read.

Below is an example pyproject.toml file that changes the line-length from the default of 88 to 80.

[tool.black]
line-length = 80

Other settings available to change can be found by running black --help.

You can read more about this here.

Conditionally apply formatting

There are two ways to tell black to apply formatting to a code block:

  1. Use # fmt: off before a block of code
  2. Use black = FALSE in chunk options

For example, black won’t format the custom_formatting object, but will format the black_formatting object.

# fmt: off
custom_formatting = [
    0,  1,  2,
    3,  4,  5,
    6,  7,  8,
]
# fmt: on
black_formatting = [
    0,  1,  2,
    3,  4,  5,
    6,  7,  8,
]

Another way to avoid formatting an entire code block is to set black = FALSE in the code chunk.

```{python, black = FALSE}

```

License

MIT

About

RStudio addin to style Python code blocks with black

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages