Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
248 lines (200 sloc) 7.66 KB
# -*- coding: utf-8 -*-
"""
@file
@brief Direct calls to IPython API without running a command line
"""
import os
from io import StringIO
def get_exporter(format, add_writer=False):
"""
Returns the :epkg:`IPython` exporter associated to a format.
@param format string (see below)
@param add_writer add writer as well
@return class
Available formats: *slides*, *pdf*, *latex*, *markdown*, *html*,
*rst*, *python*, *notebook*, *template*.
..versionchanged:: 1.7
Add parameter *add_writer*.
"""
if format == "python":
from nbconvert import PythonExporter
exp = PythonExporter
elif format == "slides":
from nbconvert import SlidesExporter
exp = SlidesExporter
elif format == "html":
from nbconvert import HTMLExporter
exp = HTMLExporter
elif format == "pdf":
from nbconvert import PDFExporter
exp = PDFExporter
elif format == "template":
from nbconvert import TemplateExporter
exp = TemplateExporter
elif format == "markdown":
from nbconvert import MarkdownExporter
exp = MarkdownExporter
elif format == "notebook":
from nbconvert import NotebookExporter
exp = NotebookExporter
elif format == "rst":
from .notebook_exporter import UpgradedRSTExporter
exp = UpgradedRSTExporter
elif format == "lagex":
from nbconvert import LatexExporter
exp = LatexExporter
else:
form = "slides, pdf, latex, markdown, html, rst, python, notebook, template"
raise ValueError(
"unexpected format: {0}, it should be in:\n{1}".format(format, form))
if add_writer:
from nbconvert.writers import FilesWriter
return exp, FilesWriter
else:
return exp
def nb2slides(nb_file, outfile, add_tag=True):
"""
Converts a notebook into slides, it copies
:epkg:`reveal.js` if not present in the folder of the output.
@param nb_file notebook file or a stream or a @see fn read_nb
@param outfile output file (a string)
@param add_tag call @see me add_tag_slide
@return impacted files
See `How do I convert a IPython Notebook into a Python file via commandline?
<http://stackoverflow.com/questions/17077494/how-do-i-convert-a-ipython-notebook-into-a-python-file-via-commandline>`_
.. exref::
:title: Convert a notebook into slides
By default, the function automatically adds sections if there is none
and it copies the javascript from reveal.js at the right place.
::
from pyquickhelper.helpgen import nb2slides
nb2slides("nb.ipynb", "convert.slides.html")
"""
from ..ipythonhelper import NotebookRunner, read_nb
from .post_process import post_process_slides_output
if isinstance(nb_file, NotebookRunner):
nb = nb_file.nb
else:
nbr = read_nb(nb_file, kernel=False)
nb = nbr.nb
if add_tag:
run = NotebookRunner(nb, kernel=False)
run.add_tag_slide()
nb = run.nb
exporter = get_exporter("slides")()
source = exporter.from_notebook_node(nb)[0]
with open(outfile, 'w+', encoding="utf8") as fh:
fh.writelines(source)
# post_processing
post_process_slides_output(outfile, False, False, False, False)
res = [outfile]
# we copy javascript dependencies, reveal.js
dirname = os.path.dirname(outfile)
reveal = os.path.join(dirname, "reveal.js")
if not os.path.exists(reveal):
from .install_js_dep import install_javascript_tools
cp = install_javascript_tools(None, dest=dirname)
res.extend(cp)
return res
def _nbpresent_export(ipynb=None, outfile=None, out_format=None, verbose=None):
if out_format in ["pdf"]:
raise NotImplementedError(
"format {0} is not allowed".format(out_format))
if out_format in ["html"]:
from nbpresent.exporters.html import PresentExporter as Exporter
from nbpresent.exporters import APP_ROOT
exp = Exporter(
template_file="nbpresent",
template_path=[os.path.join(APP_ROOT, "templates")]
)
output = exp.from_file(ipynb)[0]
if outfile is None:
return output
else:
with open(outfile, "w", encoding="utf-8") as fp:
fp.write(output)
return output
def nb2present(nb_file, outfile, add_tag=True):
"""
Converts a notebooks into slides with *format=present*.
@param nb_file notebook file or a stream or a @see fn read_nb
@param outfile output file (a string)
@param add_tag call @see me add_tag_slide
@return impacted files
"""
from ..ipythonhelper import NotebookRunner, read_nb
if isinstance(nb_file, NotebookRunner):
nb = nb_file.nb
else:
nbr = read_nb(nb_file, kernel=False)
nb = nbr.nb
run = NotebookRunner(nb, kernel=False)
if add_tag:
run.add_tag_slide()
# to be implemented
js = run.to_json()
st = StringIO(js)
content = _nbpresent_export(st, outfile, out_format="html")
if outfile is None:
outfile = content
# post_processing
from .post_process import post_process_slides_output
post_process_slides_output(outfile, False, False, False, False)
res = [outfile]
return res
def nb2html(nb_file, outfile, exc=True):
"""
Converts a notebook into HTML.
@param nb_file notebook file or a stream or a @see fn read_nb
@param outfile output file (a string)
@param exc raises an exception (True) or a warning (False)
@return impacted files
"""
from ..ipythonhelper import NotebookRunner, read_nb
if isinstance(nb_file, NotebookRunner):
nb = nb_file.nb
else:
nbr = read_nb(nb_file, kernel=False)
nb = nbr.nb
exporter = get_exporter("html")()
source = exporter.from_notebook_node(nb)[0]
with open(outfile, 'w+', encoding="utf8") as fh:
fh.writelines(source)
# post_processing
from .post_process import post_process_html_output
post_process_html_output(outfile, False, False, False, False, exc=exc)
res = [outfile]
return res
def nb2rst(nb_file, outfile, exc=True, post_process=True):
"""
Converts a notebook into RST.
@param nb_file notebook file or a stream or a @see fn read_nb
@param outfile output file (a string)
@param exc raises an exception (True) or a warning (False)
@param post_process calls @see fn post_process_rst_output
@return impacted files
"""
from ..ipythonhelper import NotebookRunner, read_nb
if isinstance(nb_file, NotebookRunner):
nb = nb_file.nb
else:
nbr = read_nb(nb_file, kernel=False)
nb = nbr.nb
exp_class, writer_class = get_exporter("rst", add_writer=True)
exporter = exp_class()
writer = writer_class()
unique_key = os.path.splitext(os.path.split(outfile)[-1])[0]
source, meta = exporter.from_notebook_node(
nb, resources=dict(unique_key=unique_key))
name, ext = os.path.splitext(outfile)
if ext != '.rst':
raise ValueError("'{0}' should have extension '.rst'".format(outfile))
writer.build_directory = os.path.dirname(outfile)
writer.write(source, meta, notebook_name=name)
# post_processing
if post_process:
from .post_process import post_process_rst_output
post_process_rst_output(outfile, False, False,
False, False, False, exc=exc)
res = [outfile]
return res
You can’t perform that action at this time.