WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 97 additions & 80 deletions voila/tornado/execution_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from jupyter_server.base.handlers import JupyterHandler
from tornado.websocket import WebSocketHandler
from tornado.web import HTTPError
from tornado.ioloop import IOLoop

try:
JUPYTER_SERVER_2 = True
Expand Down Expand Up @@ -48,91 +49,107 @@ async def on_message(
message = json.loads(message_str)
action = message.get("action", None)
payload = message.get("payload", {})
if action == "execute":
request_kernel_id = payload.get("kernel_id")
if request_kernel_id != self._kernel_id:
await self.write_message(
{
"action": "execution_error",
"payload": {"error": "Kernel ID does not match"},
}

if action != "execute":
await self.write_message(
{
"action": "error",
"payload": {"error": f"Unknown action: {action}"},
}
)
return

request_kernel_id = payload.get("kernel_id")
if request_kernel_id != self._kernel_id:
await self.write_message(
{
"action": "execution_error",
"payload": {"error": "Kernel ID does not match"},
}
)
return

km = await ensure_async(self.kernel_manager.get_kernel(self._kernel_id))
execution_data = self._execution_data.pop(self._kernel_id, None)
if execution_data is None:
await self.write_message(
{
"action": "execution_error",
"payload": {"error": "Missing notebook data"},
}
)
return

nb = execution_data["nb"]
self._executor = executor = VoilaExecutor(
nb,
km=km,
config=execution_data["config"],
show_tracebacks=execution_data["show_tracebacks"],
)
executor.kc = await executor.async_start_new_kernel_client()

# schedule notebook run and return so we don't block other messages
IOLoop.current().spawn_callback(self._run_notebook, nb, executor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

async def _run_notebook(self, nb, executor) -> Union[Awaitable[None], None]:
total_cell = len(nb.cells)
for cell_idx, input_cell in enumerate(nb.cells):
try:
output_cell = await executor.execute_cell(
input_cell, None, cell_idx, store_history=False
)
return
kernel_future = self.kernel_manager.get_kernel(self._kernel_id)
km = await ensure_async(kernel_future)
execution_data = self._execution_data.pop(self._kernel_id, None)
if execution_data is None:
except TimeoutError:
output_cell = input_cell

except CellExecutionError:
self.log.exception(
"Error at server while executing cell: %r", input_cell
)
if executor.should_strip_error():
strip_code_cell_warnings(input_cell)
executor.strip_code_cell_errors(input_cell)
output_cell = input_cell

except Exception as e:
self.log.exception(
"Error at server while executing cell: %r", input_cell
)
output_cell = nbformat.v4.new_code_cell()
if executor.should_strip_error():
output_cell.outputs = [
{
"output_type": "stream",
"name": "stderr",
"text": "An exception occurred at the server (not the notebook). {}".format(
executor.cell_error_instruction
),
}
]
else:
output_cell.outputs = [
{
"output_type": "error",
"ename": type(e).__name__,
"evalue": str(e),
"traceback": traceback.format_exception(
*sys.exc_info()
),
}
]
finally:
output_cell.pop("source", None)
await self.write_message(
{
"action": "execution_error",
"payload": {"error": "Missing notebook data"},
"action": "execution_result",
"payload": {
"output_cell": output_cell,
"cell_index": cell_idx,
"total_cell": total_cell,
},
}
)
return
nb = execution_data["nb"]
self._executor = executor = VoilaExecutor(
nb,
km=km,
config=execution_data["config"],
show_tracebacks=execution_data["show_tracebacks"],
)
executor.kc = await executor.async_start_new_kernel_client()
total_cell = len(nb.cells)
for cell_idx, input_cell in enumerate(nb.cells):
try:
output_cell = await executor.execute_cell(
input_cell, None, cell_idx, store_history=False
)
except TimeoutError:
output_cell = input_cell

except CellExecutionError:
self.log.exception(
"Error at server while executing cell: %r", input_cell
)
if executor.should_strip_error():
strip_code_cell_warnings(input_cell)
executor.strip_code_cell_errors(input_cell)
output_cell = input_cell

except Exception as e:
self.log.exception(
"Error at server while executing cell: %r", input_cell
)
output_cell = nbformat.v4.new_code_cell()
if executor.should_strip_error():
output_cell.outputs = [
{
"output_type": "stream",
"name": "stderr",
"text": "An exception occurred at the server (not the notebook). {}".format(
executor.cell_error_instruction
),
}
]
else:
output_cell.outputs = [
{
"output_type": "error",
"ename": type(e).__name__,
"evalue": str(e),
"traceback": traceback.format_exception(
*sys.exc_info()
),
}
]
finally:
output_cell.pop("source", None)
await self.write_message(
{
"action": "execution_result",
"payload": {
"output_cell": output_cell,
"cell_index": cell_idx,
"total_cell": total_cell,
},
}
)

def on_close(self) -> None:
if self._executor and self._executor.kc:
Expand Down