Skip to content

Commit a23d027

Browse files
Use changes from PR#11966 :
* Use new sessions endpoint in get_sessions call * Put back cache for state with a refresh parameter to get the state directly from backend * Return created notebook as object
1 parent 08301dc commit a23d027

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

dataikuapi/dss/notebook.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,46 @@ def unload(self, session_id=None):
1616
"""
1717
Stop this Jupyter notebook and release its resources
1818
"""
19-
state = self.get_state()
20-
if state is None:
19+
sessions = self.get_sessions()
20+
if sessions is None:
2121
raise Exception("Notebook isn't running")
22-
if state.get('activeSessions', None) is None:
23-
raise Exception("Notebook isn't running")
24-
if len(state['activeSessions']) == 0:
22+
if len(sessions) == 0:
2523
raise Exception("Notebook isn't running")
2624
if session_id is None:
27-
if len(state['activeSessions']) > 1:
25+
if len(sessions) > 1:
2826
raise Exception("Several sessions of the notebook are running, choose one")
2927
else:
30-
session_id = state['activeSessions'][0].get('sessionId', None)
28+
session_id = sessions[0].get('sessionId', None)
3129
return self.client._perform_json("DELETE",
3230
"/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self.project_key, self.notebook_name, session_id))
3331

34-
def get_state(self):
32+
def get_state(self, refresh=False):
3533
"""
3634
Get the status of this Jupyter notebook
35+
36+
:param bool refresh: if True, get the status of the notebook from the backend
3737
"""
3838
notebook_states = self.client._perform_json("GET",
39-
"/projects/%s/jupyter-notebooks/" % self.project_key,
40-
params={"active": False})
41-
for notebook in notebook_states:
42-
if notebook.get("name") == self.notebook_name:
43-
self.state = notebook
39+
"/projects/%s/jupyter-notebooks/" % self.project_key,
40+
params={"active": False})
41+
if self.state is None or refresh:
42+
for state in notebook_states:
43+
if state.get("name") == self.notebook_name:
44+
self.state = state
4445
return self.state
4546
return self.state
4647

4748
def get_sessions(self):
4849
"""
49-
Get the list of the running sessions of this Jupyter notebook
50+
Get the list of running sessions of this Jupyter notebook
5051
"""
51-
state = self.get_state()
52-
if state is None:
53-
raise Exception("Notebook isn't running")
54-
if state.get('activeSessions', None) is None:
55-
raise Exception("Notebook isn't running")
56-
return state['activeSessions']
52+
53+
if self.state is None:
54+
self.state = {}
55+
sessions = self.client._perform_json("GET",
56+
"/projects/%s/jupyter-notebooks/%s/sessions" % (self.project_key, self.notebook_name))
57+
self.state["activeSessions"] = sessions
58+
return sessions
5759

5860
def get_content(self):
5961
"""

dataikuapi/dss/project.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,16 +862,19 @@ def get_jupyter_notebook(self, notebook_name):
862862

863863
def create_jupyter_notebook(self, notebook_name, notebook_content):
864864
"""
865-
Create a new jupyter notebook.
865+
Create a new jupyter notebook and get a handle to interact with it
866866
867867
:param str notebook_name: the name of the notebook to create
868868
:param dict notebook_content: the data of the notebook to create, as a dict.
869869
The data will be converted to a JSON string internally.
870870
Use ``get_state()`` on a similar existing ``DSSNotebook`` object in order to get a sample definition object
871+
:returns: A handle to interact with the newly created jupyter notebook
872+
:rtype: :class:`~dataikuapi.dss.notebook.DSSNotebook` jupyter notebook handle
871873
"""
872-
return self.client._perform_json("POST",
873-
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name),
874-
body=notebook_content)
874+
self.client._perform_json("POST",
875+
"/projects/%s/jupyter-notebooks/%s" % (self.project_key, notebook_name),
876+
body=notebook_content)
877+
return DSSNotebook(self.client, self.project_key, notebook_name, content=notebook_content)
875878

876879
########################################################
877880
# Continuous activities

0 commit comments

Comments
 (0)