Skip to content

Commit

Permalink
Speed up progressive rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Oct 3, 2019
1 parent 9f51f17 commit 768a20e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 4 additions & 1 deletion voila/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ 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:
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)

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

0 comments on commit 768a20e

Please sign in to comment.