@@ -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 """
0 commit comments