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 Functions to prepare a setup on Windows, R functions 

4""" 

5 

6import json 

7import os 

8import sys 

9 

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

11 from codecs import open 

12 from StringIO import StringIO 

13else: 

14 from io import StringIO 

15 

16 

17r_kernel = { 

18 "display_name": "R%s", 

19 "language": "R", 

20 "argv": [ 

21 "%R_HOME%\\bin\\x64\\R.exe", 

22 "--quiet", 

23 "-e", 

24 "IRkernel::main()", 

25 "--args", 

26 "{connection_file}" 

27 ] 

28} 

29 

30python_kernel = { 

31 "language": "python", 

32 "display_name": "Python 3%s", 

33 "argv": [ 

34 "%PYTHON_WINHOME%\\python.exe", 

35 "-m", 

36 "IPython.kernel", 

37 "-f", 

38 "{connection_file}" 

39 ] 

40} 

41 

42julia_kernel = { 

43 "display_name": "Julia%s", 

44 "language": "julia", 

45 "argv": [ 

46 "%JULIA_HOME%\\bin\\julia.exe", 

47 "-i", 

48 "-F", 

49 "%JULIA_PKGDIR%\\v0.3\\IJulia\\src\\kernel.jl", 

50 "{connection_file}" 

51 ], 

52 "codemirror_mode": "julia" 

53} 

54 

55 

56def add_kernel_jupyter(kernel, path, tools_path, python_path, suffix="WP"): 

57 """ 

58 add a kernel to jupyter 

59 

60 @param kernel dictionary (see global variable in this module) 

61 @param path where to add it 

62 @param tools_path tools paths 

63 @param python_path python path 

64 @param suffix added in bracket at the end of the display name 

65 @return created file 

66 """ 

67 kernel = kernel.copy() 

68 suffix = " (%s)" % suffix 

69 kernel["display_name"] = kernel["display_name"] % suffix 

70 name = kernel["display_name"] 

71 name = name.replace(" ", "_").replace("(", "").replace(")", "") 

72 fold = os.path.join(path, name) 

73 if not os.path.exists(fold): 

74 os.mkdir(fold) 

75 st = StringIO() 

76 

77 # replacements 

78 argv = kernel["argv"] 

79 for i in range(len(argv)): 

80 if "%" in argv[i]: 

81 argv[i] = argv[i].replace("%PYTHON_WINHOME%", python_path) \ 

82 .replace("%R_HOME%", os.path.join(tools_path, "R")) \ 

83 .replace("%JULIA_HOME%", os.path.join(tools_path, "Julia")) \ 

84 .replace("%JULIA_PKGDIR%", os.path.join(tools_path, "Julia", "pkg")) 

85 

86 # dump 

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

88 json.dump(kernel, st, encoding="utf8") 

89 else: 

90 json.dump(kernel, st) 

91 ker = os.path.join(fold, "kernel.json") 

92 with open(ker, "w", encoding="utf8") as f: 

93 f.write(st.getvalue()) 

94 return ker 

95 

96 

97def install_kernels(tools_path, python_path, suffix="WP"): 

98 """ 

99 install available kernels on Windows 

100 

101 @param tools_path tools paths 

102 @param python_path python path 

103 @param suffix suffix 

104 @return list of creating files 

105 """ 

106 dest = os.environ["ProgramData"] 

107 jupyter = os.path.join(dest, "jupyter", "kernels") 

108 if not os.path.exists(jupyter): 

109 os.makedirs(jupyter) 

110 res = [] 

111 if os.path.exists(python_path): 

112 res.append( 

113 add_kernel_jupyter(python_kernel, jupyter, tools_path, python_path, suffix)) 

114 if os.path.exists(os.path.join(tools_path, "R")): 

115 res.append( 

116 add_kernel_jupyter(r_kernel, jupyter, tools_path, python_path, suffix)) 

117 if os.path.exists(os.path.join(tools_path, "Julia")): 

118 res.append( 

119 add_kernel_jupyter(julia_kernel, jupyter, tools_path, python_path, suffix)) 

120 return res