def _render_notebook_from_source( self, ipynb_source, indices=None, clear_output=False, clear_markdown=False, **kwargs): # type: (Text, Optional[Any], Optional[bool], Optional[bool], **Any) -> Text """ Get HTML format of ipython notebook so as to be rendered in RELATE flow pages. :param ipynb_source: the :class:`text` read from a ipython notebook. :param indices: a :class:`list` instance, 0-based indices of notebook cells which are expected to be rendered. :param clear_output: a :class:`bool` instance, indicating whether existing execution output of code cells should be removed. :param clear_markdown: a :class:`bool` instance, indicating whether markdown cells will be ignored.. :return: """ import nbformat from nbformat.reader import parse_json nb_source_dict = parse_json(ipynb_source) if indices: nb_source_dict.update( {"cells": [nb_source_dict["cells"][idx] for idx in indices]}) if clear_markdown: nb_source_dict.update( {"cells": [cell for cell in nb_source_dict["cells"] if cell['cell_type'] != "markdown"]}) nb_source_dict.update({"cells": nb_source_dict["cells"]}) import json ipynb_source = json.dumps(nb_source_dict) notebook = nbformat.reads(ipynb_source, as_version=4) from traitlets.config import Config c = Config() # This is to prevent execution of arbitrary code from note book c.ExecutePreprocessor.enabled = False if clear_output: c.ClearOutputPreprocessor.enabled = True c.CSSHTMLHeaderPreprocessor.enabled = False c.HighlightMagicsPreprocessor.enabled = False import os # Place the template in course template dir import course template_path = os.path.join( os.path.dirname(course.__file__), "templates", "course", "jinja2") c.TemplateExporter.template_path.append(template_path) from nbconvert import HTMLExporter html_exporter = HTMLExporter( config=c, template_file="nbconvert_template.tpl" ) (body, resources) = html_exporter.from_notebook_node(notebook) return "<div class='relate-notebook-container'>%s</div>" % body
def _render_notebook_from_source(self, ipynb_source, indices=None, clear_output=False, clear_markdown=False, **kwargs): # type: (Text, Optional[Any], Optional[bool], Optional[bool], **Any) -> Text """ Get HTML format of ipython notebook so as to be rendered in RELATE flow pages. :param ipynb_source: the :class:`text` read from a ipython notebook. :param indices: a :class:`list` instance, 0-based indices of notebook cells which are expected to be rendered. :param clear_output: a :class:`bool` instance, indicating whether existing execution output of code cells should be removed. :param clear_markdown: a :class:`bool` instance, indicating whether markdown cells will be ignored.. :return: """ import nbformat from nbformat.reader import parse_json nb_source_dict = parse_json(ipynb_source) if indices: nb_source_dict.update( {"cells": [nb_source_dict["cells"][idx] for idx in indices]}) if clear_markdown: nb_source_dict.update({ "cells": [ cell for cell in nb_source_dict["cells"] if cell['cell_type'] != "markdown" ] }) nb_source_dict.update({"cells": nb_source_dict["cells"]}) import json ipynb_source = json.dumps(nb_source_dict) notebook = nbformat.reads(ipynb_source, as_version=4) from traitlets.config import Config c = Config() # This is to prevent execution of arbitrary code from note book c.ExecutePreprocessor.enabled = False if clear_output: c.ClearOutputPreprocessor.enabled = True c.CSSHTMLHeaderPreprocessor.enabled = False c.HighlightMagicsPreprocessor.enabled = False import os # Place the template in course template dir import course template_path = os.path.join(os.path.dirname(course.__file__), "templates", "course", "jinja2") c.TemplateExporter.template_path.append(template_path) from nbconvert import HTMLExporter html_exporter = HTMLExporter(config=c, template_file="nbconvert_template.tpl") (body, resources) = html_exporter.from_notebook_node(notebook) return "<div class='relate-notebook-container'>%s</div>" % body