@@ -48,7 +48,7 @@ class JupyterExtension:
48
48
def __init__ (self , sandbox : CodeInterpreter ):
49
49
self ._sandbox = sandbox
50
50
self ._kernel_id_set = Future ()
51
- self ._start_connectiong_to_default_kernel ()
51
+ self ._start_connecting_to_default_kernel ()
52
52
53
53
def exec_cell (
54
54
self ,
@@ -57,6 +57,14 @@ def exec_cell(
57
57
on_stdout : Optional [Callable [[ProcessMessage ], Any ]] = None ,
58
58
on_stderr : Optional [Callable [[ProcessMessage ], Any ]] = None ,
59
59
) -> Result :
60
+ """
61
+ Execute code in a notebook cell.
62
+ :param code: Code to execute
63
+ :param kernel_id: Kernel id to use, if not provided the default kernel will be used
64
+ :param on_stdout: Callback for stdout messages
65
+ :param on_stderr: Callback for stderr messages
66
+ :return: Result of the execution
67
+ """
60
68
kernel_id = kernel_id or self .default_kernel_id
61
69
ws = self ._connected_kernels .get (kernel_id )
62
70
@@ -74,12 +82,30 @@ def exec_cell(
74
82
75
83
@property
76
84
def default_kernel_id (self ) -> str :
85
+ """
86
+ Get the default kernel id
87
+ :return: Default kernel id
88
+ """
77
89
if not self ._default_kernel_id :
78
90
self ._default_kernel_id = self ._kernel_id_set .result ()
79
91
80
92
return self ._default_kernel_id
81
93
82
- def create_kernel (self , cwd : str = "/home/user" , kernel_name : Optional [str ] = None , timeout : Optional [float ] = TIMEOUT ) -> str :
94
+ def create_kernel (
95
+ self ,
96
+ cwd : str = "/home/user" ,
97
+ kernel_name : Optional [str ] = None ,
98
+ timeout : Optional [float ] = TIMEOUT
99
+ ) -> str :
100
+ """
101
+ Create a new kernel, this can be useful if you want to have multiple independent code execution environments.
102
+ :param cwd: Sets the current working directory for the kernel
103
+ :param kernel_name:
104
+ Specifies which kernel should be used, useful if you have multiple kernel types.
105
+ If not provided, the default kernel will be used - "python3".
106
+ :param timeout: Sets timeout for the call
107
+ :return: Kernel id of the created kernel
108
+ """
83
109
data = {"cwd" : cwd }
84
110
if kernel_name :
85
111
data ["kernel_name" ] = kernel_name
@@ -100,6 +126,12 @@ def create_kernel(self, cwd: str = "/home/user", kernel_name: Optional[str] = No
100
126
def restart_kernel (
101
127
self , kernel_id : Optional [str ] = None , timeout : Optional [float ] = TIMEOUT
102
128
) -> None :
129
+ """
130
+ Restart a kernel
131
+ :param kernel_id:
132
+ :param timeout:
133
+ :return:
134
+ """
103
135
kernel_id = kernel_id or self .default_kernel_id
104
136
105
137
self ._connected_kernels [kernel_id ].close ()
@@ -115,6 +147,11 @@ def restart_kernel(
115
147
def shutdown_kernel (
116
148
self , kernel_id : Optional [str ] = None , timeout : Optional [float ] = TIMEOUT
117
149
) -> None :
150
+ """
151
+ Shutdown a kernel
152
+ :param kernel_id: Kernel id to shutdown
153
+ :param timeout: Timeout for the call
154
+ """
118
155
kernel_id = kernel_id or self .default_kernel_id
119
156
120
157
self ._connected_kernels [kernel_id ].close ()
@@ -126,6 +163,11 @@ def shutdown_kernel(
126
163
raise KernelException (f"Failed to shutdown kernel { kernel_id } " )
127
164
128
165
def list_kernels (self , timeout : Optional [float ] = TIMEOUT ) -> List [str ]:
166
+ """
167
+ List all the kernels
168
+ :param timeout: Timeout for the call
169
+ :return: List of kernel ids
170
+ """
129
171
response = requests .get (
130
172
f"{ self ._sandbox .get_protocol ()} ://{ self ._sandbox .get_hostname (8888 )} /api/kernels" ,
131
173
timeout = timeout ,
@@ -135,6 +177,10 @@ def list_kernels(self, timeout: Optional[float] = TIMEOUT) -> List[str]:
135
177
return [kernel ["id" ] for kernel in response .json ()]
136
178
137
179
def close (self ):
180
+ """
181
+ Close all the websocket connections to the kernels. It doesn't shutdown the kernels.
182
+ :return:
183
+ """
138
184
for ws in self ._connected_kernels .values ():
139
185
ws .close ()
140
186
@@ -145,7 +191,7 @@ def _connect_to_kernel_ws(self, kernel_id: str) -> None:
145
191
ws .connect ()
146
192
self ._connected_kernels [kernel_id ] = ws
147
193
148
- def _start_connectiong_to_default_kernel (self , timeout : Optional [float ] = TIMEOUT ) -> None :
194
+ def _start_connecting_to_default_kernel (self , timeout : Optional [float ] = TIMEOUT ) -> None :
149
195
def setup_default_kernel ():
150
196
kernel_id = self ._sandbox .filesystem .read ("/root/.jupyter/kernel_id" , timeout = timeout ).strip ()
151
197
self ._connect_to_kernel_ws (kernel_id )
0 commit comments