-
Notifications
You must be signed in to change notification settings - Fork 294
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
Comments
@janosh what formatter are you using? I believe this would be up to the formatter. |
I‘m using black. Should I file an issue with them? |
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. |
@otaj Yeah, that surprised me as well. I would have expected the magic command prefix to be something like |
So are you asking for a new feature - run magics in comments? |
@rchiodo Yes, I think that would make a lot of sense. |
@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. |
@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. |
How about |
Are you suggesting this? I think that would work. At least I can't think of a better idea. |
Exactly! |
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. 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 |
@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 # ! - shell assignment
# ! % - line magic
# ! %% - cell magic would all be uncommented to become ! - shell assignment
% - line magic
%% - cell magic in the final code cell. |
* Add feature flag magicCommandsAsComments (#6408) * Add 6408.md into news/1 Enhancements * Fix regex white space capturing * Shorten magicCommandsAsComments flag description
Including magic commands in an interactive python file breaks automatic code formatting. For instance, this
will only be formatted to
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.The text was updated successfully, but these errors were encountered: