astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
29.44k stars 957 forks source link

Add support for Quarto notebooks #6140

Open mjkanji opened 1 year ago

mjkanji commented 1 year ago

Copy-pasting my comment from #3792 below. Since Ruff already supports Jupyter notebooks, and Quarto is a plaintext equivalent of notebooks, it should hopefully be (relatively) easy to add support. The holy grail would be if the VS Code extension can highlight linting errors in the code blocks in an open .qmd file, but even just CLI support would be great to have as a start.


Chiming in here to ask if the development of this feature can also take Quarto (.qmd) into consideration. It's a great project aimed at providing a plaintext equivalent of Jupyter notebooks.

Hopefully, adding support shouldn't be too complicated because, like (vanilla) Markdown, .qmd files denote Python blocks explicitly, albeit with a slightly different syntax:

# This is a Markdown heading

This is regular text.

```{python}
#| echo: false

# This is a Python comment
print("Hello, Ruff!")


The `#| echo: false` at the top is a Quarto-specific comment that controls its behaviour. See [here](https://quarto.org/docs/computations/execution-options.html#output-options). I don't imagine Ruff necessarily cares about specially formatted comments, though maybe it'll become relevant if you add formatting as a core functionality, a la #1904. 

Unlike Markdown, and like Jupyter notebooks, Quarto documents "carry the context throughout the file" -- so adding support should hopefully just be a matter of treating the code blocks as a single Python script, while ignoring any Markdown outside of those blocks. 
wiraki commented 11 months ago

Just wanted to add my +1 to this request :)

JanPalasek commented 7 months ago

@mjkanji You cannot process the notebook code only by concatenating code cells and running the analysis on the whole code. This is due to jupyter specific features like cell-level magics.

However, it is pretty much the same as processing Jupyter notebooks. It only requires a different parser for cells.

mjkanji commented 7 months ago

@mjkanji You cannot process the notebook code only by concatenating code cells and running the analysis on the whole code. This is due to jupyter specific features like cell-level magics.

However, it is pretty much the same as processing Jupyter notebooks. It only requires a different parser for cells.

Awesome! Hopefully, this can be implemented soon!

tvatter commented 6 months ago

+1 that would be really awesome, and probably something like that would also close #8800.

danieltomasz commented 1 month ago

As this thread seems relevant does anyone know a way to disable rule for formatting comments in Jupyter code cell withing .ipynb notebooks?(I am using notebooks via vscode extension), I can move it to new issue, but this one seems relevant (altough my request is different in scope)

Quarto is not only a format of markdown based notebooks, but open source technical publishing system. With Quarto I can have Quarto .ipynb document/notebook with python code cells annotated via "comment pipe" on top it.

#| echo: false
import pandas as pd
import mne

Applying ruff formating is breaking it by adding space after #. How I can disable this behaviour ? # fmt: skip like below

#| echo: false         # fmt: skip

doesn't seem to work.

More discussion is here https://github.com/quarto-dev/quarto-cli/discussions/9137