def render_ipynb(full_path): """ Render a given ipynb file """ exporter = HTMLExporter() with open(full_path, encoding='utf-8') as file_handle: html, res = exporter.from_file(file_handle) return Response(html, mimetype='text/html')
def render_ipynb(full_path, format): """ Render a given ipynb file """ exporter = HTMLExporter() with open(full_path, encoding="utf-8") as file_handle: html, _ = exporter.from_file(file_handle) return Response(html, mimetype="text/html")
def generate_exercises(): p = Path(*EXERCISES_DIR) exporter = HTMLExporter() exporter.register_preprocessor(ClearOutputPreprocessor(), enabled=True) for exercise in p.iterdir(): if exercise.suffix == '.ipynb': html, _ = exporter.from_file(exercise.open()) with open(exercise.with_suffix('.html').name, 'w') as f: f.write(html)
def nb2html(nb_path, start=0, end=None, execute=False, kernel_name=""): """Convert a notebook Returns ------- HTML content """ logger.info(f"Converting notebook: {nb_path}") app = get_nbconvert_app(start=start, end=end, execute=execute, kernel_name=kernel_name) # Use the templates included in this package template_file = "mkdocs_html/notebook.html.j2" extra_template_paths = [os.path.join(THIS_DIR, "templates")] # Customize NBConvert App preprocessors_ = [SubCell] filters = { "highlight_code": custom_highlight_code, "markdown2html": custom_markdown2html, } exporter = HTMLExporter( config=app.config, template_file=template_file, extra_template_paths=extra_template_paths, preprocessors=preprocessors_, filters=filters, ) _, extension = os.path.splitext(nb_path) if extension == ".py": nb = jupytext.read(nb_path) nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb")) html, info = exporter.from_file(nb_file) else: html, info = exporter.from_filename(nb_path) return html
def nb2html(nb_path, execute=False, kernel_name="", theme=None, start=0, end=None): """ Convert a notebook to HTML Arguments --------- nb_path: str Path to the notebook execute: bool Whether to execute the notebook kernel_name: str Name of the kernel to use theme: str Name of the theme to use (default: light) (options: light or dark) start: int Start cell number end: int End cell number Returns ------- HTML content """ logger.info(f"Converting notebook (execute={execute}): {nb_path}") global cell_id, kernel_lang cell_id = 0 # Reset the cell id app = get_nbconvert_app(execute=execute, kernel_name=kernel_name, start=start, end=end) # Use the templates included in this package template_file = "mkdocs_html/notebook.html.j2" extra_template_paths = [settings.templates_dir] # Customize NBConvert App preprocessors_ = [SubCell] filters = { "highlight_code": custom_highlight_code, "markdown2html": custom_markdown2html, } exporter = HTMLExporter( config=app.config, template_file=template_file, extra_template_paths=extra_template_paths, preprocessors=preprocessors_, filters=filters, theme=theme, ) _, extension = os.path.splitext(nb_path) if extension == ".py": nb = jupytext.read(nb_path) nb_file = io.StringIO(jupytext.writes(nb, fmt="ipynb")) content, resources = exporter.from_file(nb_file) else: try: with open(nb_path, "r", encoding="utf-8") as f: nb_json = json.load(f) kernel_lang = nb_json["metadata"]["kernelspec"]["language"] except KeyError: pass content, resources = exporter.from_filename(nb_path) return content
# -*- coding: utf-8 -*- from __future__ import print_function import sys reload(sys) sys.setdefaultencoding("utf-8") from nbconvert.exporters import HTMLExporter from traitlets.config import Config config = Config({ "HTMLExporter": { "template_file": "basic" }, 'NbConvertBase': { 'display_data_priority': [ 'text/html', 'text/markdown', 'application/pdf', 'image/svg+xml', 'text/latex', 'image/png', 'image/jpeg', 'text/plain', ] } }) ex = HTMLExporter(config=config) html, extra = ex.from_file(sys.stdin) sys.stdout.write(html)
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import sys reload(sys) sys.setdefaultencoding("utf-8") from nbconvert.exporters import HTMLExporter from traitlets.config import Config config = Config({ "HTMLExporter": {"template_file": "basic"}, 'NbConvertBase': { 'display_data_priority': [ 'text/html', 'text/markdown', 'application/pdf', 'image/svg+xml', 'text/latex', 'image/png', 'image/jpeg', 'text/plain', ] } }) ex = HTMLExporter(config=config) html, extra = ex.from_file(sys.stdin) sys.stdout.write(html)