##// END OF EJS Templates
Add %install_ext magic function.
Thomas Kluyver -
Show More
@@ -19,6 +19,8 b' Authors:'
19
19
20 import os
20 import os
21 import sys
21 import sys
22 from urllib import urlretrieve
23 from urlparse import urlparse
22
24
23 from IPython.config.configurable import Configurable
25 from IPython.config.configurable import Configurable
24 from IPython.utils.traitlets import Instance
26 from IPython.utils.traitlets import Instance
@@ -123,3 +125,22 b' class ExtensionManager(Configurable):'
123 def _call_unload_ipython_extension(self, mod):
125 def _call_unload_ipython_extension(self, mod):
124 if hasattr(mod, 'unload_ipython_extension'):
126 if hasattr(mod, 'unload_ipython_extension'):
125 return mod.unload_ipython_extension(self.shell)
127 return mod.unload_ipython_extension(self.shell)
128
129 def install_extension(self, url, filename=None):
130 """Download and install an IPython extension.
131
132 If filename is given, the file will be so named (inside the extension
133 directory). Otherwise, the name from the URL will be used. The file must
134 have a .py or .zip extension; otherwise, a ValueError will be raised.
135 """
136 # Ensure the extension directory exists
137 if not os.path.isdir(self.ipython_extension_dir):
138 os.makedirs(self.ipython_extension_dir, mode = 0777)
139
140 if filename is None:
141 filename = urlparse(url).path.split('/')[-1]
142 if os.path.splitext(filename)[1] not in ('.py', '.zip'):
143 raise ValueError("The file must have a .py or .zip extension", filename)
144
145 filename = os.path.join(self.ipython_extension_dir, filename)
146 return urlretrieve(url, filename)
@@ -3457,6 +3457,31 b' Defaulting color scheme to \'NoColor\'"""'
3457 # print simple error message, rather than traceback if we can't
3457 # print simple error message, rather than traceback if we can't
3458 # hook up the GUI
3458 # hook up the GUI
3459 error(str(e))
3459 error(str(e))
3460
3461 def magic_install_ext(self, parameter_s):
3462 """Download and install an extension from a URL, e.g.::
3463
3464 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
3465
3466 The URL should point to an importable Python module - either a .py file
3467 or a .zip file.
3468
3469 Parameters:
3470
3471 -n filename : Specify a name for the file, rather than taking it from
3472 the URL.
3473 """
3474 opts, args = self.parse_options(parameter_s, 'n:')
3475 try:
3476 filename, headers = self.extension_manager.install_extension(args, opts.get('n'))
3477 except ValueError as e:
3478 print e
3479 return
3480
3481 filename = os.path.basename(filename)
3482 print "Installed %s. To use it, type:" % filename
3483 print " %%load_ext %s" % os.path.splitext(filename)[0]
3484
3460
3485
3461 def magic_load_ext(self, module_str):
3486 def magic_load_ext(self, module_str):
3462 """Load an IPython extension by its module name."""
3487 """Load an IPython extension by its module name."""
General Comments 0
You need to be logged in to leave comments. Login now