Convert a notebook into a document

Links: notebook, html ., PDF, python, slides ., GitHub

First, we need to retrieve the notebook name (see How to I get the current IPython Notebook name):

var kernel = IPython.notebook.kernel;
var body = document.body,
    attribs = body.attributes;
var command = "theNotebook = " + "'"+attribs['data-notebook-name'].value+"'";
<IPython.core.display.Javascript object>
if "theNotebook" in locals():
    a="pas trouvé"

On Windows, you might need to execute the following trick (see Pywin32 does not find its DLL).

from pyquickhelper.helpgen.utils_pywin32 import import_pywin32
<module 'win32com' from 'C:\Python34_x64\lib\site-packages\win32com\'>

Then, we call the following code:

from nbconvert import HTMLExporter
exportHtml = HTMLExporter()
if a != "pas trouvé":
    body,resources = exportHtml.from_filename(theNotebook)
    with open("conv_notebook.html","w",encoding="utf8") as f : f.write(body)

We can do it with the RST format (see RSTExporter).

from nbconvert import RSTExporter
exportRst = RSTExporter()
if a != "pas trouvé":
    body,resources = exportRst.from_filename(theNotebook)
    with open("conv_notebook.rst","w",encoding="utf8") as f : f.write(body)

If you need to add custom RST instructions, you could add HTML comments:

<!--RST ..index:: conversion,nbconvert !RST-->

And write custom code to add it to your RST file.

Finally, if you want to retrieve the download a local file such as the RST conversion for example:

from IPython.display import FileLink