question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Black interferes with py:percent format in indented code blocks.

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
Skylion007commented, Jul 23, 2020

@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

1reaction
Skylion007commented, Jul 14, 2020

Found another interesting edge case to keep in mind. The following cell works when transferred to colab.

# %%
if __name__ == '__main__':
     print(1)
# %%
     # INDENTED COMMENT
     print(2)

but the following does not:

# %%
if __name__ == '__main__':
     print(1)
# %%
     
     # INDENTED COMMENT
     print(2)

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found