Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (58 sloc) 1.79 KB
# -*- coding: utf-8 -*-
"""
@file
@brief An example of a custom magic for IPython.
"""
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
from IPython.core.magic import line_cell_magic
from IPython.core.display import HTML
@magics_class
class CustomMagics(Magics):
@line_magic
def ENSAEl(self, line):
"""
This command can be activated by typing::
%ENSAEl
"""
if "site" in line:
return HTML(
'<a href="http://www.xavierdupre.fr/app/ensae_teaching_cs/helpsphinx/index.html">ENSAE TD</a>')
elif "blog" in line:
return HTML(
'<a href="http://www.xavierdupre.fr/blog/xd_blog_nojs.html">blog</a>')
else:
raise Exception("unknown command: " + line)
@cell_magic
def ENSAEb(self, line, cell):
"""
This command can be activated by typing::
%%ENSAEb
"""
return [line, cell]
@line_cell_magic
def ENSAE(self, line, cell=None):
"""
This command can be activated by typing::
%ENSAE
Or::
%%ENSAE
"""
if cell is None:
line = line.strip()
if line.startswith("download"):
spl = line.split()
if len(spl) == 2:
import pyensae
r = pyensae.download_data(spl[1])
return r
else:
raise Exception("unable to interpret: " + line)
else:
return self.ENSAEl(line)
else:
raise Exception("unable to interpret:\n" + cell)
def load_ipython_extension(ip):
"""
Registers magics function, can be called from a notebook.
"""
ip.register_magics(CustomMagics)
You can’t perform that action at this time.