From ac5c9bc49165885b7a5802a40b82c82b9e3f33ef Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 13 May 2024 17:14:37 -0700 Subject: [PATCH] Comment out commands which doesnt work atm --- python/e2b_code_interpreter/main.py | 232 ++++++++++++++-------------- 1 file changed, 116 insertions(+), 116 deletions(-) diff --git a/python/e2b_code_interpreter/main.py b/python/e2b_code_interpreter/main.py index 36902f1..ead60d8 100644 --- a/python/e2b_code_interpreter/main.py +++ b/python/e2b_code_interpreter/main.py @@ -161,77 +161,77 @@ def default_kernel_id(self) -> str: self._default_kernel_id = self._kernel_id_set.result() return self._default_kernel_id - - def create_kernel( - self, - name: str, - path: str = "/home/user", - kernel_name: str = "python3", - timeout: Optional[float] = TIMEOUT, - ) -> str: - """ - Creates a new kernel, this can be useful if you want to have multiple independent code execution environments. - - The kernel can be optionally configured to start in a specific working directory and/or - with a specific kernel name. If no kernel name is provided, the default kernel will be used. - Once the kernel is created, this method establishes a WebSocket connection to the new kernel for - real-time communication. - - :param name: Name of the kernel - :param path: Sets the current working directory for the kernel. Defaults to "/home/user". - :param kernel_name: Specifies which kernel should be used, useful if you have multiple kernel types. - :param timeout: Timeout for the kernel creation request. - :return: Kernel id of the created kernel - """ - - x = { - "metadata": { - "signature": "hex-digest", - "kernel_info": {"name": kernel_name}, - "language_info": { - "name": kernel_name, - "version": "3.10.14", - }, # TODO: get version - }, - "nbformat": 4, - "nbformat_minor": 0, - "cells": [], - } - - self._sandbox.filesystem.write( - f"/home/user/default.ipynb", json.dumps(x), timeout=timeout - ) - self._sandbox.process.start("chmod 777 /home/user/default.ipynb") - - data = { - "name": name, - "kernel": {"name": kernel_name}, - "notebook": {"name": "default.ipynb"}, - "path": path, - "type": "notebook", - } - - logger.debug(f"Creating kernel with data: {data}") - - response = requests.post( - f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/sessions", - json=data, - timeout=timeout, - ) - if not response.ok: - raise KernelException(f"Failed to create kernel: {response.text}") - - response_data = response.json() - kernel_id = response_data["kernel"]["id"] - session_id = response_data["id"] - - logger.debug(f"Created kernel {kernel_id}, session {session_id}") - - threading.Thread( - target=self._connect_to_kernel_ws, args=(kernel_id, session_id, timeout) - ).start() - - return kernel_id + # + # def create_kernel( + # self, + # name: str, + # path: str = "/home/user", + # kernel_name: str = "python3", + # timeout: Optional[float] = TIMEOUT, + # ) -> str: + # """ + # Creates a new kernel, this can be useful if you want to have multiple independent code execution environments. + # + # The kernel can be optionally configured to start in a specific working directory and/or + # with a specific kernel name. If no kernel name is provided, the default kernel will be used. + # Once the kernel is created, this method establishes a WebSocket connection to the new kernel for + # real-time communication. + # + # :param name: Name of the kernel + # :param path: Sets the current working directory for the kernel. Defaults to "/home/user". + # :param kernel_name: Specifies which kernel should be used, useful if you have multiple kernel types. + # :param timeout: Timeout for the kernel creation request. + # :return: Kernel id of the created kernel + # """ + # + # x = { + # "metadata": { + # "signature": "hex-digest", + # "kernel_info": {"name": kernel_name}, + # "language_info": { + # "name": kernel_name, + # "version": "3.10.14", + # }, # TODO: get version + # }, + # "nbformat": 4, + # "nbformat_minor": 0, + # "cells": [], + # } + # + # self._sandbox.filesystem.write( + # f"/home/user/default.ipynb", json.dumps(x), timeout=timeout + # ) + # self._sandbox.process.start("chmod 777 /home/user/default.ipynb") + # + # data = { + # "name": name, + # "kernel": {"name": kernel_name}, + # "notebook": {"name": "default.ipynb"}, + # "path": path, + # "type": "notebook", + # } + # + # logger.debug(f"Creating kernel with data: {data}") + # + # response = requests.post( + # f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/sessions", + # json=data, + # timeout=timeout, + # ) + # if not response.ok: + # raise KernelException(f"Failed to create kernel: {response.text}") + # + # response_data = response.json() + # kernel_id = response_data["kernel"]["id"] + # session_id = response_data["id"] + # + # logger.debug(f"Created kernel {kernel_id}, session {session_id}") + # + # threading.Thread( + # target=self._connect_to_kernel_ws, args=(kernel_id, session_id, timeout) + # ).start() + # + # return kernel_id def restart_kernel( self, kernel_id: Optional[str] = None, timeout: Optional[float] = TIMEOUT @@ -261,51 +261,51 @@ def restart_kernel( threading.Thread( target=self._connect_to_kernel_ws, args=(kernel_id, timeout) ).start() - - def shutdown_kernel( - self, kernel_id: Optional[str] = None, timeout: Optional[float] = TIMEOUT - ) -> None: - """ - Shuts down an existing Jupyter kernel. This method is used to gracefully terminate a kernel's process. - - :param kernel_id: The unique identifier of the kernel to shutdown. If not provided, the default kernel is shutdown. - :param timeout: The timeout for the kernel shutdown request. - """ - kernel_id = kernel_id or self.default_kernel_id - logger.debug(f"Shutting down kernel {kernel_id}") - - self._connected_kernels[kernel_id].result().close() - del self._connected_kernels[kernel_id] - logger.debug(f"Closed websocket connection to kernel {kernel_id}") - - response = requests.delete( - f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/kernels/{kernel_id}", - timeout=timeout, - ) - if not response.ok: - raise KernelException(f"Failed to shutdown kernel {kernel_id}") - - logger.debug(f"Shutdown kernel {kernel_id}") - - def list_kernels(self, timeout: Optional[float] = TIMEOUT) -> List[str]: - """ - Lists all available Jupyter kernels. - - This method fetches a list of all currently available Jupyter kernels from the server. It can be used - to retrieve the IDs of all kernels that are currently running or available for connection. - - :param timeout: The timeout for the kernel list request. - :return: List of kernel ids - """ - response = requests.get( - f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/kernels", - timeout=timeout, - ) - - if not response.ok: - raise KernelException(f"Failed to list kernels: {response.text}") - - return [kernel["id"] for kernel in response.json()] + # + # def shutdown_kernel( + # self, kernel_id: Optional[str] = None, timeout: Optional[float] = TIMEOUT + # ) -> None: + # """ + # Shuts down an existing Jupyter kernel. This method is used to gracefully terminate a kernel's process. + # + # :param kernel_id: The unique identifier of the kernel to shutdown. If not provided, the default kernel is shutdown. + # :param timeout: The timeout for the kernel shutdown request. + # """ + # kernel_id = kernel_id or self.default_kernel_id + # logger.debug(f"Shutting down kernel {kernel_id}") + # + # self._connected_kernels[kernel_id].result().close() + # del self._connected_kernels[kernel_id] + # logger.debug(f"Closed websocket connection to kernel {kernel_id}") + # + # response = requests.delete( + # f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/kernels/{kernel_id}", + # timeout=timeout, + # ) + # if not response.ok: + # raise KernelException(f"Failed to shutdown kernel {kernel_id}") + # + # logger.debug(f"Shutdown kernel {kernel_id}") + # + # def list_kernels(self, timeout: Optional[float] = TIMEOUT) -> List[str]: + # """ + # Lists all available Jupyter kernels. + # + # This method fetches a list of all currently available Jupyter kernels from the server. It can be used + # to retrieve the IDs of all kernels that are currently running or available for connection. + # + # :param timeout: The timeout for the kernel list request. + # :return: List of kernel ids + # """ + # response = requests.get( + # f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/kernels", + # timeout=timeout, + # ) + # + # if not response.ok: + # raise KernelException(f"Failed to list kernels: {response.text}") + # + # return [kernel["id"] for kernel in response.json()] def close(self): """