@@ -4,49 +4,82 @@ 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 ):
1516 """
16- Stop the notebook and release its resources
17+ Stop this Jupyter notebook and release its resources
1718 """
18- state = self .get_state ()
19- if state is None :
19+ sessions = self .get_sessions ()
20+ if sessions is None :
2021 raise Exception ("Notebook isn't running" )
21- if state .get ('activeSessions' , None ) is None :
22- raise Exception ("Notebook isn't running" )
23- if len (state ['activeSessions' ]) == 0 :
22+ if len (sessions ) == 0 :
2423 raise Exception ("Notebook isn't running" )
2524 if session_id is None :
26- if len (state [ 'activeSessions' ] ) > 1 :
25+ if len (sessions ) > 1 :
2726 raise Exception ("Several sessions of the notebook are running, choose one" )
2827 else :
29- 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 })
28+ session_id = sessions [0 ].get ('sessionId' , None )
29+ return self .client ._perform_json ("DELETE" ,
30+ "/projects/%s/jupyter-notebooks/%s/sessions/%s" % (self .project_key , self .notebook_name , session_id ))
3131
32- def get_state (self ):
32+ def get_state (self , refresh = False ):
3333 """
34- Get the status of the notebook
34+ Get the status of this Jupyter notebook
35+
36+ :param bool refresh: if True, get the status of the notebook from the backend
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_states = self .client ._perform_json ("GET" ,
39+ "/projects/%s/jupyter-notebooks/" % self .project_key ,
40+ params = {"active" : False })
41+ if self .state is None or refresh :
42+ for notebook_state in notebook_states :
43+ if notebook_state .get ("name" ) == self .notebook_name :
44+ self .state = notebook_state
45+ break
3846 return self .state
3947
4048 def get_sessions (self ):
4149 """
42- Get the list of the running sessions of this notebook
50+ Get the list of running sessions of this Jupyter notebook
4351 """
44- state = self .get_state ()
45- if state is None :
46- raise Exception ("Notebook isn't running" )
47- if state .get ('activeSessions' , None ) is None :
48- raise Exception ("Notebook isn't running" )
49- 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
59+
60+ def get_content (self ):
61+ """
62+ Get the content of this Jupyter notebook (metadata, cells, nbformat)
63+ """
64+ if self .content is None :
65+ self .content = self .client ._perform_json ("GET" ,
66+ "/projects/%s/jupyter-notebooks/%s" % (self .project_key , self .notebook_name ))
67+ return self .content
68+
69+ def save (self ):
70+ """
71+ Save the content of this Jupyter notebook
72+ """
73+ return self .client ._perform_json ("PUT" ,
74+ "/projects/%s/jupyter-notebooks/%s" % (self .project_key , self .notebook_name ),
75+ body = self .content )
76+
77+ def delete (self ):
78+ """
79+ Delete this Jupyter notebook and stop all of its active sessions.
80+ """
81+ return self .client ._perform_json ("DELETE" ,
82+ "/projects/%s/jupyter-notebooks/%s" % (self .project_key , self .notebook_name ))
5083
5184 ########################################################
5285 # Discussions
0 commit comments