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

Running isort on Jupytext notebooks represented as scripts break some cells #553

Closed
mwouts opened this issue Jul 3, 2020 · 4 comments · Fixed by #585
Closed

Running isort on Jupytext notebooks represented as scripts break some cells #553

mwouts opened this issue Jul 3, 2020 · 4 comments · Fixed by #585
Milestone

Comments

@mwouts
Copy link
Owner

mwouts commented Jul 3, 2020

This is related to #446 - It would be great to be able to run isort easily on a Jupytext notebook, however ATM it does not work that well.

Indeed, isort seems to move the comments preceding an import together with the import. Thus, when we run isort on a .py notebook, imports at the top of the cell are moved along the preceding cell marker (# %% or # +), breaking the corresponding cell.

Possibly one way to fix this is to insert one blank line between the cell marker and the cell content. I'll see if we can add an option to do that, and if isort works better, then. Does this option work well with black? If so, it should be made the default for when a notebook is piped into an other command.

@mwouts
Copy link
Owner Author

mwouts commented Jul 31, 2020

@timothycrosley implemented two new options in isort, see PyCQA/isort#1338.
With those options I have successfully reorder the imports in a notebook with

jupytext --pipe 'isort - --treat-comment-as-code "# %%" --float-to-top' notebook.ipynb

See this commit: a73f386
I will now test this at a larger scale.

@rijobro
Copy link

rijobro commented Jan 27, 2021

This still seems to be a problem for me (but only on 1 of many notebooks, not sure what makes it different).

Command was jupytext --pipe 'isort - --treat-comment-as-code "# %%" --float-to-top' notebook.ipynb

I'm using jupytext 1.9.1 and isort 5.7.0.

Before:
Screenshot 2021-01-27 at 11 57 46
After:
Screenshot 2021-01-27 at 11 58 07

@mwouts
Copy link
Owner Author

mwouts commented Jan 28, 2021

Hi @rijobro , thank you for reporting this. Could you share the py:percent representation of (that part) of the notebook? Especially, is there anything else than # %% on the import cell, e.g. some cell metadata?

@rijobro
Copy link

rijobro commented Jan 28, 2021

Hi, yes it seems that the cell containing the imports had the following metadata:

   "metadata": {
    "tags": []
   },

And changing it to the following solved the problem

   "metadata": {},

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

Successfully merging a pull request may close this issue.

2 participants