Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip piping non-code cells #723

Open
rijobro opened this issue Jan 21, 2021 · 3 comments
Open

Skip piping non-code cells #723

rijobro opened this issue Jan 21, 2021 · 3 comments

Comments

@rijobro
Copy link

rijobro commented Jan 21, 2021

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
Copy link
Author

rijobro commented Jan 21, 2021

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
Copy link
Owner

mwouts commented Jan 21, 2021

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:

  • (in the --pipe-code mode), make a copy of the original notebook that contain only the code cells before exporting it to the text buffer:
    text = writes(notebook, fmt)
  • and also, when we override the inputs of the original notebook with the reformatted ones at
    piped_notebook = combine_inputs_with_outputs(piped_notebook, notebook, fmt)

    we should tell to the combine_inputs_with_outputs function that we want to preserve the non-code cells.

@rijobro
Copy link
Author

rijobro commented Jan 21, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants