forked from ucbds-infra/otter-grader
/
notebook.py
458 lines (366 loc) · 17 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
IPython notebook API for Otter Check
"""
import inspect
import requests
import json
import os
import re
import zipfile
import pickle
import time
import datetime as dt
from getpass import getpass
from glob import glob
from urllib.parse import urljoin
from IPython import get_ipython
from IPython.display import display, HTML, Javascript
from .logs import LogEntry, EventType, Log
from ..execute import check
from ..export import export_notebook
from ..plugins import PluginCollection
# from .utils import wait_for_save
_API_KEY = None
_OTTER_STATE_FILENAME = ".OTTER_STATE"
_OTTER_LOG_FILENAME = ".OTTER_LOG"
_SHELVE = False
class TestsDisplay:
"""
Class for stitching together TestCollectionResults objects and displaying them in HTML and plaintext
Args:
results (``list`` of ``tuple(str, TestCollectionResults)``): the test names and results for each test
to be displayed
"""
def __init__(self, results):
self.test_names = [r[0] for r in results]
self.results = [r[1] for r in results]
def __repr__(self):
ret = ""
for name, result in zip(self.test_names, self.results):
ret += f"{repr(result)}\n\n"
return ret
def _repr_html_(self):
ret = ""
for name, result in zip(self.test_names, self.results):
ret += f"{result._repr_html_()}\n\n"
return ret
class Notebook:
"""
Notebook class for in-notebook autograding
Args:
nb_path(``str``, optional): path to the notebook being run
test_dir (``str``, optional): path to tests directory
"""
def __init__(self, nb_path=None, test_dir="./tests"):
try:
global _API_KEY, _SHELVE
# assert os.path.isdir(test_dir), "{} is not a directory".format(test_dir)
self._path = test_dir
self._service_enabled = False
self._notebook = nb_path
# assume using otter service if there is a .otter file
otter_configs = glob("*.otter")
if otter_configs:
# check that there is only 1 config
assert len(otter_configs) == 1, "More than 1 otter config file found"
# load in config file
with open(otter_configs[0]) as f:
self._config = json.load(f)
_SHELVE = self._config.get("save_environment", False)
self._service_enabled = "endpoint" in self._config
self._ignore_modules = self._config.get("ignore_modules", [])
self._vars_to_store = self._config.get("variables", None)
self._notebook = self._config["notebook"]
if self._service_enabled:
# check that config file has required info
assert all([key in self._config for key in ["endpoint", "assignment_id", "class_id"]]), "Otter config file missing required information"
if "auth" not in self._config:
self._config["auth"] = "google"
self._google_auth_url = urljoin(self._config["endpoint"], "auth/google")
self._default_auth_url = urljoin(self._config["endpoint"], "auth")
self._submit_url = urljoin(self._config["endpoint"], "submit")
self._auth()
except Exception as e:
self._log_event(EventType.INIT, success=False, error=e)
raise e
else:
self._log_event(EventType.INIT)
# TODO: cut out personal auth?
def _auth(self):
"""
Asks student to authenticate with an Otter Service instance if Otter Service is configured
for this notebook
Raises:
``AssertionError``: if Otter Service is not enabled or an invalid auth provider is indicated
"""
try:
global _API_KEY
assert self._service_enabled, 'notebook not configured for otter service'
assert self._config["auth"] in ["google", "default"], "invalid auth provider"
if _API_KEY is not None:
self._api_key = _API_KEY
return
# have users authenticate with OAuth
if self._config["auth"] == "google":
# send them to google login page
display(HTML(f"""
<p>Please <a href="{self._google_auth_url}" target="_blank">log in</a> to Otter Service
and enter your API key below.</p>
"""))
self._api_key = input()
# else have them auth with default auth
else:
print("Please enter a username and password.")
username = input("Username: ")
password = getpass("Password: ")
# in-notebook auth
response = requests.get(url=self._default_auth_url, params={"username":username, "password":password})
self._api_key = response.content.decode("utf-8")
# print("Your API Key is {}\n".format())
# print("Paste this in and hit enter")
# self._api_key = input()
# store API key so we don't re-auth every time
_API_KEY = self._api_key
except Exception as e:
self._log_event(EventType.AUTH, success=False, error=e)
raise e
else:
self._log_event(EventType.AUTH)
def _log_event(self, event_type, results=[], question=None, success=True, error=None, shelve_env={}):
"""
Logs an event
Args:
event_type (``otter.logs.EventType``): the type of event
results (``list`` of ``otter.test_files.abstract_test.TestCollectionResults``, optional):
the results of any checks recorded by the entry
question (``str``, optional): the question name for this check
success (``bool``, optional): whether the operation was successful
error (``Exception``, optional): the exception thrown by the operation, if applicable
"""
entry = LogEntry(
event_type,
results=results,
question=question,
success=success,
error=error
)
if _SHELVE and event_type == EventType.CHECK:
entry.shelve(
shelve_env,
delete=True,
filename=_OTTER_LOG_FILENAME,
ignore_modules=self._ignore_modules,
variables=self._vars_to_store
)
entry.flush_to_file(_OTTER_LOG_FILENAME)
def _resolve_nb_path(self, nb_path):
"""
Attempts to resolve the path to the notebook being run. If ``nb_path`` is ``None``, ``self._notebook``
is checked, then the working directory is searched for `.ipynb` files. If none are found, or
more than one is found, a ``ValueError`` is raised.
Args:
nb_path (``Optional[str]``): path to the notebook
Returns:
``str``: resolved notebook path
Raises:
``ValueError``: if no notebooks or too many notebooks are found.
"""
if nb_path is None and self._notebook is not None:
nb_path = self._notebook
elif nb_path is None and glob("*.ipynb"):
notebooks = glob("*.ipynb")
assert len(notebooks) == 1, "nb_path not specified and > 1 notebook in working directory"
nb_path = notebooks[0]
elif nb_path is None:
raise ValueError("nb_path is None and no otter-service config is available")
return nb_path
def check(self, question, global_env=None):
"""
Runs tests for a specific question against a global environment. If no global environment
is provided, the test is run against the calling frame's environment.
Args:
question (``str``): name of question being graded
global_env (``dict``, optional): global environment resulting from execution of a single
notebook
Returns:
``otter.test_files.abstract_test.TestCollectionResults``: the grade for the question
"""
try:
test_path = os.path.join(self._path, question + ".py")
# ensure that desired test exists
assert os.path.isfile(test_path), "Test {} does not exist".format(question)
# pass the correct global environment
if global_env is None:
global_env = inspect.currentframe().f_back.f_globals
# run the check
result = check(test_path, global_env)
except Exception as e:
self._log_event(EventType.CHECK, question=question, success=False, error=e, shelve_env=global_env)
raise e
else:
self._log_event(EventType.CHECK, [result], question=question, shelve_env=global_env)
return result
def run_plugin(self, plugin_name, *args, nb_path=None, **kwargs):
"""
Runs the plugin ``plugin_name`` with the specified arguments. Use ``nb_path`` if the path
to the notebook is not configured.
Args:
plugin_name (``str``): importable name of an Otter plugin that implements the
``from_notebook`` hook
*args: arguments to be passed to the plugin
nb_path (``str``, optional): path to the notebook
**kwargs: keyword arguments to be passed to the plugin
"""
nb_path = self._resolve_nb_path(nb_path)
pc = PluginCollection([plugin_name], nb_path, {})
pc.run("from_notebook", *args, **kwargs)
# @staticmethod
def to_pdf(self, nb_path=None, filtering=True, pagebreaks=True, display_link=True):
"""
Exports a notebook to a PDF using Otter Export
Args:
nb_path (``str``, optional): path to the notebook we want to export; will attempt to infer
if not provided
filtering (``bool``, optional): set true if only exporting a subset of notebook cells to PDF
pagebreaks (``bool``, optional): if true, pagebreaks are included between questions
display_link (``bool``, optional): whether or not to display a download link
"""
# self._save_notebook()
try:
nb_path = self._resolve_nb_path(nb_path)
# convert(nb_path, filtering=filtering, filter_type=filter_type)
export_notebook(nb_path, filtering=filtering, pagebreaks=pagebreaks)
if display_link:
# create and display output HTML
out_html = """
<p>Your file has been exported. Download it by right-clicking
<a href="{}" target="_blank">here</a> and selecting <strong>Save Link As</strong>.
""".format(nb_path[:-5] + "pdf")
display(HTML(out_html))
except Exception as e:
self._log_event(EventType.TO_PDF, success=False, error=e)
raise e
else:
self._log_event(EventType.TO_PDF)
def export(self, nb_path=None, export_path=None, pdf=True, filtering=True, pagebreaks=True, files=[], display_link=True):
"""
Exports a submission to a zip file. Creates a submission zipfile from a notebook at ``nb_path``,
optionally including a PDF export of the notebook and any files in ``files``.
Args:
nb_path (``str``, optional): path to the notebook we want to export; will attempt to infer
if not provided
export_path (``str``, optional): path at which to write zipfile; defaults to notebook's
name + ``.zip``
pdf (``bool``, optional): whether a PDF should be included
filtering (``bool``, optional): whether the PDF should be filtered; ignored if ``pdf`` is
``False``
pagebreaks (``bool``, optional): whether pagebreaks should be included between questions
in the PDF
files (``list`` of ``str``, optional): paths to other files to include in the zip file
display_link (``bool``, optional): whether or not to display a download link
"""
self._log_event(EventType.BEGIN_EXPORT)
# self._save_notebook()
try:
nb_path = self._resolve_nb_path(nb_path)
try:
with open(nb_path) as f:
assert len(f.read().strip()) > 0, \
f"Notebook {nb_path} is empty. Please save and checkpoint your notebook and rerun this cell."
except UnicodeDecodeError:
with open(nb_path, "r", encoding="utf-8") as f:
assert len(f.read().strip()) > 0, \
f"Notebook {nb_path} is empty. Please save and checkpoint your notebook and rerun this cell."
if export_path is None:
zip_path = ".".join(nb_path.split(".")[:-1]) + ".zip"
else:
zip_path = export_path
zf = zipfile.ZipFile(zip_path, mode="w")
zf.write(nb_path)
if pdf:
pdf_path = ".".join(nb_path.split(".")[:-1]) + ".pdf"
# convert(nb_path, filtering=filtering, filter_type=filter_type)
export_notebook(nb_path, filtering=filtering, pagebreaks=pagebreaks)
zf.write(pdf_path)
if os.path.isfile(_OTTER_LOG_FILENAME):
zf.write(_OTTER_LOG_FILENAME)
if glob("*.otter"):
assert len(glob("*.otter")) == 1, "Too many .otter files (max 1 allowed)"
zf.write(glob("*.otter")[0])
for file in files:
zf.write(file)
zf.close()
if display_link:
# create and display output HTML
out_html = """
<p>Your submission has been exported. Click <a href="{}" target="_blank">here</a>
to download the zip file.</p>
""".format(zip_path)
display(HTML(out_html))
except Exception as e:
self._log_event(EventType.END_EXPORT, success=False, error=e)
raise e
else:
self._log_event(EventType.END_EXPORT)
def check_all(self):
"""
Runs all tests on this notebook. Tests are run against the current global environment, so any
tests with variable name collisions will fail.
"""
# TODO: this should use functions in execute.py to run tests in-sequence so that variable
# name collisions are accounted for
self._log_event(EventType.BEGIN_CHECK_ALL)
try:
tests = glob(os.path.join(self._path, "*.py"))
global_env = inspect.currentframe().f_back.f_globals
results = []
if not _SHELVE:
for file in sorted(tests):
if "__init__.py" not in file:
test_name = os.path.split(file)[1][:-3]
result = self.check(test_name, global_env)
results.append((test_name, result))
else:
log = Log.from_file(_OTTER_LOG_FILENAME, ascending=False)
for file in sorted(tests):
if "__init__.py" not in file:
test_name = os.path.splitext(os.path.split(file)[1])[0]
entry = log.get_question_entry(test_name)
env = entry.unshelve()
global_env.update(env)
del locals()["env"]
result = self.check(test_name, global_env)
results.append((test_name, result))
except Exception as e:
self._log_event(EventType.END_CHECK_ALL, success=False, error=e)
raise e
else:
self._log_event(EventType.END_CHECK_ALL)
return TestsDisplay(results)
def submit(self):
"""
Submits this notebook to an Otter Service instance if Otter Service is configured
Raises:
``AssertionError``: if this notebook is not configured for Otter Service
"""
assert self._service_enabled, 'notebook not configured for otter service'
try:
if not hasattr(self, '_api_key'):
self._auth()
notebook_path = os.path.join(os.getcwd(), self._notebook)
assert os.path.exists(notebook_path) and os.path.isfile(notebook_path), \
"Could not find notebook: {}".format(self._notebook)
with open(notebook_path) as f:
notebook_data = json.load(f)
notebook_data["metadata"]["assignment_id"] = self._config["assignment_id"]
notebook_data["metadata"]["class_id"] = self._config["class_id"]
print("Submitting notebook to server...")
response = requests.post(self._submit_url, json.dumps({
"api_key": self._api_key,
"nb": notebook_data,
}))
except Exception as e:
self._log_event(EventType.SUBMIT, success=False, error=e)
raise e
else:
self._log_event(EventType.SUBMIT)