Skip to content

Commit dbc0f23

Browse files
committed
Fix creating new session in the same folder
1 parent 2bdee17 commit dbc0f23

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

python/e2b_code_interpreter/main.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,18 @@ def create_kernel(
141141
:param timeout: Timeout for the kernel creation request.
142142
:return: Kernel id of the created kernel
143143
"""
144-
data = {"path": cwd, "kernel": {"name": "python3"}, "type": "notebook", "name": str(uuid.uuid4())}
145-
if kernel_name:
146-
data["kernel"]['name'] = kernel_name
144+
kernel_name = kernel_name or "python3"
145+
response = requests.post(
146+
f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/kernels",
147+
json={"name": kernel_name, "path": cwd},
148+
timeout=timeout,
149+
)
150+
if not response.ok:
151+
raise KernelException(f"Failed to create kernel: {response.text}")
152+
153+
kernel_data = response.json()
154+
kernel_id = kernel_data["id"]
155+
data = {"path": str(uuid.uuid4()), "kernel": {"name": kernel_name, "id": kernel_id}, "type": "notebook", "name": str(uuid.uuid4())}
147156
logger.debug(f"Creating kernel with data: {data}")
148157

149158
response = requests.post(
@@ -155,8 +164,17 @@ def create_kernel(
155164
raise KernelException(f"Failed to create kernel: {response.text}")
156165

157166
session_data = response.json()
158-
kernel_id = session_data["kernel"]["id"]
159167
session_id = session_data["id"]
168+
kernel_id = session_data["kernel"]["id"]
169+
170+
response = requests.patch(
171+
f"{self._sandbox.get_protocol()}://{self._sandbox.get_hostname(8888)}/api/sessions/{session_id}",
172+
json={"path": cwd},
173+
timeout=timeout,
174+
)
175+
if not response.ok:
176+
raise KernelException(f"Failed to create kernel: {response.text}")
177+
160178
logger.debug(f"Created kernel {kernel_id}")
161179

162180
threading.Thread(

0 commit comments

Comments
 (0)