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""" 

2@file 

3@brief Change Jupyter configuration 

4""" 

5import sys 

6import os 

7import re 

8 

9from ..installhelper.link_shortcuts import add_shortcut_to_desktop, suffix 

10 

11 

12def noLOG(*args, **kwargs): 

13 pass 

14 

15 

16def install_mathjax(): 

17 """ 

18 install a local copy of mathjax 

19 """ 

20 from IPython.external import mathjax # pylint: disable=E1101 

21 mathjax.install_mathjax() 

22 

23 

24def setup_ipython(current_path=None, additional_path=None, apply_modification=True, 

25 shortcut=True, browser=None): 

26 """ 

27 The function applies the modification suggested in this blog post: 

28 `Travailler avec IPython notebook <http://www.xavierdupre.fr/blog/2014-02-24_nojs.html>`_ (written in French). 

29 

30 @param additional_path additional paths to add to jupyter (a list) 

31 @param current_path change the current path when running a notebook 

32 @param apply_modification apply the modification, otherwise, just create the profile 

33 @param shortcut add short cut the desktop 

34 @param browser if not None, tries to change if it finds it (IE, Chrome, Firefox, on Windows) 

35 @return path the config file 

36 

37 If you need to create a shortcut with the appropriate paths, 

38 you can use the following instructions 

39 to open :epkg:`Jupyter` notebook on a specific folder: 

40 

41 :: 

42 

43 set path=%path%;c:\\Python370_x64;c:\\Python370_x64\\Scripts 

44 jupyter-notebook --notebook-dir=_doc\\notebooks 

45 """ 

46 if sys.platform.startswith("win"): 

47 user_profile = os.environ['USERPROFILE'] 

48 profile = os.path.join(user_profile, ".jupyter") 

49 ipython_notebook_config = os.path.join( 

50 profile, 

51 "jupyter_notebook_config.py") 

52 

53 checkpath = os.path.join(profile, "checkspoints") 

54 if not os.path.exists(checkpath): 

55 os.mkdir(checkpath) 

56 

57 if not os.path.exists(ipython_notebook_config): 

58 from ..installhelper.install_cmd_helper import run_cmd 

59 exe_exe = os.path.dirname(sys.executable) 

60 if exe_exe.lower().endswith("scripts"): 

61 exe = os.path.join(exe_exe, "jupyter-notebook.exe") 

62 else: 

63 exe = os.path.join(exe_exe, "Scripts", "jupyter-notebook.exe") 

64 cmd = exe + " -y --generate-config" 

65 out, err = run_cmd(cmd, wait=True, fLOG=noLOG) 

66 

67 if not os.path.exists(ipython_notebook_config): 

68 raise Exception( 

69 "unable to create jupyter configuration in \n'{0}'\nbecause of:\n{1}\nERR-4:\n{2}\ncmd={3}".format( 

70 ipython_notebook_config, out, err, cmd)) 

71 

72 with open(ipython_notebook_config, "r") as f: 

73 text = f.read() 

74 

75 # change current path and pylab configuration 

76 for var in ["IPKernelApp.file_to_run", 

77 "ProfileDir.location", 

78 "FileNotebookManager.checkpoint_dir", 

79 "NotebookManager.notebook_dir", 

80 "NotebookApp.ipython_dir", 

81 "IPKernelApp.pylab"]: 

82 reg = re.compile("(#? *c.{0} =.*)".format(var)) 

83 alls = reg.findall(text) 

84 if len(alls) == 1 and current_path is not None: 

85 if "pylab" in var: 

86 text = text.replace( 

87 alls[0], 

88 "c.{0} = 'inline'".format(var)) 

89 elif "checkpoint_dir" in var: 

90 text = text.replace( 

91 alls[0], 

92 "c.{0} = r'{1}'".format( 

93 var, 

94 checkpath)) 

95 elif "file_to_run" not in var: 

96 text = text.replace( 

97 alls[0], 

98 "c.{1} = r'{0}'".format( 

99 current_path, 

100 var)) 

101 else: 

102 text = text.replace( 

103 alls[0], 

104 "c.{1} = '{0}\\jupyter_startup.py'".format( 

105 current_path, 

106 var)) 

107 

108 # browser 

109 if browser is not None: 

110 if sys.platform.startswith("win"): 

111 paths = {"firefox": "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", 

112 "ie": "C:\\Program Files\\Internet Explorer\\iexplore.exe", 

113 "chrome": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", 

114 } 

115 browser = browser.lower() 

116 if browser not in paths: 

117 raise KeyError( 

118 "unable to find browser {0} in [{1}]".format( 

119 browser, ",".join( 

120 paths.keys()))) 

121 subtext = """ 

122 import webbrowser 

123 webbrowser.register('{0}', None, webbrowser.GenericBrowser(r'{1}')) 

124 c.NotebookApp.browser = '{0}' 

125 """.format(browser, paths[browser]).replace(" ", "") 

126 text += subtext 

127 

128 else: 

129 raise NotImplementedError("implemented only on Windows") 

130 

131 if apply_modification: 

132 # write modification 

133 with open(ipython_notebook_config, "w") as f: 

134 f.write(text) 

135 

136 # write jupyter_startup.py 

137 rows = ["import sys"] 

138 if additional_path is not None: 

139 for p in additional_path: 

140 if not os.path.exists(p): 

141 raise FileNotFoundError(p) 

142 rows.append("sys.path.append(r'{0}')".format(p)) 

143 s = "\n".join(rows) 

144 

145 if apply_modification: 

146 with open(os.path.join(current_path, "jupyter_startup.py"), "w") as f: 

147 f.write(s) 

148 

149 return [ipython_notebook_config] 

150 

151 else: 

152 raise NotImplementedError("implemented only for Windows") 

153 

154 

155def add_shortcut_to_desktop_for_ipython(folder): 

156 """ 

157 create a shortcut on your desktop 

158 

159 @param folder notebook dir 

160 @return filename 

161 """ 

162 file = os.path.join( 

163 os.path.split( 

164 sys.executable)[0], 

165 "Scripts", 

166 "jupyter-notebook") 

167 arguments = " --notebook-dir=" + folder 

168 ver = suffix() 

169 return add_shortcut_to_desktop( 

170 file, "notebook." + ver, "Jupyter Notebook {1} ({0})".format(folder, ver), arguments) 

171 

172 

173if __name__ == "__main__": 

174 setup_ipython("C:\\temp", [], apply_modification=False)