@@ -116,26 +116,84 @@ def jupyter_execute(
116
116
kernel : str ,
117
117
history : Optional [list [str ]] = None ,
118
118
path : Optional [str ] = None ,
119
- ) -> dict [str , Any ]: # type: ignore[empty-body]
119
+ ) -> list [ dict [str , Any ] ]: # type: ignore[empty-body]
120
120
"""
121
121
Execute code using a Jupyter kernel.
122
122
123
123
Args:
124
124
input (str): Code to execute.
125
- kernel (str): Name of kernel to use. Get options using jupyter.kernels().
125
+ kernel (str): Name of kernel to use. Get options using hub. jupyter.kernels().
126
126
history (Optional[list[str]]): Array of previous inputs (they get evaluated every time, but without output being captured).
127
127
path (Optional[str]): File path context for execution.
128
128
129
129
Returns:
130
- dict[str, Any]: JSON response containing execution results.
130
+ list[dict[str, Any]]: List of output items. Each output item contains
131
+ execution results with 'data' field containing output by MIME type
132
+ (e.g., 'text/plain' for text output) or 'name'/'text' fields for
133
+ stream output (stdout/stderr).
131
134
132
135
Examples:
133
136
Execute a simple sum using a Jupyter kernel:
134
137
135
- >>> import cocalc_api; project = cocalc_api.Project(api_key="sk-...")
136
- >>> project.jupyter.execute(history=['a=100;print(a)'],
137
- input='sum(range(a+1))',
138
- kernel='python3')
139
- {'output': [{'data': {'text/plain': '5050'}}], ...}
138
+ >>> import cocalc_api; project = cocalc_api.Project(api_key="sk-...", project_id='...')
139
+ >>> result = project.system.jupyter_execute(input='sum(range(100))', kernel='python3')
140
+ >>> result
141
+ [{'data': {'text/plain': '4950'}}]
142
+
143
+ Execute with history context:
144
+
145
+ >>> result = project.system.jupyter_execute(
146
+ ... history=['a = 100'],
147
+ ... input='sum(range(a + 1))',
148
+ ... kernel='python3')
149
+ >>> result
150
+ [{'data': {'text/plain': '5050'}}]
151
+
152
+ Print statements produce stream output:
153
+
154
+ >>> result = project.system.jupyter_execute(input='print("Hello")', kernel='python3')
155
+ >>> result
156
+ [{'name': 'stdout', 'text': 'Hello\\ n'}]
157
+ """
158
+ ...
159
+
160
+ @api_method ("system.listJupyterKernels" )
161
+ def list_jupyter_kernels (self ) -> list [dict [str , Any ]]: # type: ignore[empty-body]
162
+ """
163
+ List all running Jupyter kernels in the project.
164
+
165
+ Returns:
166
+ list[dict[str, Any]]: List of running kernels. Each kernel has:
167
+ - pid (int): Process ID of the kernel
168
+ - connectionFile (str): Path to the kernel connection file
169
+ - kernel_name (str, optional): Name of the kernel (e.g., 'python3')
170
+
171
+ Examples:
172
+ List all running kernels:
173
+
174
+ >>> import cocalc_api; project = cocalc_api.Project(api_key="sk-...", project_id='...')
175
+ >>> kernels = project.system.list_jupyter_kernels()
176
+ >>> kernels
177
+ [{'pid': 12345, 'connectionFile': '/run/user/1000/jupyter/kernel-abc123.json', 'kernel_name': 'python3'}]
178
+ """
179
+ ...
180
+
181
+ @api_method ("system.stopJupyterKernel" )
182
+ def stop_jupyter_kernel (self , pid : int ) -> dict [str , bool ]: # type: ignore[empty-body]
183
+ """
184
+ Stop a specific Jupyter kernel by process ID.
185
+
186
+ Args:
187
+ pid (int): Process ID of the kernel to stop
188
+
189
+ Returns:
190
+ dict[str, bool]: Dictionary with 'success' key indicating if the kernel was stopped
191
+
192
+ Examples:
193
+ Stop a kernel by PID:
194
+
195
+ >>> import cocalc_api; project = cocalc_api.Project(api_key="sk-...", project_id='...')
196
+ >>> project.system.stop_jupyter_kernel(pid=12345)
197
+ {'success': True}
140
198
"""
141
199
...
0 commit comments