Module:core.magics.script
Magic functions for running cells in various scripts.
2 Classes
- classIPython.core.magics.script.ScriptMagics(**kwargs:Any)
Bases:
MagicsMagics for talking to scripts
This defines a base
%%scriptcell magic for running a cellwith a program in a subprocess, and registers a few top-levelmagics that call %%script with common interpreters.- __init__(shell=None)
Create a configurable given a config config.
- Parameters:
config (Config) – If this is empty, default values are used. If config is a
Configinstance, it will be used to configure theinstance.parent (Configurable instance,optional) – The parent Configurable instance of this object.
Notes
Subclasses of Configurable must call the
__init__()method ofConfigurablebefore doing anything else and usingsuper():classMyConfigurable(Configurable):def__init__(self,config=None):super(MyConfigurable,self).__init__(config=config)# Then any other code you need to finish initialization.
This ensures that instances will be configured properly.
- event_loop
The event loop on which to run subprocesses
Not the main event loop,because we want to be able to make blocking callsand have certain requirements we don’t want to impose on the main loop.
- kill_bg_processes()
Kill all BG processes which are still running.
- killbgscripts(_nouse_='')
Kill all BG processes started by %%script and its family.
- script_magics:List
Extra script cell magics to define
This generates simple wrappers of
%%scriptfooas%%foo.If you want to add script magics that aren’t on your path,specify them in script_paths
- script_paths
Dict mapping short ‘ruby’ names to full paths, such as ‘/opt/secret/bin/ruby’
Only necessary for items in script_magics where the default path will notfind the right interpreter.
- shebang(line,cell)
%shebang[--no-raise-error][--procPROC][--bg][--errERR][--outOUT]
Run a cell via a shell command
The
%%scriptline is like the #! line of script,specifying a program (bash, perl, ruby, etc.) with which to run.The rest of the cell is run by that program.
Changed in version 9.0:Interrupting the script executed without
--bgwill end inraising an exception (unless--no-raise-erroris passed).Examples
In [1]: %%script bash ...: for i in 1 2 3; do ...: echo $i ...: done123
- options:
- --no-raise-error
Whether you should raise an error message in addition to astream on stderr if you get a nonzero exit code.
- --procPROC
The variable in which to store Popen instance. This isused only when –bg option is given.
- --bg
Whether to run the script in the background. If given, theonly way to see the output of the command is with–out/err.
- --errERR
The variable in which to store stderr from the script. Ifthe script is backgrounded, this will be the stderrpipe, instead of the stderr text itself and will not beautoclosed.
- --outOUT
The variable in which to store stdout from the script. Ifthe script is backgrounded, this will be the stdoutpipe, instead of the stderr text itself and will not beauto closed.
1 Function
- IPython.core.magics.script.script_args(f)
single decorator for adding script args