mwouts / jupytext

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

Escape magic commands like in VS Code #623

Open technic opened 4 years ago

technic commented 4 years ago

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?

mwouts commented 4 years ago

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?

technic commented 4 years ago

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

I think, yes.

mwouts commented 4 years ago

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?

technic commented 4 years ago

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

mwouts commented 4 years ago

@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:

%%

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](https://user-images.githubusercontent.com/29915202/95020412-43946c80-066b-11eb-9a44-31a65b5119f6.png)
mwouts commented 4 years ago

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

technic commented 4 years ago

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.

iamdionysus commented 3 years ago

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?

mwouts commented 3 years ago

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?

iamdionysus commented 3 years ago

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.

mwouts commented 3 years ago

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?

iamdionysus commented 3 years ago

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

viscioalj commented 2 years ago

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

knshnb commented 1 year ago

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