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 

11if sys.version_info[0] == 2: 

12 FileNotFoundError = Exception 

13 

14 

15def noLOG(*l, **p): 

16 pass 

17 

18 

19def install_mathjax(): 

20 """ 

21 install a local copy of mathjax 

22 """ 

23 from IPython.external import mathjax 

24 mathjax.install_mathjax() 

25 

26 

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

28 shortcut=True, browser=None): 

29 """ 

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

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

32 

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

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

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

36 @param shortcut add short cut the desktop 

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

38 @return path the config file 

39 

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

41 you can use the following instructions 

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

43 

44 :: 

45 

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

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

48 """ 

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

50 user_profile = os.environ['USERPROFILE'] 

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

52 ipython_notebook_config = os.path.join( 

53 profile, 

54 "jupyter_notebook_config.py") 

55 

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

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

58 os.mkdir(checkpath) 

59 

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

61 from ..installhelper.install_cmd_helper import run_cmd 

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

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

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

65 else: 

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

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

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

69 

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

71 raise Exception( 

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

73 ipython_notebook_config, out, err, cmd)) 

74 

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

76 text = f.read() 

77 

78 # change current path and pylab configuration 

79 for var in ["IPKernelApp.file_to_run", 

80 "ProfileDir.location", 

81 "FileNotebookManager.checkpoint_dir", 

82 "NotebookManager.notebook_dir", 

83 "NotebookApp.ipython_dir", 

84 "IPKernelApp.pylab"]: 

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

86 alls = reg.findall(text) 

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

88 if "pylab" in var: 

89 text = text.replace( 

90 alls[0], 

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

92 elif "checkpoint_dir" in var: 

93 text = text.replace( 

94 alls[0], 

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

96 var, 

97 checkpath)) 

98 elif "file_to_run" not in var: 

99 text = text.replace( 

100 alls[0], 

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

102 current_path, 

103 var)) 

104 else: 

105 text = text.replace( 

106 alls[0], 

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

108 current_path, 

109 var)) 

110 

111 # browser 

112 if browser is not None: 

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

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

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

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

117 } 

118 browser = browser.lower() 

119 if browser not in paths: 

120 raise KeyError( 

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

122 browser, ",".join( 

123 paths.keys()))) 

124 subtext = """ 

125 import webbrowser 

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

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

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

129 text += subtext 

130 

131 else: 

132 raise NotImplementedError("implemented only on Windows") 

133 

134 if apply_modification: 

135 # write modification 

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

137 f.write(text) 

138 

139 # write jupyter_startup.py 

140 rows = ["import sys"] 

141 if additional_path is not None: 

142 for p in additional_path: 

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

144 raise FileNotFoundError(p) 

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

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

147 

148 if apply_modification: 

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

150 f.write(s) 

151 

152 return [ipython_notebook_config] 

153 

154 else: 

155 raise NotImplementedError("implemented only for Windows") 

156 

157 

158def add_shortcut_to_desktop_for_ipython(folder): 

159 """ 

160 create a shortcut on your desktop 

161 

162 @param folder notebook dir 

163 @return filename 

164 """ 

165 file = os.path.join( 

166 os.path.split( 

167 sys.executable)[0], 

168 "Scripts", 

169 "jupyter-notebook") 

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

171 ver = suffix() 

172 return add_shortcut_to_desktop( 

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

174 

175 

176if __name__ == "__main__": 

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