forked from UTokyo-IPP/utpython_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preamble.py
53 lines (46 loc) · 2.15 KB
/
preamble.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
# NOTE: The authoritative original source of this snippet is in
# https://github.com/UTokyo-IPP/utpython/preamble.py.
import getpass
#@title Submission snippet
#@markdown Please [login to server](https://utpython-autograder-v6gvzmyosa-an.a.run.app/login) to get a JWT token, run this cell and paste the JWT token.
SERVER_URL = 'https://utpython-autograder-v6gvzmyosa-an.a.run.app'
JWT_TOKEN = getpass.getpass('Enter JWT token:')
import json
import requests
from google.colab import _message as google_message
from IPython.core import display
def Submit(exercise_id=None):
if JWT_TOKEN == "":
display.display(display.HTML("Please get JWT_TOKEN by visiting " +
"<a href='" + SERVER_URL + "/login'>Login page</a>"))
raise Exception("Please set JWT_TOKEN")
notebook = google_message.blocking_request(
"get_ipynb", request="", timeout_sec=120)["ipynb"]
ids = []
for cell in notebook['cells']:
if 'metadata' not in cell:
continue
m = cell['metadata']
if m and 'exercise_id' in m:
cell_id = m['exercise_id']
if cell_id:
ids.append(cell_id)
params = {}
if exercise_id:
if exercise_id not in ids:
raise Exception('Not valid exercise ID: ' + exercise_id + ". Valid ids: " + ", ".join(ids))
params["exercise_id"] = exercise_id
data = json.dumps(notebook)
r = requests.post(SERVER_URL + "/upload", files={"notebook": data},
headers={"Authorization": "Bearer " + JWT_TOKEN},
params=params)
if r.status_code == 401:
display.display(display.HTML("Not authorized: is your JWT_TOKEN correct? " +
"Please get JWT_TOKEN by visiting " +
"<a target='_blank' href='" + SERVER_URL + "/login'>Login page</a>" +
"in a new browser tab."))
display.display(display.HTML(r.content.decode('utf-8')))
if JWT_TOKEN == "":
display.display(display.HTML("Please get JWT_TOKEN by visiting " +
"<a href='" + SERVER_URL + "/login'>Login page</a>"))
raise Exception("Please set JWT_TOKEN")