IPython Documentation

Table Of Contents

Previous topic

core.magics.pylab

Next topic

core.oinspect

This Page

core.magics.script

Module: core.magics.script

Inheritance diagram for IPython.core.magics.script:

Magic functions for running cells in various scripts.

ScriptMagics

class IPython.core.magics.script.ScriptMagics(shell=None)

Bases: IPython.core.magic.Magics, IPython.config.configurable.Configurable

Magics for talking to scripts

This defines a base %%script cell magic for running a cell with a program in a subprocess, and registers a few top-level magics that call %%script with common interpreters.

__init__(shell=None)
arg_err(func)

Print docstring if incorrect arguments were passed

classmethod class_config_section()

Get the config class config section

classmethod class_get_help(inst=None)

Get the help string for this class in ReST format.

If inst is given, it’s current trait values will be used in place of class defaults.

classmethod class_get_trait_help(trait, inst=None)

Get the help string for a single trait.

If inst is given, it’s current trait values will be used in place of the class default.

classmethod class_print_help(inst=None)

Get the help string for a single trait and print it.

classmethod class_trait_names(**metadata)

Get a list of all the names of this classes traits.

This method is just like the trait_names() method, but is unbound.

classmethod class_traits(**metadata)

Get a list of all the traits of this class.

This method is just like the traits() method, but is unbound.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.

config

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

created = None
default_option(fn, optstr)

Make an entry in the options_table for fn, with value optstr

format_latex(strng)

Format a string for latex inclusion.

kill_bg_processes()

Kill all BG processes which are still running.

killbgscripts(_nouse_='')

Kill all BG processes started by %%script and its family.

magics = {'cell': {'script': 'shebang', 'pypy': <function named_script_magic at 0x10639b140>, 'perl': <function named_script_magic at 0x10638fde8>, 'sh': <function named_script_magic at 0x10638fc08>, 'bash': <function named_script_magic at 0x10638fcf8>, 'ruby': <function named_script_magic at 0x10638fed8>, 'python3': <function named_script_magic at 0x10639b050>}, 'line': {'killbgscripts': 'killbgscripts'}}
on_trait_change(handler, name=None, remove=False)

Setup a handler to be called when a trait changes.

This is used to setup dynamic notifications of trait changes.

Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).

Parameters :

handler : callable

A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new) or handler(name, old, new).

name : list, str, None

If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.

remove : bool

If False (the default), then install the handler. If True then unintall it.

options_table = None
parse_options(arg_str, opt_str, *long_opts, **kw)

Parse options passed to an argument string.

The interface is similar to that of getopt(), but it returns back a Struct with the options as keys and the stripped argument string still as a string.

arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.

Options:

-mode: default ‘string’. If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.

-list_all: put all option values in lists. Normally only options appearing more than once are put in a list.

-posix (True): whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.

registered = True
script_magics

An instance of a Python list.

script_paths

An instance of a Python dict.

shebang(line, cell)

%shebang [–proc PROC] [–bg] [–err ERR] [–out OUT]

Run a cell via a shell command

The %%script line 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.

Examples

In [1]: %%script bash
   ...: for i in 1 2 3; do
   ...:   echo $i
   ...: done
1
2
3
optional arguments:
--proc PROC The variable in which to store Popen instance. This is used only when –bg option is given.
--bg Whether to run the script in the background. If given, the only way to see the output of the command is with –out/err.
--err ERR The variable in which to store stderr from the script. If the script is backgrounded, this will be the stderr pipe, instead of the stderr text itself.
--out OUT The variable in which to store stdout from the script. If the script is backgrounded, this will be the stdout pipe, instead of the stderr text itself.
shell = None
trait_metadata(traitname, key)

Get metadata values for trait by key.

trait_names(**metadata)

Get a list of all the names of this classes traits.

traits(**metadata)

Get a list of all the traits of this class.

The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.

This follows the same algorithm as traits does and does not allow for any simple way of specifying merely that a metadata name exists, but has any value. This is because get_metadata returns None if a metadata key doesn’t exist.

update_config(config)

Fire the traits events when the config is updated.

IPython.core.magics.script.script_args(f)

single decorator for adding script args