Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief An example of a custom magic for IPython. 

5""" 

6from IPython.core.magic import Magics, magics_class, line_magic, cell_magic 

7from IPython.core.magic import line_cell_magic 

8from IPython.core.display import HTML 

9 

10 

11@magics_class 

12class CustomMagics(Magics): 

13 

14 @line_magic 

15 def ENSAEl(self, line): 

16 """ 

17 This command can be activated by typing:: 

18 

19 %ENSAEl 

20 """ 

21 if "site" in line: 

22 return HTML( 

23 '<a href="http://www.xavierdupre.fr/app/ensae_teaching_cs/' 

24 'helpsphinx/index.html">ENSAE TD</a>') 

25 if "blog" in line: 

26 return HTML( 

27 '<a href="http://www.xavierdupre.fr/blog/xd_blog_nojs.html">blog</a>') 

28 raise Exception("unknown command: " + line) 

29 

30 @cell_magic 

31 def ENSAEb(self, line, cell): 

32 """ 

33 This command can be activated by typing:: 

34 

35 %%ENSAEb 

36 """ 

37 return [line, cell] 

38 

39 @line_cell_magic 

40 def ENSAE(self, line, cell=None): 

41 """ 

42 This command can be activated by typing:: 

43 

44 %ENSAE 

45 

46 Or:: 

47 

48 %%ENSAE 

49 """ 

50 if cell is None: 

51 line = line.strip() 

52 if line.startswith("download"): 

53 spl = line.split() 

54 if len(spl) == 2: 

55 import pyensae.datasource 

56 r = pyensae.datasource.download_data(spl[1]) 

57 return r 

58 raise Exception("unable to interpret: %r" % line) 

59 return self.ENSAEl(line) 

60 raise RuntimeError("Unable to interpret:\n" + cell) 

61 

62 

63def load_ipython_extension(ip): 

64 """ 

65 Registers magics function, can be called from a notebook. 

66 """ 

67 ip.register_magics(CustomMagics)