Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RWTH.nb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RWTHjupyter
RWTH.nb
Commits
8705b899
Commit
8705b899
authored
4 years ago
by
Steffen Vogel
Browse files
Options
Downloads
Patches
Plain Diff
final fixes for the submission service
parent
1e28bbd6
No related branches found
No related tags found
1 merge request
!5
WIP: Add new submission class which will communicate with the RWTH JupyterHub service
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/submission.ipynb
+67
-85
67 additions, 85 deletions
examples/submission.ipynb
rwth_nb/misc/notebook.py
+9
-12
9 additions, 12 deletions
rwth_nb/misc/notebook.py
rwth_nb/misc/submission.py
+42
-7
42 additions, 7 deletions
rwth_nb/misc/submission.py
with
118 additions
and
104 deletions
examples/submission.ipynb
+
67
−
85
View file @
8705b899
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Create some random submissions...\n"
"source": [
"# Example notebooks for using the submission service"
]
}
],
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from rwth_nb.misc.submission import Submission\n",
"\n",
"from random import randint\n",
"from pprint import pprint\n",
" \n",
"sub = Submission('test_realm')\n",
" \n",
"sub = Submission('test_realm')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('\\nCreate some random submissions...')\n",
"for i in range(5):\n",
" sub.submit({\n",
...
...
@@ -30,51 +35,20 @@
]
},
{
"cell_type": "code",
"execution_count": 2,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"All submissions:\n",
"[{'data': {'random_integer': 33},\n",
" 'id': 73,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:26:33.077998',\n",
" 'submitter': 'vzi3jsam'},\n",
" {'data': {'random_integer': 70},\n",
" 'id': 72,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:26:33.067712',\n",
" 'submitter': 'vzi3jsam'},\n",
" {'data': {'random_integer': 81},\n",
" 'id': 71,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:26:33.057721',\n",
" 'submitter': 'vzi3jsam'},\n",
" {'data': {'random_integer': 97},\n",
" 'id': 70,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:26:33.047435',\n",
" 'submitter': 'vzi3jsam'},\n",
" {'data': {'random_integer': 20},\n",
" 'id': 69,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:26:33.034729',\n",
" 'submitter': 'vzi3jsam'}]\n",
"\n",
"Delete all submissions\n",
"source": [
"## Admin actions\n",
"\n",
"All submissions:\n",
"[]\n"
"For performing admin actions the user must own the role manager-{profile_slug} or submission-{realm}"
]
}
],
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Admin\n",
"print('\\nAll submissions:')\n",
"pprint(sub.get_all())\n",
" \n",
...
...
@@ -86,36 +60,17 @@
]
},
{
"cell_type": "code",
"execution_count": 3,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Nothing submitted yet!\n",
"\n",
"Now it is submitted!:\n",
"[{'data': {'test': [1, 2, 3]},\n",
" 'id': 74,\n",
" 'realm': 'test_realm',\n",
" 'submitted': '2020-10-06T15:28:36.623802',\n",
" 'submitter': 'vzi3jsam'}]\n",
"\n",
"My submissions:\n",
"[{'id': 74, 'submitted': '2020-10-06T15:28:36.623802', 'submitter': 'vzi3jsam', 'realm': 'test_realm', 'data': {'test': [1, 2, 3]}}]\n",
"\n",
"My submission ID: 74\n",
"\n",
"Deleting my submission with ID: 74\n",
"\n",
"My submissions:\n",
"[]\n"
"source": [
"## User actions"
]
}
],
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if sub.is_submitted():\n",
" print('\\nEvaluation already submitted')\n",
...
...
@@ -142,12 +97,39 @@
"print(mysubs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submitting whole notebooks\n",
"\n",
"The following cell submits the current notebook contents"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sub.submit_notebook()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"sub.fetch_notebook(subdir='fetched_notebooks', prefix='a1')"
]
}
],
"metadata": {
...
...
@@ -166,7 +148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.
7
.6"
"version": "3.
8
.6"
}
},
"nbformat": 4,
...
...
%% 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
(
'
\n
Create 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
(
'
\n
All submissions:
'
)
pprint
(
sub
.
get_all
())
print
(
'
\n
Delete all submissions
'
)
sub
.
delete_all
()
print
(
'
\n
All 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
(
'
\n
Evaluation already submitted
'
)
else
:
print
(
'
\n
Nothing submitted yet!
'
)
sub
.
submit
({
'
test
'
:
[
1
,
2
,
3
]})
if
sub
.
is_submitted
():
print
(
'
\n
Now it is submitted!:
'
)
pprint
(
sub
.
get
())
print
(
'
\n
My submissions:
'
)
mysubs
=
sub
.
get
()
print
(
mysubs
)
ident
=
mysubs
[
0
][
'
id
'
]
print
(
f
'
\n
My submission ID:
{
ident
}
'
)
print
(
f
'
\n
Deleting my submission with ID:
{
ident
}
'
)
sub
.
delete
(
ident
=
ident
)
print
(
'
\n
My 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
'
)
```
...
...
This diff is collapsed.
Click to expand it.
rwth_nb/misc/notebook.py
+
9
−
12
View file @
8705b899
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
This diff is collapsed.
Click to expand it.
rwth_nb/misc/submission.py
+
42
−
7
View file @
8705b899
...
...
@@ -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
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment