GegznaV
February 7, 2023, 12:22am
1
Is there a way to format Python code (I'd prefer using black
for this) in Rmd/qmd files? Something like styler
, just for Python code blocks.
cderv
February 17, 2023, 9:59am
2
Aim is to check and reformat Python code cells with black ?
I would say this is a editor feature. VSCODE IDE has support for it I believe:
Did you try configuration the python extension ? See here Editing Python Code in Visual Studio Code
Quarto extension in VSCODE leverage the Python extension so I believe you should get the feature within Quarto document in VSCODE.
It seems it will be replace at some point by another specific extension. This will probably mean to make Quarto extension supports it for python code chunk.
See Editor integration - Black 24.3.0 documentation
Otherwise more generally, I think it would require for black to support text file with Python code block. It seems it is an idea shared already:
opened 06:14PM - 02 Jun 18 UTC
closed 06:45AM - 29 Jun 23 UTC
T: enhancement
help wanted
C: parser
It would be nice if black would also handle Python code blocks in documentation … files (reStructuredText, markdown etc).
(via https://github.com/pytest-dev/pytest/issues/3525#issuecomment-394101163)
There is even a specific project for it:
It would need to be teached about Quarto specific code cell syntax I believe. It will recognize
```python
...
```
But not
```{python}
...
```
Regarding possible workaround like this, it is maybe possible to create a R function that would run black on a code chunk content through reticulate , and return the code formatted. This is how it is done for R using some R packages. It could be the same but calling some python module. This would work in R Markdown and Quarto document with knitr .
For Quarto document more generally, possibly doing the above with a Quarto extension is feasible. It would take a code block content and for each python code block, run black and replace the content.
Hope this helps having some ideas
cderv
February 17, 2023, 10:16am
3
FWIW I have open a feature request
opened 10:16AM - 17 Feb 23 UTC
closed 09:18AM - 13 Mar 23 UTC
### Description
For context about the Quarto project (https://quarto.org/)
>… Quarto® is an open-source scientific and technical publishing system built on [Pandoc](https://pandoc.org/)
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
<details>
<summary>About Quarto content with Python code </summary>
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
```
````
</details>
Would that be something to consider for `blacken-docs` project ?
1 Like
Thank you for the insights. And as I understand, currently there is no Python formatter for Rmd
/qmd
files that works out of the box.
E.g., for Python Notebooks (.ipynb
), there is a command line tool nbQA that applies black
and other tools for the Python cells in .ipynb
files.
cderv
March 6, 2023, 10:25am
5
I don't know of all the tools in the Python ecosystem, so maybe there is one already. Just not something the Quarto team has done.
You should ask in a Python community (or maybe twitter ? Mattermost ?) about this. Maybe someone has made one already.
Thank you for the suggestion. I opened a Stack Overflow issue .
system
Closed
April 21, 2023, 4:30pm
7
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.