magic_functions.py
166 lines
| 5.9 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r6919 | """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. | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r6961 | |||
Fernando Perez
|
r6919 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r6961 | |||
Fernando Perez
|
r6960 | # Stdlib | ||
Fernando Perez
|
r6919 | import os | ||
Fernando Perez
|
r6960 | # Our own packages | ||
Fernando Perez
|
r6919 | from IPython.config.application import Application | ||
Fernando Perez
|
r6966 | from IPython.core.magic import Magics, register_magics, line_magic | ||
Fernando Perez
|
r6919 | from IPython.testing.skipdoctest import skip_doctest | ||
Fernando Perez
|
r6960 | #----------------------------------------------------------------------------- | ||
# Magic implementation classes | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r6919 | |||
Fernando Perez
|
r6922 | @register_magics | ||
Fernando Perez
|
r6919 | class ExtensionsMagics(Magics): | ||
"""Magics to manage the IPython extensions system.""" | ||||
Fernando Perez
|
r6922 | |||
@line_magic | ||||
def install_ext(self, parameter_s=''): | ||||
Fernando Perez
|
r6919 | """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] | ||||
Fernando Perez
|
r6922 | @line_magic | ||
def load_ext(self, module_str): | ||||
Fernando Perez
|
r6919 | """Load an IPython extension by its module name.""" | ||
return self.shell.extension_manager.load_extension(module_str) | ||||
Fernando Perez
|
r6922 | @line_magic | ||
def unload_ext(self, module_str): | ||||
Fernando Perez
|
r6919 | """Unload an IPython extension by its module name.""" | ||
self.shell.extension_manager.unload_extension(module_str) | ||||
Fernando Perez
|
r6922 | @line_magic | ||
def reload_ext(self, module_str): | ||||
Fernando Perez
|
r6919 | """Reload an IPython extension by its module name.""" | ||
self.shell.extension_manager.reload_extension(module_str) | ||||
Fernando Perez
|
r6922 | @register_magics | ||
Fernando Perez
|
r6919 | class PylabMagics(Magics): | ||
"""Magics related to matplotlib's pylab support""" | ||||
@skip_doctest | ||||
Fernando Perez
|
r6922 | @line_magic | ||
def pylab(self, parameter_s=''): | ||||
Fernando Perez
|
r6919 | """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 | ||||
Fernando Perez
|
r6922 | self.shell.enable_pylab(parameter_s, import_all=import_all_status) | ||
@register_magics | ||||
class DeprecatedMagics(Magics): | ||||
"""Magics slated for later removal.""" | ||||
Fernando Perez
|
r6923 | |||
Fernando Perez
|
r6922 | @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." | ||||
]) | ||||