jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Escape magic commands like in VS Code

Open technic opened this issue 5 years ago • 14 comments

Hi, vscode has the following feature https://github.com/microsoft/vscode-python/issues/6408 So, the magic commands in the py file commented like this are recognized:

#%%
#!%load_ext autoreload
#!%autoreload 2

Currently jupytext stores magic like this:

# %%
# %load_ext autoreload
# %autoreload 2

Is there a way to configure it or change?

technic avatar Sep 13 '20 13:09 technic

Hi @technic , thank you for sharing this. Well currently Jupytext does not have an option for this, but sure it makes sense to offer compatibility with vscode... Although it is a bit unfortunate that they took a different approach than ours :smile: Do we have any idea why they were not happy with # %%magic_command? Maybe they though it was too close to the # %% cell delimiter?

mwouts avatar Sep 13 '20 15:09 mwouts

Maybe they though it was too close to the # %% cell delimiter?

I think, yes.

technic avatar Sep 13 '20 15:09 technic

I may address the point soon. @technic , we will have to choose between the two options below

  1. We create a new option comment_magics_like_vscode (do you see a better name?), or/and
  2. We create a new py:vscode format (like the py:hydrogen format, which is the same as py:percent except for the magics that are not commented)

Which is your favorite approach?

mwouts avatar Oct 01 '20 07:10 mwouts

Thanks! For me both is ok. I think the second option is easier to setup.

technic avatar Oct 01 '20 09:10 technic

@technic , I was giving a try at this, but I am not sure I can get the magic commands to work in VS Code. Here is what I tried:

  • create a notebook with a %%time magic cell
  • export it to a script. The result is a script with two cells:
# %%
from IPython import get_ipython

# %%
get_ipython().run_cell_magic('time', '', '1 + 1')
  • add a new cell with the magic syntax reported above, i.e.
# %%
#!%%time
1 + 1
  • then execute the whole script. In my experience the #!%%time command does nothing special (unlike the run_cell_magic command), as shown below. Do you get a different outcome? In other words, are we sure that the #!% commands are still supported by VS Code? image

mwouts avatar Oct 04 '20 16:10 mwouts

Oh, and I just tried a simple (uncommented) magic command (activate the option command_magics=False, or use the py:hydrogen format), and it worked! Is this what you were looking for? Screenshot from 2020-10-04 18-03-54

mwouts avatar Oct 04 '20 16:10 mwouts

Hi, i think this option has to be enabled in vscode

python.dataScience.magicCommandsAsComments

I didn't know that uncomented commands are supported in vscode now! However this still can raise unnecessary warnings by linters.

technic avatar Oct 04 '20 17:10 technic

Hi, I think this one is on the milestone 1.7.1 but currently we are at 1.11.4. Any chance this 1.7.1 will be done in near future?

iamdionysus avatar Jul 21 '21 05:07 iamdionysus

Hi @iamdionysus , well we did not implement anything here, but rather I think we noticed that uncommented magic command could be used directly in VS code. You can get these uncommented magic commands with the py:hydrogen format. Can you give it a try and report whether or not it helps with your use case?

mwouts avatar Jul 27 '21 10:07 mwouts

Hi @mwouts Thanks for your feedback. I have tried py:hydrogen format. That will comment out magic command so I can run in the VS code. But the problem with that is it is not python code so VS code will give me it is a problem because it is not Expected expression in .py code. One reason why we use .py with jupytext is to make our python code as pure python code I guess.

iamdionysus avatar Jul 28 '21 05:07 iamdionysus

I see. I just gave a quick try to the magicCommandsAsComments setting in the jupyter extension to make sure I could activate the setting on my side: image

and indeed it worked (i.e. the magic command #!%%time became effective when I turned the setting on in vscode) image

I agree that this is better than py:hydrogen as it avoids the python code error: image

I think this should be implemented as a new option in config.py. The name will be important, what do you think of magic_commands_as_vscode_comments ?

Or would you prefer to allow comment_magics = "vscode" in addition to True and False?

mwouts avatar Aug 03 '21 08:08 mwouts

Thanks for checking it out! comment_magics = "vscode" looks good.

iamdionysus avatar Aug 03 '21 09:08 iamdionysus

Any update on this? The solution described sounds perfect, just need it to be implemented and released. Thanks.

viscioalj avatar Sep 09 '22 18:09 viscioalj

Thanks for the great software! Any update on this? I think this would be a very helpful feature for using jupytext with VSCode.

knshnb avatar Jun 12 '23 06:06 knshnb