Skip to content
Snippets Groups Projects
Commit 8705b899 authored by Steffen Vogel's avatar Steffen Vogel :santa_tone2:
Browse files

final fixes for the submission service

parent 1e28bbd6
No related branches found
No related tags found
1 merge request!5WIP: Add new submission class which will communicate with the RWTH JupyterHub service
%% Cell type:markdown id: tags:
# Example notebooks for using the submission service
%% Cell type:code id: tags:
``` python
from rwth_nb.misc.submission import Submission
from random import randint
from pprint import pprint
sub = Submission('test_realm')
```
%% Cell type:code id: tags:
``` python
print('\nCreate some random submissions...')
for i in range(5):
sub.submit({
'random_integer': randint(1, 100)
})
```
%% Output
%% Cell type:markdown id: tags:
Create some random submissions...
## Admin actions
For performing admin actions the user must own the role manager-{profile_slug} or submission-{realm}
%% Cell type:code id: tags:
``` python
# Admin
print('\nAll submissions:')
pprint(sub.get_all())
print('\nDelete all submissions')
sub.delete_all()
print('\nAll submissions:')
pprint(sub.get_all())
```
%% Output
%% Cell type:markdown id: tags:
All submissions:
[{'data': {'random_integer': 33},
'id': 73,
'realm': 'test_realm',
'submitted': '2020-10-06T15:26:33.077998',
'submitter': 'vzi3jsam'},
{'data': {'random_integer': 70},
'id': 72,
'realm': 'test_realm',
'submitted': '2020-10-06T15:26:33.067712',
'submitter': 'vzi3jsam'},
{'data': {'random_integer': 81},
'id': 71,
'realm': 'test_realm',
'submitted': '2020-10-06T15:26:33.057721',
'submitter': 'vzi3jsam'},
{'data': {'random_integer': 97},
'id': 70,
'realm': 'test_realm',
'submitted': '2020-10-06T15:26:33.047435',
'submitter': 'vzi3jsam'},
{'data': {'random_integer': 20},
'id': 69,
'realm': 'test_realm',
'submitted': '2020-10-06T15:26:33.034729',
'submitter': 'vzi3jsam'}]
Delete all submissions
All submissions:
[]
## User actions
%% Cell type:code id: tags:
``` python
if sub.is_submitted():
print('\nEvaluation already submitted')
else:
print('\nNothing submitted yet!')
sub.submit({'test': [1, 2, 3]})
if sub.is_submitted():
print('\nNow it is submitted!:')
pprint(sub.get())
print('\nMy submissions:')
mysubs = sub.get()
print(mysubs)
ident = mysubs[0]['id']
print(f'\nMy submission ID: {ident}')
print(f'\nDeleting my submission with ID: {ident}')
sub.delete(ident=ident)
print('\nMy submissions:')
mysubs = sub.get()
print(mysubs)
```
%% Output
%% Cell type:markdown id: tags:
## Submitting whole notebooks
The following cell submits the current notebook contents
%% Cell type:code id: tags:
``` python
sub.submit_notebook()
```
%% Cell type:markdown id: tags:
Nothing submitted yet!
Now it is submitted!:
[{'data': {'test': [1, 2, 3]},
'id': 74,
'realm': 'test_realm',
'submitted': '2020-10-06T15:28:36.623802',
'submitter': 'vzi3jsam'}]
My submissions:
[{'id': 74, 'submitted': '2020-10-06T15:28:36.623802', 'submitter': 'vzi3jsam', 'realm': 'test_realm', 'data': {'test': [1, 2, 3]}}]
My submission ID: 74
Deleting my submission with ID: 74
My submissions:
[]
The following cell fetches all submitted notebooks in the current realm by the current user and stores them in the current directory
%% Cell type:code id: tags:
``` python
sub.fetch_notebook(subdir='fetched_notebooks', prefix='a1')
```
......
import json
import os
import requests
from IPython.lib import kernel
from notebook import notebookapp
def get_contents(token=None, api_url=None):
if token is None:
token = os.environ['JPY_API_TOKEN']
def get_contents():
servers = list(notebookapp.list_running_servers())
if api_url is None:
prefix = os.environ['JUPYTERHUB_SERVICE_PREFIX'].strip('/')
api_url = f'http://localhost:8888/{prefix}/api'
token = servers[0]['token']
api_url = servers[0]['url'] + 'api'
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
......@@ -21,11 +19,10 @@ def get_contents(token=None, api_url=None):
r = requests.request('GET', f'{api_url}/sessions', headers=headers)
sessions = json.loads(r.text)
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
path = sess['notebook']['path']
for session in r.json():
if session['kernel']['id'] == kernel_id:
path = session['notebook']['path']
r = requests.request('GET', f'{api_url}/contents/{path}', headers=headers)
return json.loads(r.text)
\ No newline at end of file
return r.json()
\ No newline at end of file
......@@ -9,11 +9,19 @@ import rwth_nb.misc.notebook as notebook
class Submission:
"""Submit and retrieve submissions to RWTH JupyterHub Service"""
token = os.environ['JUPYTERHUB_API_TOKEN']
service_url = 'http://proxy-public.jhub/services/submission'
def __init__(self, realm):
self.realm = realm
self.profile = os.environ.get('JUPYTERHUB_PROFILE')
self.token = os.environ.get('JUPYTERHUB_API_TOKEN')
self.service_url = 'http://proxy-public.jhub/services/submission'
if self.token is None or self.profile is None:
raise Exception('Submissions only work inside the RWTHjupyter cluster!')
def url(self, user='me', ident=None):
url = f'{self.service_url}/{self.realm}'
url = f'{self.service_url}/{self.profile}/{self.realm}'
if user is not None:
url += f'/{user}'
......@@ -38,9 +46,6 @@ class Submission:
return payload
def __init__(self, realm):
self.realm = realm
def submit(self, data):
"""Submit some arbitrary JSON payload"""
......@@ -80,6 +85,37 @@ class Submission:
return j['submissions']
def fetch_notebook(self, user='me', ident=None, limit=None, subdir=None, prefix=None):
subs = self.get(user, ident, limit)
for sub in subs:
nb = sub.pop('data')
realm = sub['realm']
submitter = sub['submitter']
id = sub['id']
name, ext = os.path.splitext(nb['name'])
# Add submission details as metadata to the notebook
nb['content']['metadata']['submission'] = sub
fn = f'{name}_{realm}_{submitter}-{id}{ext}'
if prefix:
fn = f'{prefix}_{fn}'
if subdir:
os.makedirs(subdir)
fn = f'{subdir}/{fn}'
with open(fn, 'w+') as nb_file:
json.dump(nb['content'], nb_file)
def fetch_all_notebooks(self):
"""Get all submissions in a realm"""
return self.fetch_notebook(user=None)
def delete(self, user='me', ident=None):
"""Delete the users submission"""
......@@ -87,7 +123,6 @@ class Submission:
headers=self.headers())
r.raise_for_status()
def get_all(self):
"""Get all submissions in a realm"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment