The last time I blogged was nearly a year ago and my family was one person less in size!
Adjusting to life with three kids has been demanding, so I felt it was best to take some time off from side projects.
While I still consider myself a hobbyist game developer, I don’t have the energy to commit to games right now. However, this blog satiates my desire to create. In rare instances, it has even given back to others! I had a friend lean heavily on my Flask RESTful API post to help them succeed on a technical coding challenge for a job interview.
Enough about me, on to the good stuff…
The Black Formatter
Tools should format code, not people.
By relying on a code formatter, software engineers can spend more time on more important tasks when working on their code or peer reviewing another’s code. When it comes to formatting Python code, I prefer Black.
Black can be installed easily via pip:
pip install black
Once installed, you can format any file with the following command:
black my_file.py
Black and Docker
Installing Black locally is fine, but sometimes a local install won’t do. Let’s go through how to install Black via Docker and configure autoformatting in your PyCharm project.
Note: Feel free to grab this barebones Python project to use with this article. You can also follow along using your own project inside of PyCharm.
Install Black via Docker
First, we need to install Black via Docker.
Go ahead and add a docker
folder to your project’s main folder, and then add a black
folder inside there. Add the following Dockerfile
to this new black
folder:
FROM python:3.11-slim-buster
WORKDIR /
RUN pip install black[d]
EXPOSE 45484
CMD ["blackd", "--bind-host", "0.0.0.0", "--bind-port", "45484"]
Next, we need to build this image via the following command from your project’s main directory:
docker build -t black docker/black
Once our image has finished building, we can run it inside a Docker container via the following command:
docker run -d -p 45484:45484 black
A quick docker ps
should show our container is successfully running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14443067a0c5 black "blackd --bind-host …" 2 minutes ago Up 2 minutes 0.0.0.0:45484->45484/tcp sad_carver
Formatting Your Code Automatically in PyCharm
Next, we need to configure PyCharm to automatically format our Python code via Black running in our Docker container.
Install the BlackConnect plugin for PyCharm. You can access PyCharm’s plugin marketplace by opening PyCharm’s preferences window and going to the “Plugins” menu. Once there, search for BlackConnect
and install it.
After installing BlackConnect open PyCharm’s preferences window and navigate to Tools–>BlackConnect. Configure BlackConnect with the following settings:
With BlackConnect configured, go ahead and make a small change in your code (even random whitespace will do) to a file that has not yet been Black formatted. Assuming everything went well, your code should autoformat upon save, like so:
Note: The above GIF is formatting the format_me.py
file found in my barebones Python project referenced above.
Conclusion
That does it for automatically formatting your Python code in PyCharm via Black using Docker. Your Python code should magically format itself every time you save your work!