[IPython-dev] Making custom converter/template with nbconvert

Wes Turner wes.turner at gmail.com
Thu Dec 18 11:07:33 EST 2014


Great idea!

Are there imports which must/should be repeated in each output file?

What is the advantage of splitting one notebook into multiple output
documents?
(Why not just create multiple documents?)

It looks like:

 ipython/IPython/nbconvert/exporters/htmlexporter.py:HTMLExporter

https://github.com/ipython/ipython/blob/master/IPython/nbconvert/exporters/html.py#L61
 extends
 ipython/IPython/nbconvert/exporters/templateexporter.py:TemplateExporter
 extends
 ipython/IPython/nbconvert/exporters/exporter.py:Exporter

https://github.com/ipython/ipython/blob/master/IPython/nbconvert/exporters/exporter.py#L45

If each document is to be the same, you probably don't need to reimplement
_load_template.

I could be wrong, but it would probably be easiest to split nb_copy by
iterating through the notebook cells.


https://github.com/ipython/ipython/blob/master/IPython/nbconvert/exporters/templateexporter.py#L196


class EDXExporter(HTMLExporter):
    def from_notebook_node(nb, resources, **kw):
        nb_copy, resources = super(EDXExporter,
self).from_notebook_node(nb, resources, **kw)
        output = collections.OrderedDict()
        chunks = split_notebook_by_headings?(nb_copy)
        for section in chunks:
            output[section['name']] = self.template.render(nb=nb_copy,
resources=resources)
        return output, resources

https://github.com/ipython/ipython/blob/master/IPython/nbconvert/nbconvertapp.py#L308

https://github.com/ipython/ipython/blob/master/IPython/nbconvert/writers/files.py#L46

class MultipleFilesWriter(FilesWriter):
    def write(self, output, resources, notebook_name=None, **kw):
        # ...
        for section_name, _output in output.iteritems():
             # ...


On Thu, Dec 18, 2014 at 8:49 AM, Rafał Skolasiński <rjtskolasinski at gmail.com
> wrote:
>
> Hi Thomas,
>
> Yes, that sounds like a logical way. But I still have few question about
> how to do it.
>
> First, do I also need to have custom preprocessor? At which point of
> conversion it will be the most convenient to make changes?
>
> Should my custom exporter class inherit from a TemplateExporter? Or maybe
> separation into files should happen at this level?
>
> And last, once I will have my exporter class will it be enough to specify
> which exporter should be used in config file and provide config to
> nbconvert via --config mycfg.py ?
>
> Thanks for all your help!
>
> Best,
> Rafal
>
> P.S. If there are any useful guides or FAQs about writing custom
> converters that would be very helpful. I looked for some on my own but
> didn't have much luck.
>
> 2014-12-16 23:13 GMT+01:00 Thomas Kluyver <takowl at gmail.com>:
>
>> Hi Rafał,
>>
>> On 16 December 2014 at 13:40, Rafał Skolasiński <rjtskolasinski at gmail.com
>> > wrote:
>>
>>> Unfortunately each unit must go into separate html file and here comes
>>> my first question: what is the easiest way to force nbconverter to export
>>> one notebook into few html files (let's say Heading 1 will start every unit
>>> and at the same time will be the name of it)?
>>
>>
>> I think you will need to make a custom exporter - nbconvert supports
>> different output formats with exporter classes, which you can find here:
>> https://github.com/ipython/ipython/tree/master/IPython/nbconvert/exporters
>>
>> Your case will be a bit trickier than most of those examples, because all
>> the TemplateExporter subclasses assume that they are writing a single main
>> output file. But there's no reason that an exporter class couldn't write
>> several files from one notebook.
>>
>> Thomas
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141218/5b5924eb/attachment.html>


More information about the IPython-dev mailing list