Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions dataikuapi/dss/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ class DSSNotebook(object):
"""
A Python/R/Scala notebook on the DSS instance
"""
def __init__(self, client, project_key, notebook_name, state=None):
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):
Expand All @@ -27,14 +28,20 @@ def unload(self, session_id=None):
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/notebooks/" % self.project_key, params={'notebookName' : self.notebook_name, 'sessionId' : session_id})
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is (and was) not compatible with SQL notebooks

Suggested change
Get the status of the notebook
Get the status of the Jupyter notebook

"""
if self.state is None:
self.state = self.client._perform_json("GET", "/projects/%s/notebooks/" % self.project_key, params={'notebookName' : self.notebook_name})
notebook_list = self.client._perform_json("GET",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep the cache mechanism?
(If you put back the cache, maybe you should add with an optional parameter clear_cache = false for when the user know the cache is wrong. Or a clear_cache() method)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable contains the metadata of the notebook, not a notebook.
The java object is names JupyterNotebookListItem which is super confusing so maybe let's not use that.
Maybe something like:

Suggested change
notebook_list = self.client._perform_json("GET",
notebook_states = 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):
Expand All @@ -48,6 +55,30 @@ def get_sessions(self):
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
########################################################
Expand Down
48 changes: 48 additions & 0 deletions dataikuapi/dss/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,54 @@ def new_job_definition_builder(self, job_type='NON_RECURSIVE_FORCED_BUILD'):
warnings.warn("new_job_definition_builder is deprecated, please use new_job", DeprecationWarning)
return JobDefinitionBuilder(self, job_type)

########################################################
# Jupyter Notebooks
########################################################

def list_jupyter_notebooks(self, as_objects=True, active=False):
"""
List the jupyter notebooks of a project.

:param bool as_objects: if True, return the jupyter notebooks as a :class:`dataikuapi.dss.notebook.DSSNotebook`
notebook handles instead of raw JSON
:param bool active: if True, only return currently running jupyter notebooks.


:returns: The list of the notebooks - see as_objects for more information
:rtype: list
"""
notebooks = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/" % self.project_key,
params={"active": active})
if as_objects:
return [DSSNotebook(self.client, notebook_state['projectKey'], notebook_state['name'], state=notebook_state) for notebook_state in notebooks]
else:
return notebooks

def get_jupyter_notebook(self, notebook_name):
"""
Get a handle to interact with a specific jupyter notebook

:param str notebook_name: The name of the jupyter notebook to retrieve
:returns: A handle to interact with this jupyter notebook
:rtype: :class:`~dataikuapi.dss.notebook.DSSNotebook` jupyter notebook handle
"""
notebook_content = self.client._perform_json("GET",
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name))
return DSSNotebook(self.client, self.project_key, notebook_name, content=notebook_content)

def create_jupyter_notebook(self, notebook_name, notebook_content):
"""
Create a new jupyter notebook.

:param str notebook_name: the name of the notebook to create
:param dict notebook_content: the data of the notebook to create, as a dict.
The data will be converted to a JSON string internally.
Use ``get_state()`` on a similar existing ``DSSNotebook`` object in order to get a sample definition object
"""
return self.client._perform_json("POST",
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name),
body=notebook_content)

########################################################
# Continuous activities
########################################################
Expand Down