0% found this document useful (0 votes)
43 views12 pages

Black Document

Uploaded by

srikant.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views12 pages

Black Document

Uploaded by

srikant.k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

The uncompromising code formatter

- - “Any color you like.”

1
Table of Contents
1. Introduction:..................................................................3
2. Implementations: ..........................................................4
2.1 VS code: ...................................................................4
2.2 Databricks: ................................................................5
3. Linting with CI/CD: ......................................................9
4. Additional options in CI/CD Black:............................12
5. Conclusion: .................................................................12

2
1. Introduction:
• What is Black?
Black is an open-source Python code formatter that automatically
reformats Python code according to a set of pre-defined style
guidelines. It focuses on consistent formatting and enforces strict
rules to create a uniform coding style across projects. ` black` will
automatically format the file in place, adhering to the PEP 8 style
guide.

• Why Use Black?


Consistency: Black ensures that all code in your project follows
the same formatting conventions.
Readability: Well-formatted code is easier to read and
understand, which improves collaboration.
Automation: Black automatically formats your code, saving time
and reducing manual formatting efforts.

For more detailed information and examples, please refer to the official
Black documentation: https://black.readthedocs.io/en/stable/.
• Black is distributed under the MIT License. Make sure to review
and understand the terms of the license before using or
contributing to the project.

3
2. Implementations:

2.1 VS code:

1. Installation of Black by using the below command

2. We can format all the notebooks by using the below command:

3. If there are folders, we can run like the below mentioned screenshot:

4
2.2 Databricks:
Here is how you can install the `black` package in Databricks:
1. Databricks Cluster:
Make sure you have a Databricks cluster up and running.
2. Library Installation:
- Go to your Databricks workspace.
- Navigate to the cluster where you want to install `black`.
- Click on the "Libraries" tab.
3. Install Using PyPI:
- Click on the "Install New" button.
- Choose "PyPI" as the source.
- Search for `black` in the search box.
- Select the version you want to install.
4. Install the Library:
- Click the "Install" button to install the library.
5. Restart the Cluster:
- After the installation is complete, restart your Databricks cluster.
6. Import and Use:
- Create a new Databricks notebook.
- Import the `black` library.
- Use the library as needed in your notebook.

5
The below screenshot is about where we can find libraries which is
residing on cluster configurations and steps:

in the edit menu we have option called Format notebook

6
The above-mentioned methods will format the code in your Databricks
notebook.

7
2.3 Before Vs After Using the BLACK Formatter:

8
3. Linting with CI/CD:
Linting is the process of analyzing code for potential errors, stylistic
inconsistencies, and non-adherence to coding standards. It is a static
analysis technique that aims to catch issues early in the development
process, even before the code is executed. Linting tools, such as Black
for Python, help developers identify and rectify problems that might
otherwise lead to bugs, security vulnerabilities, or difficulties in
maintaining the codebase.

Note: By default, when executing the lint stage with type black, then
Jenkins spools up a virtual environment to run the Black module, which
then checks (it does not format by default) that all specified files (the
whole project, by default) are formatted in compliance with Black code
formatting standards.

Integration with CI/CD: Linting is often integrated into Continuous


Integration/Continuous Deployment (CI/CD) pipelines. This ensures that
code is automatically checked for errors and style violations before
being merged into the main codebase.

I. Integrating Black with Continuous Integration (CI/CD)


Pipelines, by adding the below piece of code in the manifest
file itself like the below:

9
`lint`: This seems to indicate the start of a configuration section for
linting settings.

`type: black`: This specifies the type of linting tool being used, which
is "black" in this case.

`enabled: true`: This indicates that linting using the specified tool
(Black) is enabled.

`blackCommand`: By default, “black --check”

`blackModules`: This is utilized where you need to execute the black


lint on specified module list:

10
Before applying black to the notebook’s books integrated with CI/CD
pipelines (error one which is not formatted by black):

After Applying black to notebook’s integrated with CI/CD pipelines


(Success, which is formatted by black):

Benefits of CI/CD Integration with Black


- Enforce consistent code formatting across the codebase
automatically.
- Catch formatting issues early in the development cycle, reducing
technical debt.
- Improve code quality and maintainability by automating code
formatting checks
- Encourage best practices and collaboration by standardizing code
formatting conventions.

11
4. Additional options in CI/CD Black:

Black, the Python code formatter, provides several options that you can
use to customize its behavior. These options can be specified in a
configuration file (such as `manifest.yaml`) or passed as command-line
arguments. Here are some of the commonly used additional options:

5. Conclusion:
• `black` is a powerful tool that simplifies code formatting, making
your Python codebase more readable and consistent. By
integrating it into your CI/CD pipeline through either VS Code or
Databricks, you can ensure that your code is consistently
formatted and adheres to best practices.
• Remember that automated code formatting should always be
accompanied by good programming practices and code reviews to
maintain code quality.
• Integrating Black into your CI/CD pipeline ensures that your
codebase is consistently and automatically formatted according to
the Black style guide. This practice helps maintain code quality
and readability while promoting collaboration and adherence to
coding conventions.

12

You might also like