/
notebook.py
350 lines (295 loc) · 10 KB
/
notebook.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from abc import abstractmethod
import os, sys
from pathlib import Path
from urllib.error import URLError
import tempfile
import re
import shutil
from snakemake.exceptions import WorkflowError
from snakemake.shell import shell
from snakemake.script import get_source, ScriptBase, PythonScript, RScript
from snakemake.logging import logger
from snakemake.common import is_local_file
from snakemake.common import ON_WINDOWS
from snakemake.sourcecache import SourceCache, infer_source_file
from snakemake.utils import format
KERNEL_STARTED_RE = re.compile(r"Kernel started: (?P<kernel_id>\S+)")
KERNEL_SHUTDOWN_RE = re.compile(r"Kernel shutdown: (?P<kernel_id>\S+)")
class EditMode:
def __init__(self, server_addr=None, draft_only=False):
if server_addr is not None:
self.ip, self.port = server_addr.split(":")
self.draft_only = draft_only
def get_cell_sources(source):
import nbformat
nb = nbformat.reads(source, as_version=nbformat.NO_CONVERT)
return [cell["source"] for cell in nb["cells"]]
class JupyterNotebook(ScriptBase):
editable = True
def draft(self):
import nbformat
preamble = self.get_preamble()
nb = nbformat.v4.new_notebook()
self.insert_preamble_cell(preamble, nb)
nb["cells"].append(nbformat.v4.new_code_cell("# start coding here"))
nb["metadata"] = {"language_info": {"name": self.get_language_name()}}
os.makedirs(os.path.dirname(self.local_path), exist_ok=True)
with open(self.local_path, "wb") as out:
out.write(nbformat.writes(nb).encode())
def draft_and_edit(self, listen):
self.draft()
self.source = open(self.local_path).read()
self.evaluate(edit=listen)
def write_script(self, preamble, fd):
import nbformat
nb = nbformat.reads(self.source, as_version=nbformat.NO_CONVERT)
self.remove_preamble_cell(nb)
self.insert_preamble_cell(preamble, nb)
fd.write(nbformat.writes(nb).encode())
def execute_script(self, fname, edit=None):
import nbformat
fname_out = self.log.get("notebook", None)
if fname_out is None or edit:
output_parameter = ""
else:
fname_out = os.path.join(os.getcwd(), fname_out)
output_parameter = "--output {fname_out:q}"
with tempfile.TemporaryDirectory() as tmp:
if edit is not None:
assert not edit.draft_only
logger.info("Opening notebook for editing.")
cmd = (
"jupyter notebook --browser ':' --no-browser --log-level ERROR --ip {edit.ip} --port {edit.port} "
"--NotebookApp.quit_button=True {{fname:q}}".format(edit=edit)
)
else:
cmd = (
"jupyter-nbconvert --log-level ERROR --execute {output_parameter} "
"--to notebook --ExecutePreprocessor.timeout=-1 {{fname:q}}".format(
output_parameter=output_parameter,
)
)
if ON_WINDOWS:
fname = fname.replace("\\", "/")
fname_out = fname_out.replace("\\", "/") if fname_out else fname_out
self._execute_cmd(
cmd,
fname_out=fname_out,
fname=fname,
additional_envvars={"IPYTHONDIR": tmp},
)
if edit:
logger.info("Saving modified notebook.")
nb = nbformat.read(fname, as_version=4)
self.remove_preamble_cell(nb)
# clean up all outputs
for cell in nb["cells"]:
cell["outputs"] = []
nbformat.write(nb, self.local_path)
def insert_preamble_cell(self, preamble, notebook):
import nbformat
preamble_cell = nbformat.v4.new_code_cell(preamble)
preamble_cell["metadata"]["tags"] = ["snakemake-job-properties"]
notebook["cells"].insert(0, preamble_cell)
def remove_preamble_cell(self, notebook):
preambles = [
i
for i, cell in enumerate(notebook["cells"])
if "snakemake-job-properties" in cell["metadata"].get("tags", [])
]
if len(preambles) > 1:
raise WorkflowError(
"More than one snakemake preamble cell found in notebook. "
"Please clean up the notebook first, by removing all or all but one of them."
)
elif len(preambles) == 1:
preamble = preambles[0]
# remove old preamble
del notebook["cells"][preamble]
@abstractmethod
def get_language_name(self):
...
@abstractmethod
def get_interpreter_exec(self):
...
class PythonJupyterNotebook(JupyterNotebook):
def get_preamble(self):
preamble_addendum = "import os; os.chdir(r'{cwd}');".format(cwd=os.getcwd())
return PythonScript.generate_preamble(
self.path,
self.source,
self.basedir,
self.input,
self.output,
self.params,
self.wildcards,
self.threads,
self.resources,
self.log,
self.config,
self.rulename,
self.conda_env,
self.container_img,
self.singularity_args,
self.env_modules,
self.bench_record,
self.jobid,
self.bench_iteration,
self.cleanup_scripts,
self.shadow_dir,
self.is_local,
preamble_addendum=preamble_addendum,
)
def get_language_name(self):
return "python"
def get_interpreter_exec(self):
return "python"
class RJupyterNotebook(JupyterNotebook):
def get_preamble(self):
preamble_addendum = "setwd('{cwd}');".format(cwd=os.getcwd())
return RScript.generate_preamble(
self.path,
self.source,
self.basedir,
self.input,
self.output,
self.params,
self.wildcards,
self.threads,
self.resources,
self.log,
self.config,
self.rulename,
self.conda_env,
self.container_img,
self.singularity_args,
self.env_modules,
self.bench_record,
self.jobid,
self.bench_iteration,
self.cleanup_scripts,
self.shadow_dir,
preamble_addendum=preamble_addendum,
)
def get_language_name(self):
return "r"
def get_interpreter_exec(self):
return "RScript"
def get_exec_class(language):
exec_class = {
"jupyter_python": PythonJupyterNotebook,
"jupyter_r": RJupyterNotebook,
}.get(language, None)
if exec_class is None:
raise ValueError("Unsupported notebook: Expecting Jupyter Notebook (.ipynb).")
return exec_class
def notebook(
path,
basedir,
input,
output,
params,
wildcards,
threads,
resources,
log,
config,
rulename,
conda_env,
conda_base_path,
container_img,
singularity_args,
env_modules,
bench_record,
jobid,
bench_iteration,
cleanup_scripts,
shadow_dir,
edit,
runtime_sourcecache_path,
):
"""
Load a script from the given basedir + path and execute it.
"""
draft = False
path = format(path, wildcards=wildcards, params=params)
if edit is not None:
if is_local_file(path):
if not os.path.isabs(path):
local_path = os.path.join(basedir, path)
else:
local_path = path
if not os.path.exists(local_path):
# draft the notebook, it does not exist yet
language = None
draft = True
path = "file://{}".format(os.path.abspath(local_path))
if path.endswith(".py.ipynb"):
language = "jupyter_python"
elif path.endswith(".r.ipynb"):
language = "jupyter_r"
else:
raise WorkflowError(
"Notebook to edit has to end on .py.ipynb or .r.ipynb in order "
"to decide which programming language shall be used."
)
else:
raise WorkflowError(
"Notebook {} is not local, but edit mode is only allowed for "
"local notebooks.".format(path)
)
if not draft:
path, source, language, is_local = get_source(
path, SourceCache(runtime_sourcecache_path), basedir, wildcards, params
)
else:
source = None
is_local = True
path = infer_source_file(path)
exec_class = get_exec_class(language)
executor = exec_class(
path,
source,
basedir,
input,
output,
params,
wildcards,
threads,
resources,
log,
config,
rulename,
conda_env,
conda_base_path,
container_img,
singularity_args,
env_modules,
bench_record,
jobid,
bench_iteration,
cleanup_scripts,
shadow_dir,
is_local,
)
if edit is None:
executor.evaluate(edit=edit)
elif edit.draft_only:
executor.draft()
msg = "Generated skeleton notebook:\n{} ".format(path)
if conda_env and not container_img:
msg += (
"\n\nEditing with VSCode:\nOpen notebook, run command 'Select notebook kernel' (Ctrl+Shift+P or Cmd+Shift+P), and choose:"
"\n{}\n".format(
str(Path(conda_env) / "bin" / executor.get_interpreter_exec())
)
)
msg += (
"\nEditing with Jupyter CLI:"
"\nconda activate {}\njupyter notebook {}\n".format(conda_env, path)
)
logger.info(msg)
elif draft:
executor.draft_and_edit(listen=edit)
else:
executor.evaluate(edit=edit)