Пример #1
0
 def from_notebook_node(self, nb, resources=None, **kw):
     langinfo = nb.metadata.get('language_info', {})
     lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
     self.register_filter('highlight_code',
                          Highlight2HTML(pygments_lexer=lexer, parent=self))
     return super(HTMLExporter,
                  self).from_notebook_node(nb, resources, **kw)
Пример #2
0
    async def generate_from_notebook_node(self,
                                          nb,
                                          resources=None,
                                          extra_context={},
                                          **kw):
        # this replaces from_notebook_node, but calls template.generate instead of template.render
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        highlight_code = self.filters.get(
            'highlight_code', Highlight2HTML(pygments_lexer=lexer,
                                             parent=self))
        self.register_filter('highlight_code', highlight_code)

        # NOTE: we don't call HTML or TemplateExporter' from_notebook_node
        nb_copy, resources = super(TemplateExporter, self).from_notebook_node(
            nb, resources, **kw)
        resources.setdefault('raw_mimetypes', self.raw_mimetypes)
        resources['global_content_filter'] = {
            'include_code': not self.exclude_code_cell,
            'include_markdown': not self.exclude_markdown,
            'include_raw': not self.exclude_raw,
            'include_unknown': not self.exclude_unknown,
            'include_input': not self.exclude_input,
            'include_output': not self.exclude_output,
            'include_input_prompt': not self.exclude_input_prompt,
            'include_output_prompt': not self.exclude_output_prompt,
            'no_prompt': self.exclude_input_prompt
            and self.exclude_output_prompt,
        }

        async for output in self.template.generate_async(nb=nb_copy,
                                                         resources=resources,
                                                         **extra_context):
            yield (output, resources)
Пример #3
0
 def from_notebook_node(self, nb, resources=None, **kw):
     '''So we could use our own template filters'''
     langinfo = nb.metadata.get('language_info', {})
     lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
     highlight = Highlight2HTML(pygments_lexer=lexer, parent=self)
     self.register_filter('highlight_code', highlight)
     self.register_filter('markdown2html', convert_markdown)
     return super(HTMLExporter,
                  self).from_notebook_node(nb, resources, **kw)
Пример #4
0
    def from_notebook_node(self, nb, resources=None, **kw):
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        highlight_code = self.filters.get('highlight_code', Highlight2HTML(pygments_lexer=lexer, parent=self))

        filter_data_type = WidgetsDataTypeFilter(notebook_metadata=self._nb_metadata, parent=self, resources=resources)

        self.register_filter('highlight_code', highlight_code)
        self.register_filter('filter_data_type', filter_data_type)
        return super().from_notebook_node(nb, resources, **kw)
Пример #5
0
    def from_notebook_node(self, nb, resources=None, **kw):
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        highlight_code = self.filters.get(
            'highlight_code', Highlight2HTML(pygments_lexer=lexer,
                                             parent=self))
        self.register_filter('highlight_code', highlight_code)

        output, resources = super(HTMLExporter, self).from_notebook_node(
            nb, resources, **kw)
        att_output = self.process_attachments(nb, output)
        return att_output, resources
Пример #6
0
    def from_notebook_node(self, nb, resources=None, **kw):
        # ********************************************** #
        # From HTMLExporter
        # https://github.com/jupyter/nbconvert/blob/master/nbconvert/exporters/html.py
        langinfo = nb.metadata.get("language_info", {})
        lexer = langinfo.get("pygments_lexer", langinfo.get("name", None))
        highlight_code = self.filters.get(
            "highlight_code", Highlight2HTML(pygments_lexer=lexer, parent=self)
        )
        self.register_filter("highlight_code", highlight_code)
        # ********************************************** #

        # ***************** CUSTOM CODE **************** #
        return super()._from_notebook_node_override(nb, resources, **kw)
    def from_notebook_node(self, nb, resources=None, **kw):
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        self.register_filter('highlight_code',
                             Highlight2HTML(pygments_lexer=lexer, parent=self))
        lenvshtmlpreprocessor = LenvsHTMLPreprocessor()

        self.register_preprocessor(lenvshtmlpreprocessor, enabled=True)
        self._init_preprocessors()
        nb, resources = lenvshtmlpreprocessor(nb, resources)
        output, resources = super(LenvsHTMLExporter, self).from_notebook_node(
            nb, resources, **kw)
        # postout = postprocess(output)
        # print(postout[0:200]) #WORKS
        return output, resources
Пример #8
0
    async def generate_from_notebook_node(self,
                                          nb,
                                          resources=None,
                                          extra_context={},
                                          **kw):
        # this replaces from_notebook_node, but calls template.generate instead of template.render
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        highlight_code = self.filters.get(
            'highlight_code', Highlight2HTML(pygments_lexer=lexer,
                                             parent=self))
        self.register_filter('highlight_code', highlight_code)

        # NOTE: we don't call HTML or TemplateExporter' from_notebook_node
        nb_copy, resources = super(TemplateExporter, self).from_notebook_node(
            nb, resources, **kw)
        resources.setdefault('raw_mimetypes', self.raw_mimetypes)
        resources['global_content_filter'] = {
            'include_code': not self.exclude_code_cell,
            'include_markdown': not self.exclude_markdown,
            'include_raw': not self.exclude_raw,
            'include_unknown': not self.exclude_unknown,
            'include_input': not self.exclude_input,
            'include_output': not self.exclude_output,
            'include_input_prompt': not self.exclude_input_prompt,
            'include_output_prompt': not self.exclude_output_prompt,
            'no_prompt': self.exclude_input_prompt
            and self.exclude_output_prompt,
        }

        # Jinja with Python3.5 does not support async (generators), which
        # means that it's not async all the way down. Which means that we
        # cannot use coroutines for the cell_generator, and that they will
        # block the IO loop. In that case we will run the iterator in a
        # thread instead.

        @async_generator_to_thread
        @async_generator
        async def async_jinja_generator():
            # Top level variables are passed to the template_exporter here.
            for output in self.template.generate(nb=nb_copy,
                                                 resources=resources,
                                                 **extra_context):
                await yield_((output, resources))

        async for output, resources in async_jinja_generator():
            await yield_((output, resources))
Пример #9
0
    def from_notebook_node(self, nb, resources=None, **kw):
        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        self.register_filter('highlight_code',
                             Highlight2HTML(pygments_lexer=lexer, parent=self))
        self.environment.globals['include_latexdefs'] = include_latexdefs
        self.environment.globals['include_userenvs_cfg'] = include_userenvs_cfg
        lenvshtmlpreprocessor = LenvsHTMLPreprocessor()

        self.register_preprocessor(lenvshtmlpreprocessor, enabled=True)
        self._init_preprocessors()
        nb, resources = lenvshtmlpreprocessor(nb, resources)
        output, resources = super(LenvsSlidesExporter,
                                  self).from_notebook_node(
                                      nb, resources, **kw)

        return output, resources
Пример #10
0
    def from_notebook_node(self, nb, resources=None, **kw):
        if not hasattr(self, "_ipynb_file"):
            self._ipynb_file = None
        url_root = "http://{}".format("localhost:{}".format(self.port))
        output_root = Path(resources["output_files_dir"])
        lab_path = self.lab_path()
        static_path = Path(notebook.__file__).parent / "static"
        nb_names = [
            resources["metadata"]["name"]
        ] + [
            str(ef)[:-6] for ef in self.extra_files
            if ef.name.endswith('.ipynb')
        ]

        urls = [
            "lab"
        ] + [
            "api/contents/{}.ipynb".format(nb_name)
            for nb_name in nb_names
        ] + [
            "api/contents/{}".format(ef)
            for ef in self.extra_files
            if not ef.name.endswith(".ipynb")
        ] + self.extra_urls

        with TemporaryDirectory() as tmpdir:
            urls += list(self.prepare_contents(tmpdir))
            urls += list(self.prepare_notebooks(tmpdir, nb, nb_names))
            self.fetch_all(tmpdir, url_root, urls, resources)

        self.copy_assets(output_root, lab_path, static_path)
        self.fix_urls(output_root, nb_names)
        self.fake_apis(output_root)
        self.fake_home(output_root)

        langinfo = nb.metadata.get('language_info', {})
        lexer = langinfo.get('pygments_lexer', langinfo.get('name', None))
        self.register_filter('highlight_code',
                             Highlight2HTML(pygments_lexer=lexer, parent=self))

        return super(HTMLExporter, self).from_notebook_node(
            nb, resources, **kw)