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 Magic command to help within notebooks 

5""" 

6from IPython.core.magic import magics_class, line_magic 

7try: 

8 import qgrid 

9except ImportError: 

10 # We ignore that error since it is not 

11 # part of the mandatory requirements. 

12 qgrid = None 

13from jyquickhelper import add_notebook_menu 

14from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers 

15 

16 

17@magics_class 

18class MagicNotebook(MagicClassWithHelpers): 

19 

20 """ 

21 Defines magic commands to help with notebooks 

22 

23 .. versionadded:: 1.1 

24 """ 

25 

26 @staticmethod 

27 def nb_menu_parser(): 

28 """ 

29 defines the way to parse the magic command ``%nb_menu`` 

30 """ 

31 parser = MagicCommandParser( 

32 description='display a menu in the notebook based on javascript', prog="nb_menu") 

33 parser.add_argument( 

34 '-t', 

35 '--title', 

36 type=str, 

37 default="Plan", 

38 help='title before the menu') 

39 parser.add_argument( 

40 '-i', 

41 '--menu-id', 

42 type=str, 

43 default="my_menu_id", 

44 help='menu id used to locate the corresponding HTML tag div') 

45 parser.add_argument( 

46 '-f', 

47 '--format', 

48 type=str, 

49 default="html", 

50 help='format to consider, html or rst') 

51 parser.add_argument( 

52 '-l1', 

53 '--level1', 

54 type=int, 

55 default=2, 

56 help='first level to consider in the menu') 

57 parser.add_argument( 

58 '-l2', 

59 '--level2', 

60 type=int, 

61 default=4, 

62 help='last level to consider in the menu') 

63 parser.add_argument( 

64 '-r', 

65 '--raw', 

66 type=bool, 

67 default=False, 

68 help='if true, displays the javascript, otherwise the html') 

69 return parser 

70 

71 @line_magic 

72 def nb_menu(self, line): 

73 """ 

74 defines ``%nb_menu`` 

75 which displays a menu 

76 

77 .. nbref:: 

78 :title: nb_menu 

79 

80 The magic command ``%nb_menu`` displays a menu in a notebook. 

81 It parses a notebook and displays a menu gathering all sections 

82 within that notebook. The code is the following:: 

83 

84 from pyquickhelper.ipythonhelper import add_notebook_menu 

85 js = add_notebook_menu(menu_id=<menu_id>, header=<title>, format=<format>, 

86 first_level=<level1>, last_level=<level2>, 

87 raw=<raw>) 

88 """ 

89 parser = self.get_parser(MagicNotebook.nb_menu_parser, "nb_menu") 

90 args = self.get_args(line, parser) 

91 

92 if args is not None: 

93 js = add_notebook_menu(menu_id=args.menu_id, header=args.title, format=args.format, 

94 first_level=args.level1, last_level=args.level2, raw=args.raw) 

95 return js 

96 

97 @staticmethod 

98 def jsdf_parser(): 

99 """ 

100 defines the way to parse the magic command ``%jsdf`` 

101 """ 

102 parser = MagicCommandParser( 

103 description='display a pandas DataFrame based on module qgrid', prog="jsdf") 

104 parser.add_argument( 

105 'df', 

106 help='dataframe to display') 

107 parser.add_argument( 

108 '--defaultColumnWidth', 

109 type=int, 

110 default=80, 

111 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

112 parser.add_argument( 

113 '--enableColumnReorder', 

114 type=bool, 

115 default=True, 

116 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

117 parser.add_argument( 

118 '--multiColumnSort', 

119 type=bool, 

120 default=False, 

121 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

122 parser.add_argument( 

123 '--rowHeight', 

124 type=int, 

125 default=25, 

126 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

127 parser.add_argument( 

128 '--showHeaderRow', 

129 type=bool, 

130 default=False, 

131 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

132 parser.add_argument( 

133 '--forceFitColumns', 

134 type=bool, 

135 default=False, 

136 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

137 parser.add_argument( 

138 '--autoHeight', 

139 type=bool, 

140 default=False, 

141 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

142 parser.add_argument( 

143 '--enableCellNavigation', 

144 type=bool, 

145 default=True, 

146 help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options') 

147 parser.add_argument( 

148 '--precision', 

149 type=int, 

150 default=4, 

151 help='see function qgrid.set_defaults') 

152 parser.add_argument( 

153 '--editable', 

154 type=bool, 

155 default=True, 

156 help='make the output editable') 

157 return parser 

158 

159 @line_magic 

160 def jsdf(self, line): 

161 """ 

162 defines ``%jsdf`` 

163 which displays a pandas dataframe into a notebook using qgrid (javascript) 

164 

165 .. nbref:: 

166 :title: jsdf 

167 

168 The magic command ``%jsdf`` displays a dataframe using 

169 `qgrid <https://qgrid.readthedocs.io/en/latest/>`_ module. 

170 The code is the following:: 

171 

172 import qgrid 

173 if firt_call: 

174 qgrid.set_defaults(precision=<precision>) 

175 self.first_jsdf_call = False 

176 

177 df = args.df 

178 grid_options = dict(defaultColumnWidth=<defaultColumnWidth>, 

179 enableColumnReorder=<enableColumnReorder>, 

180 multiColumnSort=<multiColumnSort>, 

181 rowHeight=<rowHeight>, 

182 showHeaderRow=<showHeaderRow>, 

183 forceFitColumns=<forceFitColumns>, 

184 autoHeight=<autoHeight>, 

185 enableCellNavigation=<enableCellNavigation>) 

186 qgrid.show_grid(df, grid_options=grid_options) 

187 """ 

188 parser = self.get_parser(MagicNotebook.jsdf_parser, "jsdf") 

189 args = self.get_args(line, parser) 

190 

191 if qgrid is None: 

192 return "qgrid is not installed." 

193 

194 if not hasattr(self, "first_jsdf_call") or self.first_jsdf_call: 

195 if args is not None: 

196 qgrid.set_defaults(precision=args.precision) 

197 else: 

198 qgrid.set_defaults() 

199 self.first_jsdf_call = False 

200 

201 if args is not None: 

202 df = args.df 

203 grid_options = dict(defaultColumnWidth=args.defaultColumnWidth, 

204 enableColumnReorder=args.enableColumnReorder, 

205 multiColumnSort=args.multiColumnSort, 

206 rowHeight=args.rowHeight, 

207 showHeaderRow=args.showHeaderRow, 

208 forceFitColumns=args.forceFitColumns, 

209 autoHeight=args.autoHeight, 

210 enableCellNavigation=args.enableCellNavigation, 

211 editable=args.editable) 

212 res = qgrid.show_grid(df, grid_options=grid_options) 

213 return "" if res is None else res 

214 

215 

216def register_notebook_magics(ip=None): 

217 """ 

218 register magics function, can be called from a notebook 

219 

220 @param ip from ``get_ipython()`` 

221 """ 

222 if ip is None: 

223 from IPython import get_ipython 

224 ip = get_ipython() 

225 ip.register_magics(MagicNotebook)