|
|
"""Magic functions for InteractiveShell.
|
|
|
"""
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
|
|
|
# Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
|
|
|
# Copyright (C) 2008 The IPython Development Team
|
|
|
|
|
|
# Distributed under the terms of the BSD License. The full license is in
|
|
|
# the file COPYING, distributed as part of this software.
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Imports
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
# Stdlib
|
|
|
import os
|
|
|
|
|
|
# Our own packages
|
|
|
from IPython.config.application import Application
|
|
|
from IPython.core.magic import Magics, register_magics, line_magic
|
|
|
from IPython.testing.skipdoctest import skip_doctest
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
# Magic implementation classes
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
@register_magics
|
|
|
class ExtensionsMagics(Magics):
|
|
|
"""Magics to manage the IPython extensions system."""
|
|
|
|
|
|
@line_magic
|
|
|
def install_ext(self, parameter_s=''):
|
|
|
"""Download and install an extension from a URL, e.g.::
|
|
|
|
|
|
%install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
|
|
|
|
|
|
The URL should point to an importable Python module - either a .py file
|
|
|
or a .zip file.
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
-n filename : Specify a name for the file, rather than taking it from
|
|
|
the URL.
|
|
|
"""
|
|
|
opts, args = self.parse_options(parameter_s, 'n:')
|
|
|
try:
|
|
|
filename = self.shell.extension_manager.install_extension(args,
|
|
|
opts.get('n'))
|
|
|
except ValueError as e:
|
|
|
print e
|
|
|
return
|
|
|
|
|
|
filename = os.path.basename(filename)
|
|
|
print "Installed %s. To use it, type:" % filename
|
|
|
print " %%load_ext %s" % os.path.splitext(filename)[0]
|
|
|
|
|
|
|
|
|
@line_magic
|
|
|
def load_ext(self, module_str):
|
|
|
"""Load an IPython extension by its module name."""
|
|
|
return self.shell.extension_manager.load_extension(module_str)
|
|
|
|
|
|
@line_magic
|
|
|
def unload_ext(self, module_str):
|
|
|
"""Unload an IPython extension by its module name."""
|
|
|
self.shell.extension_manager.unload_extension(module_str)
|
|
|
|
|
|
@line_magic
|
|
|
def reload_ext(self, module_str):
|
|
|
"""Reload an IPython extension by its module name."""
|
|
|
self.shell.extension_manager.reload_extension(module_str)
|
|
|
|
|
|
|
|
|
@register_magics
|
|
|
class PylabMagics(Magics):
|
|
|
"""Magics related to matplotlib's pylab support"""
|
|
|
|
|
|
@skip_doctest
|
|
|
@line_magic
|
|
|
def pylab(self, parameter_s=''):
|
|
|
"""Load numpy and matplotlib to work interactively.
|
|
|
|
|
|
%pylab [GUINAME]
|
|
|
|
|
|
This function lets you activate pylab (matplotlib, numpy and
|
|
|
interactive support) at any point during an IPython session.
|
|
|
|
|
|
It will import at the top level numpy as np, pyplot as plt, matplotlib,
|
|
|
pylab and mlab, as well as all names from numpy and pylab.
|
|
|
|
|
|
If you are using the inline matplotlib backend for embedded figures,
|
|
|
you can adjust its behavior via the %config magic::
|
|
|
|
|
|
# enable SVG figures, necessary for SVG+XHTML export in the qtconsole
|
|
|
In [1]: %config InlineBackend.figure_format = 'svg'
|
|
|
|
|
|
# change the behavior of closing all figures at the end of each
|
|
|
# execution (cell), or allowing reuse of active figures across
|
|
|
# cells:
|
|
|
In [2]: %config InlineBackend.close_figures = False
|
|
|
|
|
|
Parameters
|
|
|
----------
|
|
|
guiname : optional
|
|
|
One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
|
|
|
'osx' or 'tk'). If given, the corresponding Matplotlib backend is
|
|
|
used, otherwise matplotlib's default (which you can override in your
|
|
|
matplotlib config file) is used.
|
|
|
|
|
|
Examples
|
|
|
--------
|
|
|
In this case, where the MPL default is TkAgg::
|
|
|
|
|
|
In [2]: %pylab
|
|
|
|
|
|
Welcome to pylab, a matplotlib-based Python environment.
|
|
|
Backend in use: TkAgg
|
|
|
For more information, type 'help(pylab)'.
|
|
|
|
|
|
But you can explicitly request a different backend::
|
|
|
|
|
|
In [3]: %pylab qt
|
|
|
|
|
|
Welcome to pylab, a matplotlib-based Python environment.
|
|
|
Backend in use: Qt4Agg
|
|
|
For more information, type 'help(pylab)'.
|
|
|
"""
|
|
|
|
|
|
if Application.initialized():
|
|
|
app = Application.instance()
|
|
|
try:
|
|
|
import_all_status = app.pylab_import_all
|
|
|
except AttributeError:
|
|
|
import_all_status = True
|
|
|
else:
|
|
|
import_all_status = True
|
|
|
|
|
|
self.shell.enable_pylab(parameter_s, import_all=import_all_status)
|
|
|
|
|
|
|
|
|
@register_magics
|
|
|
class DeprecatedMagics(Magics):
|
|
|
"""Magics slated for later removal."""
|
|
|
|
|
|
@line_magic
|
|
|
def install_profiles(self, parameter_s=''):
|
|
|
"""%install_profiles has been deprecated."""
|
|
|
print '\n'.join([
|
|
|
"%install_profiles has been deprecated.",
|
|
|
"Use `ipython profile list` to view available profiles.",
|
|
|
"Requesting a profile with `ipython profile create <name>`",
|
|
|
"or `ipython --profile=<name>` will start with the bundled",
|
|
|
"profile of that name if it exists."
|
|
|
])
|
|
|
|
|
|
@line_magic
|
|
|
def install_default_config(self, parameter_s=''):
|
|
|
"""%install_default_config has been deprecated."""
|
|
|
print '\n'.join([
|
|
|
"%install_default_config has been deprecated.",
|
|
|
"Use `ipython profile create <name>` to initialize a profile",
|
|
|
"with the default config files.",
|
|
|
"Add `--reset` to overwrite already existing config files with defaults."
|
|
|
])
|
|
|
|