vscode-jupyter
vscode-jupyter copied to clipboard
Add command to run file using %run magic
It would be really useful if there was a command to run the current file in the interactive window using the %run
IPython magic. The new command (e.g. jupyter.runFileMagic
) would just have to execute %run path/to/current/script.py
in the interactive window. This would then make it possible to edit a file and run it immediately in the interactive window with the use of a keyboard shortcut like you can already with running the current file in the terminal.
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run
I currently use jupyter.runFileInteractive
from a keyboard shortcut to approximate this behaviour (which effectively copies the current file contents to the interactive window), however the %run
magic provides a number of advantages:
Expected behaviour of sys.argv
If a script is run with jupyter.runFileInteractive
, sys.argv
returns a series of values (presumably) from the Jupyter server which at best are unwanted and at worst can break code which uses sys.argv
to take command line arguments. The %run
magic, on the other hand, behaves as if the file was executed in a normal terminal, returning the correct filename and no additional values from the Jupyter server.
(Additionally, the %run
magic can be used to pass custom arguments to sys.argv
, though this wouldn't be affected by creating some new jupyter.runFileMagic
command.)
Files can be re-run using command history
Once a file has been run once, it is sometimes useful to re-run it by scrolling through command history using the up/down keys in the interactive window input box. The use of the %run
magic allows this to work properly, unlike jupyter.runFileInteractive
which doesn't appear in the command history and wouldn't reflect any changes in the file.
Clearer command history in interactive window
Multi-line commands in the interactive window are collapsed so that only the first line is visible, meaning that most files run using jupyter.runFileInteractive
all appear the same (as they all have the first line #!/usr/bin/env python3
). Therefore, if I want to see which file was run for each cell, I have to expand the command and look at the code directly which is a bit awkward. If you could easily use the %run
magic instead, the command would be a single line and therefore it would be possible to see which file was run for each cell with no user interaction.
Thanks very much!
Thanks for the suggestion.
Seems like we should just change jupyter.runFileInteractive
to use %run instead of just running all the cells in a file.