-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathnotebook.py
More file actions
92 lines (83 loc) · 3.61 KB
/
notebook.py
File metadata and controls
92 lines (83 loc) · 3.61 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from .discussion import DSSObjectDiscussions
class DSSNotebook(object):
"""
A Python/R/Scala notebook on the DSS instance
"""
def __init__(self, client, project_key, notebook_name, state=None, content=None):
self.client = client
self.project_key = project_key
self.notebook_name = notebook_name
self.state = state
self.content = content
self.state_is_peek = True
def unload(self, session_id=None):
"""
Stop the notebook and release its resources
"""
state = self.get_state()
if state is None:
raise Exception("Notebook isn't running")
if state.get('activeSessions', None) is None:
raise Exception("Notebook isn't running")
if len(state['activeSessions']) == 0:
raise Exception("Notebook isn't running")
if session_id is None:
if len(state['activeSessions']) > 1:
raise Exception("Several sessions of the notebook are running, choose one")
else:
session_id = state['activeSessions'][0].get('sessionId', None)
return self.client._perform_json("DELETE",
"/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self.project_key, self.notebook_name, session_id))
def get_state(self):
"""
Get the status of the notebook
"""
notebook_list = self.client._perform_json("GET",
"/projects/%s/jupyter-notebooks/" % self.project_key,
params={"active": False})
for notebook in notebook_list:
if notebook.get("name") == self.notebook_name:
self.state = notebook
return self.state
return self.state
def get_sessions(self):
"""
Get the list of the running sessions of this notebook
"""
state = self.get_state()
if state is None:
raise Exception("Notebook isn't running")
if state.get('activeSessions', None) is None:
raise Exception("Notebook isn't running")
return state['activeSessions']
def get_content(self):
"""
Get the content of this notebook (metadata, cells, nbformat)
"""
if self.content is None:
self.content = self.client._perform_json("GET",
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name))
return self.content
def save(self):
"""
Save the content of this notebook
"""
return self.client._perform_json("PUT",
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name),
body=self.content)
def delete(self):
"""
Delete this jupyter notebook and stop all of its active sessions.
"""
return self.client._perform_json("DELETE",
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name))
########################################################
# Discussions
########################################################
def get_object_discussions(self):
"""
Get a handle to manage discussions on the notebook
:returns: the handle to manage discussions
:rtype: :class:`dataikuapi.discussion.DSSObjectDiscussions`
"""
return DSSObjectDiscussions(self.client, self.project_key, "JUPYTER_NOTEBOOK", self.notebook_name)