adamchainz / blacken-docs

Run `black` on python code blocks in documentation files
MIT License
641 stars 42 forks source link

Support formatting python code cells in Quarto document (`.qmd` extensions) #222

Closed cderv closed 1 year ago

cderv commented 1 year ago

Description

For context about the Quarto project (https://quarto.org/)

Quarto® is an open-source scientific and technical publishing system built on Pandoc

Quarto document will be Markdown text file leveraging the Pandoc Markdown syntax, but with the addition for Computation code cells. It supports different computation engine, including Python https://quarto.org/docs/computations/python.html The could also be other block syntax containing Python code to format.

Some details below

About Quarto content with Python code Here is an example of `.qmd` using Python computation to produce a HTML document ````markdown --- title: "matplotlib demo" format: html: code-fold: true jupyter: python3 --- For a demonstration of a line plot on a polar axis, see @fig-polar. ```{python} #| label: fig-polar #| fig-cap: "A line plot on a polar axis" import numpy as np import matplotlib.pyplot as plt r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r fig, ax = plt.subplots( subplot_kw = {'projection': 'polar'} ) ax.plot(theta, r) ax.set_rticks([0.5, 1, 1.5, 2]) ax.grid(True) plt.show() ``` ```` The special code block syntax using `` ```{python} `` represents computation cells with special comments using YAML syntax for configuration and then usual Python code. These are cells that will be evaluated in order for there output to be including in a classic `.md` file, and then converted to an output format (here `.html`), by Pandoc. A Quarto document can have also usual Markdown code blocks using the `` ```python `` syntax (https://quarto.org/docs/authoring/markdown-basics.html#source-code) . Those code blocks won't be evaluated, just shown in the output as highlighted python code. For those blocks, I believe `blacken-docs` already works. Last type of code block containing python code is "unexecuted block" (https://quarto.org/docs/computations/execution-options.html#unexecuted-blocks) useful to demo Quarto syntax in an output ````markdown ```{{python}} import numpy as np ``` ````

Would that be something to consider for blacken-docs project ?

cderv commented 1 year ago

We believe that https://github.com/psf/black/issues/294 can be dealt with directly on Quarto side. We'll follow work in https://github.com/quarto-dev/quarto/issues/111 and thus close this issue as it will be easier to call the format than learn / know how to navigate a qmd file from an another tool.