Skip to content

Commit 601dd25

Browse files
implementations of api client for jupyter notebooks
1 parent d3d6c3e commit 601dd25

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

dataikuapi/dss/notebook.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ class DSSNotebook(object):
44
"""
55
A Python/R/Scala notebook on the DSS instance
66
"""
7-
def __init__(self, client, project_key, notebook_name, state=None):
7+
def __init__(self, client, project_key, notebook_name, state=None, content=None):
88
self.client = client
99
self.project_key = project_key
1010
self.notebook_name = notebook_name
1111
self.state = state
12+
self.content = content
1213
self.state_is_peek = True
1314

1415
def unload(self, session_id=None):
@@ -27,14 +28,19 @@ def unload(self, session_id=None):
2728
raise Exception("Several sessions of the notebook are running, choose one")
2829
else:
2930
session_id = state['activeSessions'][0].get('sessionId', None)
30-
return self.client._perform_json("DELETE", "/projects/%s/notebooks/" % self.project_key, params={'notebookName' : self.notebook_name, 'sessionId' : session_id})
31+
return self.client._perform_json("DELETE",
32+
"/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self.project_key, self.notebook_name, session_id))
3133

3234
def get_state(self):
3335
"""
3436
Get the status of the notebook
3537
"""
36-
if self.state is None:
37-
self.state = self.client._perform_json("GET", "/projects/%s/notebooks/" % self.project_key, params={'notebookName' : self.notebook_name})
38+
notebook_list = self.client._perform_json("GET",
39+
"/projects/%s/jupyter-notebooks/" % self.project_key,
40+
params={"active": False})
41+
for notebook in notebook_list:
42+
if notebook.get("name") == self.notebook_name:
43+
self.state = notebook
3844
return self.state
3945

4046
def get_sessions(self):
@@ -48,6 +54,30 @@ def get_sessions(self):
4854
raise Exception("Notebook isn't running")
4955
return state['activeSessions']
5056

57+
def get_content(self):
58+
"""
59+
Get the content of this notebook (metadata, cells, nbformat)
60+
"""
61+
if self.content is None:
62+
self.content = self.client._perform_json("GET",
63+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name))
64+
return self.content
65+
66+
def save(self):
67+
"""
68+
Save the content of this notebook
69+
"""
70+
return self.client._perform_json("PUT",
71+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name),
72+
body=self.content)
73+
74+
def delete(self):
75+
"""
76+
Delete this jupyter notebook and stop all of its active sessions.
77+
"""
78+
return self.client._perform_json("DELETE",
79+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, self.notebook_name))
80+
5181
########################################################
5282
# Discussions
5383
########################################################

dataikuapi/dss/project.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,54 @@ def new_job_definition_builder(self, job_type='NON_RECURSIVE_FORCED_BUILD'):
825825
warnings.warn("new_job_definition_builder is deprecated, please use new_job", DeprecationWarning)
826826
return JobDefinitionBuilder(self, job_type)
827827

828+
########################################################
829+
# Jupyter Notebooks
830+
########################################################
831+
832+
def list_jupyter_notebooks(self, as_objects=True, active=False):
833+
"""
834+
List the jupyter notebooks of a project.
835+
836+
:param bool as_objects: if True, return the jupyter notebooks as a :class:`dataikuapi.dss.notebook.DSSNotebook`
837+
notebook handles instead of raw JSON
838+
:param bool active: if True, only return currently running jupyter notebooks.
839+
840+
841+
:returns: The list of the notebooks - see as_objects for more information
842+
:rtype: list
843+
"""
844+
notebooks = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/" % self.project_key,
845+
params={"active": active})
846+
if as_objects:
847+
return [DSSNotebook(self.client, notebook['projectKey'], notebook['name'], notebook) for notebook in notebooks]
848+
else:
849+
return notebooks
850+
851+
def get_jupyter_notebook(self, notebook_name):
852+
"""
853+
Get a handle to interact with a specific jupyter notebook
854+
855+
:param str notebook_name: The name of the jupyter notebook to retrieve
856+
:returns: A handle to interact with this jupyter notebook
857+
:rtype: :class:`~dataikuapi.dss.notebook.DSSNotebook` jupyter notebook handle
858+
"""
859+
notebook_content = self.client._perform_json("GET",
860+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name))
861+
return DSSNotebook(self.client, self.project_key, notebook_name, None, notebook_content)
862+
863+
def create_jupyter_notebook(self, notebook_name, notebook_content):
864+
"""
865+
Create a new jupyter notebook.
866+
867+
:param str notebook_name: the name of the notebook to create
868+
:param dict notebook_content: the data of the notebook to create, as a dict.
869+
The data will be converted to a JSON string internally.
870+
Use ``get_state()`` on a similar existing ``DSSNotebook`` object in order to get a sample definition object
871+
"""
872+
return self.client._perform_json("POST",
873+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name),
874+
body=notebook_content)
875+
828876
########################################################
829877
# Continuous activities
830878
########################################################

0 commit comments

Comments
 (0)