##// END OF EJS Templates
jupyter-rendering: added a custom preprocessor to implement Javascript object...
marcink -
r1495:4a485783 default
parent child Browse files
Show More
@@ -268,16 +268,31 b' class MarkupRenderer(object):'
268 raise
268 raise
269
269
270 @classmethod
270 @classmethod
271 def jupyter(cls, source):
271 def jupyter(cls, source, safe=True):
272 from rhodecode.lib import helpers
272 from rhodecode.lib import helpers
273
274 from traitlets.config import Config
273 import nbformat
275 import nbformat
274 from nbconvert import HTMLExporter
276 from nbconvert import HTMLExporter
275 from traitlets.config import Config
277 from nbconvert.preprocessors import Preprocessor
276
278
277 class CustomHTMLExporter(HTMLExporter):
279 class CustomHTMLExporter(HTMLExporter):
278 def _template_file_default(self):
280 def _template_file_default(self):
279 return 'basic'
281 return 'basic'
280
282
283 class Sandbox(Preprocessor):
284
285 def preprocess(self, nb, resources):
286 sandbox_text = 'SandBoxed(IPython.core.display.Javascript object)'
287 for cell in nb['cells']:
288 if safe and 'outputs' in cell:
289 for cell_output in cell['outputs']:
290 if 'data' in cell_output:
291 if 'application/javascript' in cell_output['data']:
292 cell_output['data']['text/plain'] = sandbox_text
293 cell_output['data'].pop('application/javascript', None)
294 return nb, resources
295
281 def _sanitize_resources(resources):
296 def _sanitize_resources(resources):
282 """
297 """
283 Skip/sanitize some of the CSS generated and included in jupyter
298 Skip/sanitize some of the CSS generated and included in jupyter
@@ -295,6 +310,7 b' class MarkupRenderer(object):'
295
310
296 def as_html(notebook):
311 def as_html(notebook):
297 conf = Config()
312 conf = Config()
313 conf.CustomHTMLExporter.preprocessors = [Sandbox]
298 html_exporter = CustomHTMLExporter(config=conf)
314 html_exporter = CustomHTMLExporter(config=conf)
299
315
300 (body, resources) = html_exporter.from_notebook_node(notebook)
316 (body, resources) = html_exporter.from_notebook_node(notebook)
General Comments 0
You need to be logged in to leave comments. Login now