-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathbuild.jl
112 lines (100 loc) · 3.63 KB
/
build.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using JSON
const BEGIN_MARKER = "###JULIA-WEBIO-CONFIG-BEGIN"
const END_MARKER = "###JULIA-WEBIO-CONFIG-END"
function install_ijulia_config()
config_file = joinpath(homedir(), ".jupyter", "jupyter_notebook_config.py")
if isfile(config_file)
config_str = String(read(config_file))
else
mkpath(dirname(config_file))
config_str = ""
end
# remove previous config
config_str = replace(config_str, Regex("\n?" * BEGIN_MARKER * ".*" * END_MARKER * "\n?", "s") => "")
config_str *= """
$BEGIN_MARKER
import sys, os
if os.path.isfile($(repr(joinpath(dirname(@__FILE__), "jlstaticserve.py")))):
sys.path.append($(repr(dirname(@__FILE__))))
c = get_config()
c.NotebookApp.nbserver_extensions = {
"jlstaticserve": True
}
else:
print("WebIO config in ~/.jupyter/jupyter_notebook_config.py but WebIO plugin not found")
$END_MARKER
"""
write(config_file, config_str)
config_file_json = joinpath(homedir(), ".jupyter", "jupyter_notebook_config.json")
if isfile(config_file_json)
dict = try
JSON.parse(read(config_file_json, String))
catch err
println(stderr, "Error parsing Jupyter config file $config_file_json - fix it and build again or delete it to enable WebIO")
@goto jsondone
end
app = Base.@get! dict "NotebookApp" Dict()
nbext = Base.@get! app "nbserver_extensions" Dict()
nbext["jlstaticserve"] = true
open(config_file_json, "w") do io
prettyio = JSON.Writer.PrettyContext(io, 4)
JSON.print(prettyio, dict)
end
end
@label jsondone
end
function get_jupyter_datadir()
try
return readline(open(`jupyter --data-dir`))
catch (e)
# Is there a way to use Conda.jl if it's installed?
@warn "Didn't detect Jupyter."
end
# Try guessing based on OS
# https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html
if Sys.iswindows()
# Is this right?
return joinpath("%APPDATA%", "jupyter")
elseif Sys.isapple()
return joinpath(homedir(), "Library", "Jupyter")
else
# Maybe need to check XDG_DATA_HOME environment variable?
return joinpath(homedir(), ".local", "share", "jupyter")
end
end
"""
Install the Jupyter WebIO notebook extension.
"""
function install_webio_nbextension()
extension_dir = joinpath(get_jupyter_datadir(), "nbextensions")
mkpath(extension_dir)
# I think the config dir is always ~/.jupyter, even on Windows.
config_dir = joinpath(homedir(), ".jupyter", "nbconfig")
mkpath(config_dir)
config_file_json = joinpath(config_dir, "notebook.json")
# Copy the nbextension files.
@info "Copying WebIO nbextension files to $(extension_dir)."
cp(
joinpath(@__DIR__, "../packages/jupyter-notebook-provider/dist"),
joinpath(extension_dir, "webio"),
; force=true
)
# Enable the notebook extension.
config_data = Dict()
if isfile(config_file_json)
config_data = try
JSON.parse(read(config_file_json, String))
catch err
println(stderr, "Error parsing Jupyter config file $config_file_json - fix it and build again or delete it to enable WebIO")
return
end
end
config_data["load_extensions"] = get(config_data, "load_extensions", Dict())
config_data["load_extensions"]["webio/main"] = true
open(config_file_json, "w") do io
prettyio = JSON.Writer.PrettyContext(io, 4)
JSON.print(prettyio, config_data)
end
end
install_ijulia_config()
install_webio_nbextension()