Git Product home page Git Product logo

pyblack's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.