Skip to main content

Apply black formatter for a piece of python source code

Project description

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

Project details


Download files

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

Source Distribution

blacken-selection-0.1.3.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file blacken-selection-0.1.3.tar.gz.

File metadata

  • Download URL: blacken-selection-0.1.3.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for blacken-selection-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2934b8bde75e604a863cc25d067303393acfbf6dafbbb73bfcd0dd32e5cef059
MD5 c907bf73a6344285742ce2987bd8bd0e
BLAKE2b-256 4688f71114495de8e3583501dd101496a576d0adc226ff85ca7969c7d97de53e

See more details on using hashes here.

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