diff options
-rw-r--r-- | trappy/__init__.py | 1 | ||||
-rw-r--r-- | trappy/nbexport/__init__.py | 29 | ||||
-rw-r--r-- | trappy/nbexport/preprocessors.py | 94 | ||||
-rw-r--r-- | trappy/plotter/EventPlot.py | 15 | ||||
-rw-r--r-- | trappy/plotter/ILinePlotGen.py | 15 | ||||
-rw-r--r-- | trappy/plotter/IPythonConf.py | 16 |
6 files changed, 158 insertions, 12 deletions
diff --git a/trappy/__init__.py b/trappy/__init__.py index 7ea1a9d..f8a9581 100644 --- a/trappy/__init__.py +++ b/trappy/__init__.py @@ -34,6 +34,7 @@ except ImportError: pass from trappy.dynamic import register_dynamic_ftrace, register_ftrace_parser, \ unregister_ftrace_parser +import trappy.nbexport # We define unregister_dynamic_ftrace() because it undoes what # register_dynamic_ftrace(). Internally it does exactly the same as 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) diff --git a/trappy/plotter/EventPlot.py b/trappy/plotter/EventPlot.py index c0366b1..cd53778 100644 --- a/trappy/plotter/EventPlot.py +++ b/trappy/plotter/EventPlot.py @@ -186,8 +186,14 @@ class EventPlot(AbstractDataPlotter): def _init_html(self): """Initialize HTML for the plot""" - div_js = """ + div_js = '' + for url in [IPythonConf.D3_PLOTTER_URL, IPythonConf.D3_TIP_URL]: + div_js += '<!-- TRAPPY_PUBLISH_SOURCE_LIB = "{}" -->\n'.format(url) + + div_js += """ <script> + /* TRAPPY_PUBLISH_IMPORT = "plotter/js/EventPlot.js" */ + /* TRAPPY_PUBLISH_REMOVE_START */ var req = require.config( { paths: { @@ -208,18 +214,19 @@ class EventPlot(AbstractDataPlotter): } } }); + /* TRAPPY_PUBLISH_REMOVE_STOP */ """ div_js += "var data = {};\n".format(self._data) div_js += """ - req(["require", "EventPlot"], function() { + req(["require", "EventPlot"], function() { /* TRAPPY_PUBLISH_REMOVE_LINE */ EventPlot.generate('""" + self._fig_name + """', '""" + IPythonConf.add_web_base("") + """', data); - }); + }); /* TRAPPY_PUBLISH_REMOVE_LINE */ </script> """ self._html.append( - '<div id="{}" class="eventplot">{}</div>'.format(self._fig_name, + '<div id="{}" class="eventplot">\n{}</div>'.format(self._fig_name, div_js)) def _add_css(self): diff --git a/trappy/plotter/ILinePlotGen.py b/trappy/plotter/ILinePlotGen.py index e87a53f..13860de 100644 --- a/trappy/plotter/ILinePlotGen.py +++ b/trappy/plotter/ILinePlotGen.py @@ -50,8 +50,16 @@ class ILinePlotGen(object): def _add_graph_cell(self, fig_name): """Add a HTML table cell to hold the plot""" - div_js = """ + div_js = '' + lib_urls = [IPythonConf.DYGRAPH_COMBINED_URL, IPythonConf.DYGRAPH_SYNC_URL, + IPythonConf.UNDERSCORE_URL] + for url in lib_urls: + div_js += '<!-- TRAPPY_PUBLISH_SOURCE_LIB = "{}" -->\n'.format(url) + + div_js += """ <script> + /* TRAPPY_PUBLISH_IMPORT = "plotter/js/ILinePlot.js" */ + /* TRAPPY_PUBLISH_REMOVE_START */ var ilp_req = require.config( { paths: { @@ -70,9 +78,10 @@ class ILinePlotGen(object): } } }); - ilp_req(["require", "ILinePlot"], function() { + /* TRAPPY_PUBLISH_REMOVE_STOP */ + ilp_req(["require", "ILinePlot"], function() { /* TRAPPY_PUBLISH_REMOVE_LINE */ ILinePlot.generate('""" + fig_name + """', '""" + IPythonConf.add_web_base("") + """'); - }); + }); /* TRAPPY_PUBLISH_REMOVE_LINE */ </script> """ diff --git a/trappy/plotter/IPythonConf.py b/trappy/plotter/IPythonConf.py index 9e170a9..05f1a22 100644 --- a/trappy/plotter/IPythonConf.py +++ b/trappy/plotter/IPythonConf.py @@ -21,15 +21,21 @@ import os import shutil from distutils.version import StrictVersion as V +D3_PLOTTER_URL = "https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" +D3_TIP_URL = "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js" +DYGRAPH_COMBINED_URL = "http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js" +DYGRAPH_SYNC_URL = "http://dygraphs.com/extras/synchronizer.js" +UNDERSCORE_URL = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js" + IPLOT_RESOURCES = { "ILinePlot": [ - "http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js", + DYGRAPH_COMBINED_URL, "js/ILinePlot.js", - "http://dygraphs.com/extras/synchronizer.js", - "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"], + DYGRAPH_SYNC_URL, + UNDERSCORE_URL], "EventPlot": [ - "https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js", - "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js", + D3_PLOTTER_URL, + D3_TIP_URL, "js/EventPlot.js", "css/EventPlot_help.jpg"]} |