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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RWTHjupyter
RWTH.nb
Commits
29840f3a
Commit
29840f3a
authored
4 years ago
by
Steffen Vogel
Browse files
Options
Downloads
Patches
Plain Diff
fix notebook helpers
parent
1851f5b0
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
rwth_nb/misc/notebook.py
+27
-15
27 additions, 15 deletions
rwth_nb/misc/notebook.py
tests/notebook.py
+11
-0
11 additions, 0 deletions
tests/notebook.py
with
38 additions
and
15 deletions
rwth_nb/misc/notebook.py
+
27
−
15
View file @
29840f3a
import
os
import
requests
from
IPython.lib
import
kernel
from
notebook
import
notebookapp
def
get_contents
():
servers
=
list
(
notebookapp
.
list_running_servers
())
base_url
=
'
https://jupyter.rwth-aachen.de
'
hub_api_url
=
f
'
{
base_url
}
/hub/api
'
token
=
servers
[
0
][
'
token
'
]
api_url
=
servers
[
0
][
'
url
'
]
+
'
api
'
def
get_current_path
():
token
=
os
.
environ
[
'
JUPYTERHUB_API_TOKEN
'
]
user
=
os
.
environ
[
'
JUPYTERHUB_USER
'
]
connection_file_path
=
kernel
.
get_connection_file
()
connection_file
=
os
.
path
.
basename
(
connection_file_path
)
kernel_id
=
connection_file
.
split
(
'
-
'
,
1
)[
1
].
split
(
'
.
'
)[
0
]
headers
=
{
'
Authorization
'
:
f
'
token
{
token
}
'
}
r
=
requests
.
request
(
'
GET
'
,
f
'
{
api_url
}
/sessions
'
,
headers
=
headers
)
r
=
requests
.
request
(
'
GET
'
,
f
'
{
base_url
}
/user/
{
user
}
/api/sessions
'
,
headers
=
{
'
Authorization
'
:
f
'
Bearer
{
token
}
'
})
r
.
raise_for_status
()
for
session
in
r
.
json
():
if
session
[
'
kernel
'
][
'
id
'
]
==
kernel_id
:
path
=
session
[
'
notebook
'
][
'
path
'
]
return
session
[
'
notebook
'
][
'
path
'
]
def
get_contents
(
path
=
None
,
user
=
None
,
token
=
None
):
if
path
is
None
:
path
=
get_current_path
()
if
user
is
None
:
user
=
os
.
environ
[
'
JUPYTERHUB_USER
'
]
if
token
is
None
:
token
=
os
.
environ
[
'
JUPYTERHUB_API_TOKEN
'
]
r
=
requests
.
request
(
'
GET
'
,
f
'
{
api_url
}
/contents/
{
path
}
'
,
headers
=
headers
)
r
=
requests
.
request
(
'
GET
'
,
f
'
{
base_url
}
/user/
{
user
}
/api/contents/
{
path
}
'
,
headers
=
{
'
Authorization
'
:
'
Bearer
'
+
token
})
r
.
raise_for_status
()
return
r
.
json
()
This diff is collapsed.
Click to expand it.
tests/notebook.py
0 → 100644
+
11
−
0
View file @
29840f3a
import
rwth_nb.misc.notebook
as
nb
from
pprint
import
pprint
# Works only inside a notebook!
path
=
get_current_path
()
content
=
get_contents
(
path
)
pprint
(
content
)
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