当前位置: 首页>>代码示例>>Python>>正文


Python HTMLExporter.from_filename方法代码示例

本文整理汇总了Python中IPython.nbconvert.exporters.HTMLExporter.from_filename方法的典型用法代码示例。如果您正苦于以下问题:Python HTMLExporter.from_filename方法的具体用法?Python HTMLExporter.from_filename怎么用?Python HTMLExporter.from_filename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPython.nbconvert.exporters.HTMLExporter的用法示例。


在下文中一共展示了HTMLExporter.from_filename方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: nb_renderer

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
def nb_renderer(full_path):
    directory, base = split(full_path)
    cache_file = join(directory, '.%s.html' % base)
    if not current_app.config.get('DEBUG'):
        try:
            if isfile(cache_file) and getmtime(full_path) < getmtime(cache_file):
                current_app.logger.debug('Using Cache File %s' % cache_file)
                return raw_renderer(cache_file)
        except:
            current_app.logger.warn('There was an error reading from the cache file %s' % cache_file)

    ex = HTMLExporter(extra_loaders=[current_app.jinja_env.loader],
                      template_file='wakari_notebook.html')

    ex.environment.globals.update(current_app.jinja_env.globals)
    current_app.update_template_context(ex.environment.globals)
    ex.environment.globals.update(dirname=dirname(request.view_args['path']))

    output, _ = ex.from_filename(full_path)


    try:
        with open(cache_file, 'w') as fd:
            current_app.logger.debug('Writing Cache File %s' % cache_file)
            fd.write(output.encode(errors='replace'))
    except (OSError, IOError):
        current_app.logger.warn('There was an error writing to the cache file %s' % cache_file)
        try:
            if isfile(cache_file): os.unlink(cache_file)
        except OSError:
            current_app.logger.warn('There was an error removing the cache file %s' % cache_file)
            pass

    return output
开发者ID:B-Rich,项目名称:wakari-app-viewer,代码行数:36,代码来源:renderer.py

示例2: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)

        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)['metadata']

            # Fix metadata to pelican standars
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
            metadata['ipython'] = True

        # Convert ipython notebook to html
        config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                         'highlight_class': '.highlight-ipynb'}})
        exporter = HTMLExporter(config=config, template_file='basic',
                                filters={'highlight2html': custom_highlighter})

        content, info = exporter.from_filename(filepath)

        # Process using Pelican HTMLReader
        content = '<body>{0}</body>'.format(content)  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        summary = parser.summary

        metadata['summary'] = summary

        # Remove some CSS styles, so it doesn't break the themes.
        def filter_tags(style_text):
            style_list = style_text.split('\n')
            exclude = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li',
                       '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text',
                       'code', 'pre', 'div.text_cell_render']
            style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0]
            ans = '\n'.join(style_list)
            return '<style type=\"text/css\">{0}</style>'.format(ans)

        css = '\n'.join(filter_tags(css) for css in info['inlining']['css'])
        css = css + CUSTOM_CSS
        body = css + body

        return body, metadata
开发者ID:5n1p,项目名称:pelican-ipythonnb,代码行数:61,代码来源:ipythonnb.py

示例3: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)

        _metadata = {}
        # See if metadata file exists metadata
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)
        if os.path.exists(metadata_filepath):
            with open(metadata_filepath, 'r') as metadata_file:
                content = metadata_file.read()
                metadata_file = open(metadata_filepath)
                md = markdown.Markdown(extensions=['meta'])
                md.convert(content)
                _metadata = md.Meta

            for key, value in _metadata.items():
                _metadata[key] = value[0]
        else:
            # Try to load metadata from inside ipython nb
            ipynb_file = open(filepath)
            _metadata = json.load(ipynb_file)['metadata']

        metadata = {}
        for key, value in _metadata.items():
            key = key.lower()
            metadata[key] = self.process_metadata(key, value)
        metadata['ipython'] = True

        # Converting ipythonnb to html
        config = Config({'CSSHTMLHeaderTransformer':
                         {'enabled': True,
                          'highlight_class': '.highlight-ipynb'}})
        exporter = HTMLExporter(config=config, template_file='basic',
                                filters={'highlight2html': custom_highlighter})
        body, info = exporter.from_filename(filepath)

        def filter_tags(s):
            l = s.split('\n')
            exclude = ['a', '.rendered_html', '@media']
            l = [i for i in l if len(list(filter(i.startswith, exclude))) == 0]
            ans = '\n'.join(l)
            return STYLE_TAG.format(ans)

        STYLE_TAG = '<style type=\"text/css\">{0}</style>'
        css = '\n'.join(filter_tags(css) for css in info['inlining']['css'])
        css = css + CUSTOM_CSS
        #body = css + body
        return body, metadata
开发者ID:adamgreig,项目名称:negativeacknowledge,代码行数:51,代码来源:ipythonnb.py

示例4: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)

        _metadata = {}
        # See if metadata file exists metadata
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)
        if os.path.exists(metadata_filepath):
            with open(metadata_filepath, 'r') as metadata_file:
                content = metadata_file.read()
                metadata_file = open(metadata_filepath)
                md = markdown.Markdown(extensions=['meta'])
                md.convert(content)
                _metadata = md.Meta

            for key, value in _metadata.items():
                _metadata[key] = value[0]
        else:
            # Try to load metadata from inside ipython nb
            ipynb_file = open(filepath)
            _metadata = json.load(ipynb_file)['metadata']

        metadata = {}
        for key, value in _metadata.items():
            key = key.lower()
            metadata[key] = self.process_metadata(key, value)
        metadata['ipython'] = True

        # Converting ipythonnb to html
        config = Config({'CSSHTMLHeaderTransformer': {'enabled': True}})
        here = os.path.dirname(os.path.realpath(__file__))
        loader = FileSystemLoader(here)
        exporter = HTMLExporter(config=config, extra_loaders=[loader],
                template_file='content.tpl')
        body, info = exporter.from_filename(filepath)

        return body, metadata
开发者ID:paxswill,项目名称:pelican-ipythonnb,代码行数:40,代码来源:__init__.py

示例5: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)

        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)['metadata']

            # Fix metadata to pelican standards
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
        metadata['ipython'] = True

        # Convert ipython notebook to html
        config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                         'highlight_class': '.highlight-ipynb'}})
        exporter = HTMLExporter(config=config, template_file='basic',
                                filters={'highlight2html': custom_highlighter})

        content, info = exporter.from_filename(filepath)

        if BeautifulSoup:
            soup = BeautifulSoup(content)
            for i in soup.findAll("div", {"class" : "input"}):
                if i.findChildren()[1].find(text='#ignore') is not None:
                    i.extract()
        else:
            soup = content

        # Process using Pelican HTMLReader
        content = '<body>{0}</body>'.format(soup)  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        summary = parser.summary

        metadata['summary'] = summary

        def filter_css(style_text):
            '''
            HACK: IPython returns a lot of CSS including its own bootstrap.
            Get only the IPython Notebook CSS styles.
            '''
            index = style_text.find('/*!\n*\n* IPython notebook\n*\n*/')
            if index > 0:
                style_text = style_text[index:]
            index = style_text.find('/*!\n*\n* IPython notebook webapp\n*\n*/')
            if index > 0:
                style_text = style_text[:index]

            style_text = re.sub(r'color\:\#0+(;)?', '', style_text)
            style_text = re.sub(r'\.rendered_html[a-z0-9 ]*\{[a-z0-9:;%.#\-\s\n]+\}', '', style_text)

            return '<style type=\"text/css\">{0}</style>'.format(style_text)

        ipython_css = '\n'.join(filter_css(css_style) for css_style in info['inlining']['css'])
        body = ipython_css + body + LATEX_CUSTOM_SCRIPT

        return body, metadata
开发者ID:TheNeuralBit,项目名称:theneuralbit,代码行数:75,代码来源:ipynb.py

示例6: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split(".")[0] + ".ipynb-meta"
        metadata_filepath = os.path.join(filedir, metadata_filename)

        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)["metadata"]

            # Fix metadata to pelican standards
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
            metadata["ipython"] = True

        # Convert ipython notebook to html
        config = Config({"CSSHTMLHeaderTransformer": {"enabled": True, "highlight_class": ".highlight-ipynb"}})
        exporter = HTMLExporter(config=config, template_file="basic", filters={"highlight2html": custom_highlighter})

        content, info = exporter.from_filename(filepath)

        # Process using Pelican HTMLReader
        content = "<body>{0}</body>".format(content)  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        summary = parser.summary

        metadata["summary"] = summary

        # Remove some CSS styles, so it doesn't break the themes.
        def filter_tags(style_text):
            style_list = style_text.split("\n")
            exclude = [
                "p",
                "h1",
                "h2",
                "h3",
                "h4",
                "h5",
                "h6",
                "a",
                "ul",
                "ol",
                "li",
                ".rendered_html",
                "@media",
                ".navbar",
                "nav.navbar",
                ".navbar-text",
                "code",
                "pre",
                "div.text_cell_render",
            ]
            style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0]
            ans = "\n".join(style_list)
            return '<style type="text/css">{0}</style>'.format(ans)

        css = "\n".join(filter_tags(css) for css in info["inlining"]["css"])
        css = css + CUSTOM_CSS
        body = css + body

        return body, metadata
开发者ID:Mondego,项目名称:pyreco,代码行数:77,代码来源:allPythonContent.py

示例7: read

# 需要导入模块: from IPython.nbconvert.exporters import HTMLExporter [as 别名]
# 或者: from IPython.nbconvert.exporters.HTMLExporter import from_filename [as 别名]
    def read(self, filepath):
        metadata = {}

        # Files
        filedir = os.path.dirname(filepath)
        filename = os.path.basename(filepath)
        metadata_filename = filename.split('.')[0] + '.ipynb-meta'
        metadata_filepath = os.path.join(filedir, metadata_filename)

        # Load metadata
        if os.path.exists(metadata_filepath):
            # Metadata is on a external file, process using Pelican MD Reader
            md_reader = MarkdownReader(self.settings)
            _content, metadata = md_reader.read(metadata_filepath)
        else:
            # Load metadata from ipython notebook file
            ipynb_file = open(filepath)
            metadata = json.load(ipynb_file)['metadata']

            # Fix metadata to pelican standars
            for key, value in metadata.items():
                del metadata[key]
                key = key.lower()
                metadata[key] = self.process_metadata(key, value)
            metadata['ipython'] = True

        # Convert ipython notebook to html
        config = Config({'CSSHTMLHeaderTransformer': {'enabled': True,
                         'highlight_class': '.highlight-ipynb'}})
        exporter = HTMLExporter(config=config, template_file='basic',
                                filters={'highlight2html': custom_highlighter})

        content, info = exporter.from_filename(filepath)

        soup = BeautifulSoup(content)

        # find all the inputs with hide and strip them out
        inputs = 0
        for i in soup.findAll("div", {"class" : "input"}):
            if i.findChildren()[1].findChild().findChild().findChild().find(text='#HIDE') != None:
                i.extract()
            else:
                inputs = inputs+1
                tag = soup.new_tag('a', href="javascript:toggle('input%s');" % inputs , target='_self')
                tag.string = i.findChildren()[0].text.strip()
                i.findChildren()[0].clear()
                i.findChildren()[0].append(tag)
                try:
                    i.find("div", {"class" :"input_area"})['id'] = 'input%s' %inputs
                except:
                    print i


        # Process using Pelican HTMLReader
        content = '<body>{0}</body>'.format(unicode(soup))  # So Pelican HTMLReader works
        parser = MyHTMLParser(self.settings, filename)
        parser.feed(content)
        parser.close()
        body = parser.body
        summary = parser.summary

        metadata['summary'] = summary

        # Remove some CSS styles, so it doesn't break the themes.
        def filter_tags(style_text):
            style_list = style_text.split('\n')
            exclude = ['body','p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li',
                       '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text',
                       'code', 'pre', 'div.text_cell_render','body']
            style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0]
            ans = '\n'.join(style_list)
            return '<style type=\"text/css\">{0}</style>'.format(ans)

        css = '\n'.join(filter_tags(css) for css in info['inlining']['css'])
        css = CUSTOM_CSS
        body = css + body
        body = body + js
        return body, metadata
开发者ID:anemes,项目名称:pelican-ipythonnb,代码行数:80,代码来源:ipythonnb.py


注:本文中的IPython.nbconvert.exporters.HTMLExporter.from_filename方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。