mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.64k stars 386 forks source link

Skip piping non-code cells #723

Open rijobro opened 3 years ago

rijobro commented 3 years ago

To autofix notebooks, I run a command like this:

jupytext "$filename" \
        --pipe "autoflake --in-place --remove-unused-variables --imports numpy,monai,matplotlib,torch,ignite {}" \
        --pipe "isort -" \
        --pipe "black -l 79 -" \
        --pipe autopep8 \
        --pipe "sed 's/ = list()/ = []/'"

This is removing spaces from the end of markdown lines, where a double space is needed to add a paragraph break.

Is there any way to prevent these autofixes from being applied to anything other than python cells?

rijobro commented 3 years ago

I'm not sure if this can be solved with --pipe-fmt. I can't see a list of arguments for this option, I've only seen auto:percent and py:percent. I'm not sure what the latter does, but it doesn't solve my problem.

mwouts commented 3 years ago

Hi @rijobro , thanks for sharing your use case!

This is removing spaces from the end of markdown lines, where a double space is needed to add a paragraph break.

Very interesting! Yes sure the Python-oriented tools don't know that the comment is written in Markdown, and thus they don't respect this specificity.

Is there any way to prevent these autofixes from being applied to anything other than python cells?

Not at the moment. --pipe runs the selected tool on the text representation of the full notebook (in the format --pipe-fmt, which as you saw defaults to the percent format, i.e. the one with explicit # %% cell markers - probably the most robust format for this use case).

But I agree, most of the tools above will only act on the code, so it would make sense to allow running them only on the code cells. Would a --pipe-code option be what you're after? If you are open to making a PR, here a a few pointers. I think we could:

rijobro commented 3 years ago

Thanks for the quick reply!

Your suggested solution certainly looks like it would solve the problem. Unfortunately, I don't think I'll have time to implement it. In the meantime I've opted for a quick-and-easy fix: --pipe "autopep8 - --ignore W291".

This won't remove any EOL spaces for comments, which will include markdown and non-markdown, but it's a liveable solution for the time being.