Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up progressive rendering #396

Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion voila/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,27 @@ def set_state(self, state):

class VoilaExecutePreprocessor(ExecutePreprocessor):
"""Execute, but respect the output widget behaviour"""
def preprocess(self, nb, resources, km=None):
def __init__(self, **kwargs):
super(VoilaExecutePreprocessor, self).__init__(**kwargs)
self.output_hook_stack = collections.defaultdict(list) # maps to list of hooks, where the last is used
self.output_objects = {}

def preprocess(self, nb, resources, km=None):
try:
result = super(VoilaExecutePreprocessor, self).preprocess(nb, resources=resources, km=km)
except CellExecutionError as e:
self.log.error(e)
result = (nb, resources)
return result

def preprocess_cell(self, cell, resources, cell_index, store_history=True):
try:
result = super(VoilaExecutePreprocessor, self).preprocess_cell(cell, resources, cell_index, store_history)
except CellExecutionError as e:
self.log.error(e)
result = [cell]
return result

Comment on lines +111 to +117
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

To mimic the VoilaExecutePreprocessor.preprocess behavior. I change this behavior a bit in a separate PR concerning error outputs BTW.

Copy link
Member Author

Choose a reason for hiding this comment

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

The try except prevents a 505 server error when the Notebook contains an error

def register_output_hook(self, msg_id, hook):
# mimics
# https://jupyterlab.github.io/jupyterlab/services/interfaces/kernel.ikernelconnection.html#registermessagehook
Expand Down
21 changes: 11 additions & 10 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from jupyter_server.utils import url_path_join
import nbformat

from .execute import executenb
from nbconvert.preprocessors import ClearOutputPreprocessor

from .execute import executenb, VoilaExecutePreprocessor
from .exporter import VoilaExporter


Expand Down Expand Up @@ -133,15 +135,14 @@ def _jinja_cell_generator(self, nb, kernel_id):
"""Generator that will execute a single notebook cell at a time"""
km = self.kernel_manager.get_kernel(kernel_id)

all_cells = list(nb.cells) # copy the cells, since we will modify in place
for cell in all_cells:
# we execute one cell at a time
nb.cells = [cell] # reuse the same notebook
result = executenb(nb, km=km, cwd=self.cwd, config=self.traitlet_config)
cell = result.cells[0] # keep a reference to the executed cell
nb.cells = all_cells # restore notebook in case we access it from the template
# we don't filter empty cells, since we do not know how many empty code cells we will have
yield cell
nb, resources = ClearOutputPreprocessor().preprocess(nb, {'metadata': {'path': self.cwd}})
ep = VoilaExecutePreprocessor(config=self.traitlet_config)
maartenbreddels marked this conversation as resolved.
Show resolved Hide resolved

with ep.setup_preprocessor(nb, resources, km=km):
for cell_idx, cell in enumerate(nb.cells):
res = ep.preprocess_cell(cell, resources, cell_idx, store_history=False)

yield res[0]

@tornado.gen.coroutine
def load_notebook(self, path):
Expand Down