Code source de ensae_teaching_cs.td_2a.custom_magics

# -*- coding: utf-8 -*-
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

[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="">ENSAE TD</a>') elif "blog" in line: return HTML( '<a href="">blog</a>') else: 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 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)
[docs]def load_ipython_extension(ip): """ Registers magics function, can be called from a notebook. :githublink:`%|py|69` """ ip.register_magics(CustomMagics)