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

IPython magic commands need to be runnable from comments so other tools can work with them. #3263

Closed
janosh opened this issue Jul 2, 2019 · 13 comments

Comments

@janosh
Copy link
Contributor

janosh commented Jul 2, 2019

Including magic commands in an interactive python file breaks automatic code formatting. For instance, this

#%%
%load_ext autoreload
%autoreload 2

#%%
a,b,c = [1 , 2 , 3]

will only be formatted to

#%%
# %load_ext autoreload
# %autoreload 2

#%%
a, b, c = [1, 2, 3]

if the magic commands are commented out. It would be nice if magic commands didn't throw off the formatter, at least if the file contains #%% cell delimiters.

@rchiodo
Copy link
Contributor

rchiodo commented Jul 2, 2019

@janosh what formatter are you using? I believe this would be up to the formatter.

@janosh
Copy link
Contributor Author

janosh commented Jul 3, 2019

I‘m using black. Should I file an issue with them?

@otaj
Copy link

otaj commented Jul 3, 2019

Definitely do not file an issue with them - IPython magic is not python compliant. If you actually want to run the file in an interpreter (not IPython), then these magics would make it a syntax error. It would be best, if there was a way, how to keep the line/cell magic commented and still getting picked up by the interactive window.

@janosh
Copy link
Contributor Author

janosh commented Jul 3, 2019

@otaj Yeah, that surprised me as well. I would have expected the magic command prefix to be something like #% precisely so as to not interfere with running a file in regular Python and IPython

@rchiodo
Copy link
Contributor

rchiodo commented Jul 3, 2019

So are you asking for a new feature - run magics in comments?

@janosh
Copy link
Contributor Author

janosh commented Jul 3, 2019

@rchiodo Yes, I think that would make a lot of sense.

@rchiodo rchiodo changed the title IPython magic commands break code formatting IPython magic commands need to be runnable from comments so other tools can work with them. Jul 3, 2019
@janosh
Copy link
Contributor Author

janosh commented Jul 8, 2019

@rchiodo Would this be a good first issue? I could really use this so I might have a try if someone could point me in the right direction.

@rchiodo
Copy link
Contributor

rchiodo commented Jul 8, 2019

@janosh sorry but I'm not sure. We'd have to specify a way to mark comments as containing a magic command. I think I'd like to discuss the idea with the rest of the internal team first (this issue hasn't been triaged yet as everybody was out last week).

You could start with something that's behind a feature flag though. Flags are specified in the package.json. An example is enablePlotViewer. If you submitted a PR, we could use that as a starting point for the discussion.

Not sure how to escape them yet though. #%% and # %% would both escape as a cell boundary right now, so cell magics wouldn't work. #!%% maybe? That's not very discoverable though. Maybe as a multiline comment instead? '''%%bash echo foo'''. Seems too wordy.

@janosh
Copy link
Contributor Author

janosh commented Jul 8, 2019

How about #! and # !? That would allow people using shell assignments to simply comment them out with cmd+/ and have them still work as expected in VS Code's interactive window while also playing nicely with tools like linters.

@rchiodo
Copy link
Contributor

rchiodo commented Jul 8, 2019

Are you suggesting this?
#! - shell assignment
#!% - line magic
#!%% - cell magic

I think that would work. At least I can't think of a better idea.

@janosh
Copy link
Contributor Author

janosh commented Jul 8, 2019

Are you suggesting this?
#! - shell assignment
#!% - line magic
#!%% - cell magic

Exactly!

@rchiodo
Copy link
Contributor

rchiodo commented Jul 8, 2019

If you were going to try implementing this, I think you'd likely remove the comments in the same spot we modify the code for markdown.

Here:
https://github.com/microsoft/vscode-python/blob/66de533a2a72276f16d82d6d4494cebee47e63bd/src/client/datascience/cellFactory.ts#L50

Instead of splitting a cell into a possible markdown cell and a code cell, you'd also remove the comments from line/cell/shell assignments. Probably during https://github.com/microsoft/vscode-python/blob/66de533a2a72276f16d82d6d4494cebee47e63bd/src/client/datascience/cellFactory.ts#L15

@janosh
Copy link
Contributor Author

janosh commented Jul 8, 2019

@rchiodo Found some time this evening to work on this and created microsoft/vscode-python#6494. I wasn't quite sure if and where exactly I should allow for white space. Right now, there can be an arbitrary number of spaces between # and ! as well as between ! and #. So the following

#    ! - shell assignment
# ! % - line magic
#    !       %% - cell magic

would all be uncommented to become

! - shell assignment
% - line magic
%% - cell magic

in the final code cell.

rchiodo referenced this issue in microsoft/vscode-python Jul 9, 2019
* Add feature flag magicCommandsAsComments (#6408)

* Add 6408.md into news/1 Enhancements

* Fix regex white space capturing

* Shorten magicCommandsAsComments flag description
@janosh janosh closed this as completed Jul 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jul 25, 2019
@microsoft microsoft unlocked this conversation Nov 14, 2020
@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Nov 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants