forked from ucbds-infra/otter-grader
/
utils.py
39 lines (30 loc) · 1016 Bytes
/
utils.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
"""
Utils for Otter Check
"""
import time
import hashlib
from IPython import get_ipython
from IPython.display import display, Javascript
def save_notebook(filename, timeout=10):
"""
Force-saves a Jupyter notebook by displaying JavaScript.
Args:
filename (``str``): path to notebook file being saved
timeout (``int`` or ``float``): number of seconds to wait for save before timing-out
Returns
``bool``: whether the notebook was saved successfully
"""
timeout = timeout * 10**9
if get_ipython() is not None:
f = open(filename, "rb")
md5 = hashlib.md5(f.read()).hexdigest()
start = time.time_ns()
display(Javascript("Jupyter.notebook.save_checkpoint();"))
curr = md5
while curr == md5 and time.time_ns() - start <= timeout:
time.sleep(1)
f.seek(0)
curr = hashlib.md5(f.read()).hexdigest()
f.close()
return curr != md5
return True