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

Stripping shell commands causing formatting issues #749

Closed
robfalck opened this issue Mar 1, 2021 · 3 comments
Closed

Stripping shell commands causing formatting issues #749

robfalck opened this issue Mar 1, 2021 · 3 comments

Comments

@robfalck
Copy link

robfalck commented Mar 1, 2021

I'm using JupyterBook to publish docs in a way that allows users to open the notebooks on colab or binder.
To ensure colab has access to our package, our notebooks typically start with a block like this:

try:
    import package
except ImportError:
    !pip install my_package

Normally one would immediately follow that shell command with another import package, but we perform that down below so that we can strip the above code from our published docs.

When testing the notebooks using Jupytext, the next line of code will cause an IndentError.

For instance, if the next line of code is:

import numpy as np

Then when running:

jupytext --check 'pytest {}' my_notebook.ipynb 

we'd see something like:

  File "/var/folders/v5/8bxf_lxx6wz5t5s099qr99w00000gq/T/paraboloid_sczvu3bt.py", line 35
    import numpy as np
    ^
IndentationError: expected an indented block

We can fix this by including an indented pass after the shell command:

try:
    import package
except ImportError:
    !pip install my_package
    pass

It seems as if the shell command is just being stripped from the code, and therefore python needs the next line to be indented. Would it be possible to just replace the shell command with pass so the explicit pass isn't necessary?

@mwouts
Copy link
Owner

mwouts commented Mar 2, 2021

Hi @robfalck , thank you for reporting this!

Well what happens is that the .py representation of the cell is

try:
    import package
except ImportError:
    # !pip install my_package

which is not a complete Python paragraph. As you mention, adding an explicit pass below the bash command is one way to solve this, and I have seen this approach in use at other places like #694.

Maybe another possibility would be to comment out entirely that cell in the .py representation passed to pytest. Would you like to try adding an active-ipynb cell tag to the try/pip install cell?

Regarding your suggestion that Jupytext itself could insert the pass command, I'd prefer not to explore that direction as it would make round trips more difficult to handle (e.g. how could we determine whether pass was initially in the notebook, or not?)

@robfalck
Copy link
Author

robfalck commented Mar 3, 2021

That works for me, thank you. I'll close this issue.

@robfalck robfalck closed this as completed Mar 3, 2021
@mwouts
Copy link
Owner

mwouts commented Mar 3, 2021

Great! Thank you @robfalck for your understanding.

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