Black interferes with py:percent format in indented code blocks.
See original GitHub issueI am having an issue where py:percent notebooks that have cells divided within an if statement get destroyed by black when using --pipe black
# %%
if __name__ == '__main__':
print(1)
# %%
print(2)
gets reformatted into the following, which is not equivalent
# %%
if __name__ == '__main__':
print(1)
# %%
print(2)
For example, when running the following.
jupytext --to notebook test.py jupytext --to py:percent --pipe black test.ipynb
Will cause all cells that contain only indented code to be merged into one giant cell. Are they any formats / workarounds to ensure black does not destroy the scripts like so? Or are there any relevant settings that jupytext can set that will resolve this issue?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
How to Auto-Format Your Python Code with Black
It is a great toolkit for checking your code base against coding style (PEP8), programming errors like “library imported but unused”, “Undefined ...
Read more >Jupytext documentation
Similar to the jupytext Markdown format, MyST Markdown uses code blocks to contain code cells. The difference.
Read more >Code Blocks and Indentation | Flow of Control in Python
In this chapter, Toby Donaldson looks at how to change the order in which statements are executed by using if-statements and loops.
Read more >sphinx_extensions — Quantify-Core documentation
A sphinx extension that converts python Jupyter notebook scripts .rst.py (or .rst.*.py ) in the percent format to .rst ( .rst.* ) files...
Read more >Auto Format your Python Code with Black. | by Davis David
Black can reformat your file in place according to the Black code style. It helps your brain to focus on the problem you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mwouts Yeah, that seems to fix it on my end. At least presuming it rounds trips with --test locally and the outputted notebook appears to be identical.
Here is the example colab btw: https://github.com/facebookresearch/habitat-sim/blob/master/examples/tutorials/colabs/rigid_object_tutorial.ipynb
Found another interesting edge case to keep in mind. The following cell works when transferred to colab.
but the following does not:
The extra newline at the beginning of the cell seems to reset the indentation of the entire cell. Just something to keep in mind.
JuPyTer and VSCode however don’t seem to care and both cells work fine in both of them. Colab gives an indentation error though.