diff options
author | Kapileshwar Singh <kpsingh@google.com> | 2016-04-26 01:04:00 +0200 |
---|---|---|
committer | Javi Merino <javi.merino@arm.com> | 2016-04-26 18:09:04 +0100 |
commit | 7712c8f14d2bac1d2fbdbe5ed78449d71fb75a96 (patch) | |
tree | c90bff546926af381a4a11ff316b30bfe75295c0 /trappy/nbexport | |
parent | 39e9830783904ca716793d3ba8e9b733a1af89ba (diff) | |
download | trappy-7712c8f14d2bac1d2fbdbe5ed78449d71fb75a96.tar.gz |
trappy: Add custom HTML exporter for TRAPpy IPython Notebooks
This allows the notebooks to be exported as:
jupyter nbconvert -—to trappy.nbexport.HTML doc/InteractivePlotter.ipynb
Signed-off-by: Kapileshwar Singh <kpsingh@google.com>
Diffstat (limited to 'trappy/nbexport')
-rw-r--r-- | trappy/nbexport/__init__.py | 29 | ||||
-rw-r--r-- | trappy/nbexport/preprocessors.py | 94 |
2 files changed, 123 insertions, 0 deletions
diff --git a/trappy/nbexport/__init__.py b/trappy/nbexport/__init__.py new file mode 100644 index 0000000..0293318 --- /dev/null +++ b/trappy/nbexport/__init__.py @@ -0,0 +1,29 @@ +# Copyright 2015-2016 ARM Limited +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +"""HTML Exporter for TRAPPY plotter data. This allows +* Custom Preprocessing +""" + +from nbconvert.exporters.html import HTMLExporter +from trappy.nbexport.preprocessors import TrappyPlotterPreprocessor + + +class HTML(HTMLExporter): + """HTML Exporter class for TRAPpy notebooks""" + + def __init__(self, **kwargs): + super(HTML, self).__init__(**kwargs) + self.register_preprocessor(TrappyPlotterPreprocessor, enabled=True) diff --git a/trappy/nbexport/preprocessors.py b/trappy/nbexport/preprocessors.py new file mode 100644 index 0000000..096dab6 --- /dev/null +++ b/trappy/nbexport/preprocessors.py @@ -0,0 +1,94 @@ +# Copyright 2015-2016 ARM Limited +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +"""Preprocessor to remove Marked Lines from IPython Output Cells""" + + +from nbconvert.preprocessors import Preprocessor +import os +import re + +REMOVE_START = '/* TRAPPY_PUBLISH_REMOVE_START */' +REMOVE_STOP = '/* TRAPPY_PUBLISH_REMOVE_STOP */' +REMOVE_LINE = '/* TRAPPY_PUBLISH_REMOVE_LINE */' +IMPORT_SCRIPT = r'/\* TRAPPY_PUBLISH_IMPORT = "([^"]+)" \*/' +SOURCE_LIB = r'<!-- TRAPPY_PUBLISH_SOURCE_LIB = "([^"]+)" -->' + + +class TrappyPlotterPreprocessor(Preprocessor): + """Preprocessor to remove Marked Lines from IPython Output Cells""" + + def __init__(self, *args, **kwargs): + super(Preprocessor, self).__init__(*args, **kwargs) + self.inlined_files = [] + self.sourced_libs = [] + + def preprocess_cell(self, cell, resources, cell_index): + """Check if cell has text/html output and filter it""" + + if cell.cell_type == 'code' and hasattr(cell, "outputs"): + for output in cell.outputs: + if output.output_type == "display_data" and \ + hasattr( output.data, "text/html"): + filtered = self.filter_output(output.data["text/html"]) + output.data["text/html"] = filtered + return cell, resources + + def filter_output(self, output): + """Function to remove marked lines""" + + lines = output.split('\n') + + final_lines = [] + multi_line_remove = False + for line in lines: + if REMOVE_START in line: + multi_line_remove = True + continue + if REMOVE_STOP in line: + multi_line_remove = False + continue + if multi_line_remove or REMOVE_LINE in line: + continue + + import_match = re.search(IMPORT_SCRIPT, line) + if import_match: + trappy_base = os.path.dirname(os.path.dirname(__file__)) + import_file = os.path.join(trappy_base, import_match.group(1)) + if import_file in self.inlined_files: + continue + + with open(import_file) as fin: + final_lines.extend([l[:-1] for l in fin.readlines()]) + + self.inlined_files.append(import_file) + continue + + source_match = re.search(SOURCE_LIB, line) + if source_match: + lib_url = source_match.group(1) + if lib_url in self.sourced_libs: + continue + + scl = '<script src="{}" type="text/javascript" charset="utf-8"></script>'.\ + format(lib_url) + final_lines.append(scl) + + self.sourced_libs.append(lib_url) + continue + + final_lines.append(line) + + return '\n'.join(final_lines) |