# -*- coding: utf-8 -*-
"""
An example of a custom magic for IPython.
:githublink:`%|py|6`
"""
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
[docs]@magics_class
class CustomMagics(Magics):
[docs] @line_magic
def ENSAEl(self, line):
"""
This command can be activated by typing::
%ENSAEl
:githublink:`%|py|20`
"""
if "site" in line:
return HTML(
'<a href="http://www.xavierdupre.fr/app/ensae_teaching_cs/'
'helpsphinx/index.html">ENSAE TD</a>')
if "blog" in line:
return HTML(
'<a href="http://www.xavierdupre.fr/blog/xd_blog_nojs.html">blog</a>')
raise Exception("unknown command: " + line)
[docs] @cell_magic
def ENSAEb(self, line, cell):
"""
This command can be activated by typing::
%%ENSAEb
:githublink:`%|py|36`
"""
return [line, cell]
[docs] @line_cell_magic
def ENSAE(self, line, cell=None):
"""
This command can be activated by typing::
%ENSAE
Or::
%%ENSAE
:githublink:`%|py|49`
"""
if cell is None:
line = line.strip()
if line.startswith("download"):
spl = line.split()
if len(spl) == 2:
import pyensae.datasource
r = pyensae.datasource.download_data(spl[1])
return r
raise Exception("unable to interpret: %r" % line)
return self.ENSAEl(line)
raise RuntimeError("Unable to interpret:\n" + cell)
[docs]def load_ipython_extension(ip):
"""
Registers magics function, can be called from a notebook.
:githublink:`%|py|66`
"""
ip.register_magics(CustomMagics)