black icon indicating copy to clipboard operation
black copied to clipboard

Black support for Databricks notebook formatting - Ignore lines starting with ! or %

Open vivek-freddy opened this issue 11 months ago • 5 comments

I have a .py file which runs as notebook

# Databricks notebook source
!pip install -U dspy python-dotenv
!pip install torchmetrics
dbutils.library.restartPython() 
# COMMAND ----------
from tqdm.notebook import tqdm
tqdm.pandas()
import logging
# COMMAND ----------
!pip install matplotlib seaborn

The above is python file which runs as notebook where each cell is defined by # COMMAND in databricks. When applying black formatting I want to skip the pip install related lines. I tried using noqa but still above is causing warnings. How do I skip checking lines which have ! or % as starting character?

My pre-commit file config is

repos:
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        name: black
        description: "Black: The uncompromising Python code formatter"
        entry: black
        language: python
        minimum_pre_commit_version: 2.9.2
        require_serial: true
        types_or: [ python, pyi ]
      - id: black-jupyter
        name: black-jupyter
        description:
          "Black: The uncompromising Python code formatter (with Jupyter Notebook support)"
        entry: black
        language: python
        minimum_pre_commit_version: 2.9.2
        require_serial: true
        types_or: [ python, pyi, jupyter ]
        additional_dependencies: [ ".[jupyter]" ]

On running

pre-commit run --all-files

Warnings which I get is

Cannot parse: 7:0: !pip install -U dspy python-dotenv

vivek-freddy avatar Jan 22 '25 07:01 vivek-freddy

Use --skip-string-normalization and # noqa Ensure you add # noqa to lines you want to skip and pass the --skip-string-normalization option to Black to prevent reformatting lines unnecessarily. For proper handling of non-Python lines like ! and %, use tools like black_nbconvert or preprocess the notebook file to filter such lines.

saksham-sak avatar Jan 26 '25 06:01 saksham-sak

please assign me this issue i want to work on it ☺️

Student-ShivamChauhan avatar Jan 26 '25 13:01 Student-ShivamChauhan

I feel like this would be a somewhat unnecessary change - see the similar issue #3557 and https://github.com/psf/black/issues/3668

cobaltt7 avatar Jan 28 '25 19:01 cobaltt7

(This is also much fiddlier to support than those ones, since this is not valid Python syntax but those are)

hauntsaninja avatar Jan 28 '25 20:01 hauntsaninja

Write a script to preprocess the .py file, comment out ! and % lines, run Black, and then uncomment the lines. Example:

Preprocess

with open("notebook.py", "r") as f: lines = f.readlines()

with open("notebook.py", "w") as f: for line in lines: if line.startswith(("!", "%")): f.write(f"# {line}") else: f.write(line)

Run Black

...

Postprocess

with open("notebook.py", "r") as f: lines = f.readlines()

with open("notebook.py", "w") as f: for line in lines: if line.startswith("# !") or line.startswith("# %"): f.write(line[2:]) else: f.write(line)

agarwalrujal avatar Jan 29 '25 09:01 agarwalrujal

9k

Mazhar413 avatar Jul 25 '25 22:07 Mazhar413

We're probably not going to support this. It's difficult to support things that do not parse as Python. Building a little hacky wrapper script around Black might be your best option.

hauntsaninja avatar Jul 25 '25 22:07 hauntsaninja