Skip to main content

A tool that applies the Black python source code formatter to a single piece of python source code instead of a whole file or project.

Usage

$ pip install blacken-selection
$ blacken-selection -h
usage: Apply the black code formatter for a piece of python source code

positional arguments:
  SOURCE                The python source code to be formatted. If not
                        provided then source is read from stdin.

optional arguments:
  -h, --help            show this help message and exit
  -l LINE_LENGTH, --line-length LINE_LENGTH
                        How many characters per line to allow. [default: 88]
$ blacken-selection "def main(): pass"
def main():
    pass

Usage from PyCharm on linux

Unfortunately PyCharm External Tools cannot be used directly. The $SelectedText$ macro passes the selection as a command line argument to the tool but there is no way to properly quote a python string using double quotes.

But there is a solution:

  • Install the xclip command line tool, we will use it to grab the code selection in PyCharm via the clipboard
  • Install blacken_selection as described above
  • Grab the simple shell script blacken-selection-xclip.sh

Setup blacken-selection-xclip.sh as an external tool in PyCharm:

File -> Settings -> Tools -> External Tools

Click the + icon to add a new external tool with the following values:

    Name: blacken
    Description: Blacken a piece of python code
    Program: <absolute path for your blacken-selection-xclip.sh>
    Arguments:

Select a piece of python source code in your editor and from the right click context menu select:

External Tools -> blacken

Or you can confiugre a shortcut (e.g. Shift+Alt+B) to trigger the tool in:

Settings -> Keymap-> External Tools -> blacken

The tool will grab the selected text form the clipboard, format it with black, and the copy the formatted code back to the clipboard. So you can basically do the select, shift+alt+B, insert in PyCharm to format a piece of python code with black.

Metadata

Release files for blacken-selection 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for blacken-selection 0.2.0
File Size Uploaded
blacken-selection-0.2.0.tar.gz 3.4 kB Details

Release files / blacken-selection-0.2.0.tar.gz

Download URL blacken-selection-0.2.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2fc8ea108374da75cb648834d0731c713e1b33fb9eee26540150d04d4acc2e05
BLAKE2b-256 checksum
How to use checksums
eb9b5d3df571a9fec41878f287b91443565f9d7308c1518e7158c84086c8a532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.17

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page