-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert coalescese_streams function to CoalesceStreamsPreprocessor
`coalescese_streams` was the last remaining "decorated function Preprocessor", and I couldn't find an example of how to use it. Here it is converted to be a Preprocessor subclass, like the others. A top-level --coalesce-streams flag is also added.
- Loading branch information
1 parent
1562531
commit 8fb5d4c
Showing
6 changed files
with
90 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,44 @@ | ||
"""Preprocessor for merging consecutive stream outputs for easier handling.""" | ||
import re | ||
|
||
# Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
from nbconvert.preprocessors import Preprocessor | ||
|
||
import functools | ||
import re | ||
|
||
from traitlets.log import get_logger | ||
|
||
|
||
def cell_preprocessor(function): | ||
""" | ||
Wrap a function to be executed on all cells of a notebook | ||
The wrapped function should have these parameters: | ||
cell : NotebookNode cell | ||
Notebook cell being processed | ||
resources : dictionary | ||
Additional resources used in the conversion process. Allows | ||
preprocessors to pass variables into the Jinja engine. | ||
index : int | ||
Index of the cell being processed | ||
""" | ||
|
||
@functools.wraps(function) | ||
def wrappedfunc(nb, resources): | ||
get_logger().debug("Applying preprocessor: %s", function.__name__) | ||
for index, cell in enumerate(nb.cells): | ||
nb.cells[index], resources = function(cell, resources, index) | ||
return nb, resources | ||
|
||
return wrappedfunc | ||
|
||
CR_PAT = re.compile(r".*\r(?=[^\n])") | ||
|
||
cr_pat = re.compile(r".*\r(?=[^\n])") | ||
|
||
|
||
@cell_preprocessor | ||
def coalesce_streams(cell, resources, index): | ||
class CoalesceStreamsPreprocessor(Preprocessor): | ||
""" | ||
Merge consecutive sequences of stream output into single stream | ||
to prevent extra newlines inserted at flush calls | ||
Parameters | ||
---------- | ||
cell : NotebookNode cell | ||
Notebook cell being processed | ||
resources : dictionary | ||
Additional resources used in the conversion process. Allows | ||
transformers to pass variables into the Jinja engine. | ||
index : int | ||
Index of the cell being processed | ||
""" | ||
|
||
outputs = cell.get("outputs", []) | ||
if not outputs: | ||
def preprocess_cell(self, cell, resources, cell_index): | ||
""" | ||
Apply a transformation on each cell. See base.py for details. | ||
""" | ||
outputs = cell.get("outputs", []) | ||
if not outputs: | ||
return cell, resources | ||
|
||
last = outputs[0] | ||
new_outputs = [last] | ||
for output in outputs[1:]: | ||
if ( | ||
output.output_type == "stream" | ||
and last.output_type == "stream" | ||
and last.name == output.name | ||
): | ||
last.text += output.text | ||
else: | ||
new_outputs.append(output) | ||
last = output | ||
|
||
# process \r characters | ||
for output in new_outputs: | ||
if output.output_type == "stream" and "\r" in output.text: | ||
output.text = CR_PAT.sub("", output.text) | ||
|
||
cell.outputs = new_outputs | ||
return cell, resources | ||
|
||
last = outputs[0] | ||
new_outputs = [last] | ||
for output in outputs[1:]: | ||
if ( | ||
output.output_type == "stream" | ||
and last.output_type == "stream" | ||
and last.name == output.name | ||
): | ||
last.text += output.text | ||
|
||
else: | ||
new_outputs.append(output) | ||
last = output | ||
|
||
# process \r characters | ||
for output in new_outputs: | ||
if output.output_type == "stream" and "\r" in output.text: | ||
output.text = cr_pat.sub("", output.text) | ||
|
||
cell.outputs = new_outputs | ||
return cell, resources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters