Esempio n. 1
0
def convert_dir(dir):

	base = os.path.dirname(os.path.realpath(__file__))
	template_dir = os.path.join(base)
	template = 'custom_notebook.tpl'
	templateLoader = jinja2.FileSystemLoader( searchpath=template_dir )
	templateEnv = jinja2.Environment( loader=templateLoader )

	config = Config()
	config.NbconvertApp.fileext = 'html'
	config.TemplateExporter.template_file = template

	exporter = TemplateExporter(config=config, extra_loaders=[templateLoader], template=template)
	exporter.template_file = template

	for f in [x for x in os.listdir(dir) if re.match('.*\.ipynb$',x)]:
		f = os.path.join(dir,f)
		out = re.match('^(.+)\.ipynb$',f).group(1) + '.html'
		out = re.sub('\s','_',out)
		print out
		nb = nbformat.current.read(open(f,'rw'),'ipynb')

		html, resources = exporter.from_notebook_node(nb)
		html = html.encode('ascii', 'ignore')
		o = open(out,'w')
		o.write(html)
		o.close()
Esempio n. 2
0
class SphinxHowtoExporter(latex.LatexExporter):
    """
    Exports Sphinx "HowTo" LaTeX documents.  The Sphinx "HowTo" exporter 
    produces short document format latex for use with PDFLatex.
    """

    template_file = Unicode('sphinx_howto',
                            config=True,
                            help="Name of the template file to use")

    _default_config = Config({'SphinxTransformer': {'enabled': True}})

    def __init__(self, transformers=None, filters=None, config=None, **kw):

        c = self.default_config
        if config:
            c.merge(config)

        super(SphinxHowtoExporter, self).__init__(transformers=transformers,
                                                  filters=filters,
                                                  config=c,
                                                  **kw)

    def _register_transformers(self):

        #Register the transformers of the base class.
        super(SphinxHowtoExporter, self)._register_transformers()

        #Register sphinx latex transformer
        self.register_transformer(SphinxTransformer)
Esempio n. 3
0
class RstExporter(exporter.Exporter):
    """
    Exports restructured text documents.
    """

    file_extension = Unicode(
        'rst',
        config=True,
        help="Extension of the file that should be written to disk")

    template_file = Unicode('rst',
                            config=True,
                            help="Name of the template file to use")

    _default_config = Config({'ExtractFigureTransformer': {'enabled': True}})

    def __init__(self, transformers=None, filters=None, config=None, **kw):

        c = self.default_config
        if config:
            c.merge(config)

        super(RstExporter, self).__init__(transformers=transformers,
                                          filters=filters,
                                          config=c,
                                          **kw)
Esempio n. 4
0
    def __init__(self, transformers=None, filters=None, config=None, **kw):
        """
        Public constructor
    
        Parameters
        ----------
        transformers : list[of transformer]
            Custom transformers to apply to the notebook prior to engaging
            the Jinja template engine.  Any transformers specified here
            will override existing transformers if a naming conflict
            occurs.
        filters : list[of filter]
            Custom filters to make accessible to the Jinja templates.  Any
            filters specified here will override existing filters if a
            naming conflict occurs.
        config : config
            User configuration instance.
        """

        #Call base class constructor.

        c = self.default_config
        if config:
            c.merge(Config(config))

        super(LatexExporter, self).__init__(transformers,
                                            filters,
                                            config=c,
                                            **kw)
Esempio n. 5
0
 def default_config(self):
     c = Config({
         'NbConvertBase': {
             'display_data_priority': [
                 'text/latex', 'application/pdf', 'image/png', 'image/jpeg',
                 'image/svg+xml', 'text/plain'
             ]
         },
         'ExtractOutputPreprocessor': {
             'enabled': True
         },
         'SVG2PDFPreprocessor': {
             'enabled': True
         },
         'LatexPreprocessor': {
             'enabled': True
         },
         'SphinxPreprocessor': {
             'enabled': True
         },
         'HighlightMagicsPreprocessor': {
             'enabled': True
         }
     })
     c.merge(super(LatexExporter, self).default_config)
     return c
Esempio n. 6
0
 def default_config(self):
     c = Config({
     'SphinxTransformer': {'enabled':True}
     })
     c.merge(super(SphinxHowtoExporter,self).default_config)
     return c
                 
Esempio n. 7
0
def activate(profile, mixed=False):
    dname = os.path.dirname(__file__)
    pdir = locate_profile(profile)

    jc = JSONFileConfigLoader('ipython_notebook_config.json', pdir)

    try:
        config = jc.load_config()
    except (ConfigFileNotFound, ValueError) as e:
        config = Config()
    if 'NotebookApp' in config:
        if ('tornado_settings'
                in config['NotebookApp']) or ('contents_manager_class'
                                              in config['NotebookApp']):
            # TODO, manually merge tornado settin if exist
            # but cannot do anythin automatically if contents_manager_calss is set
            raise ValueError(
                'You already got some configuration that will conflict with google drive. Bailin out'
            )
    if mixed:
        drive_config = JSONFileConfigLoader('mixed_contents.json',
                                            dname).load_config()
    else:
        drive_config = JSONFileConfigLoader('ipython_notebook_config.json',
                                            dname).load_config()
    config.merge(drive_config)
    print('Activating Google Drive integration for profile "%s"' % profile)
    config['nbformat'] = 1

    with io.open(os.path.join(pdir, 'ipython_notebook_config.json'),
                 'w',
                 encoding='utf-8') as f:
        f.write(cast_unicode_py2(json.dumps(config, indent=2)))
Esempio n. 8
0
def test_find_connection_file():
    cfg = Config()
    with TemporaryDirectory() as d:
        cfg.ProfileDir.location = d
        cf = 'kernel.json'
        app = DummyConsoleApp(config=cfg, connection_file=cf)
        app.initialize()

        security_dir = os.path.join(app.profile_dir.location, 'security')
        profile_cf = os.path.join(security_dir, cf)

        with open(profile_cf, 'w') as f:
            f.write("{}")

        for query in (
                'kernel.json',
                'kern*',
                '*ernel*',
                'k*',
        ):
            nt.assert_equal(
                connect.find_connection_file(query, path=security_dir),
                profile_cf)

        BaseIPythonApplication._instance = None
Esempio n. 9
0
    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
Esempio n. 10
0
 def IPythonShell(namespace, banner):
     from IPython import start_ipython
     sys.argv = [''
                 ]  # This is needed because argparse tries to grab argv[0]
     from IPython.config import Config
     c = Config()
     c.TerminalInteractiveShell.banner1 = banner
     start_ipython(user_ns=namespace, argv=[], config=c)
Esempio n. 11
0
 def default_config(self):
     c = Config({
         'RevealHelpPreprocessor': {
             'enabled': True,
         },
     })
     c.merge(super(SlidesExporter, self).default_config)
     return c
Esempio n. 12
0
 def test_preprocessor_dottedobjectname(self):
     """
     Can a preprocessor be added to the preprocessors list by dotted object name?
     """
     config = Config({'Exporter': {'preprocessors': ['IPython.nbconvert.exporters.tests.cheese.CheesePreprocessor']}})
     exporter = self._make_exporter(config=config)
     (output, resources) = exporter.from_filename(self._get_notebook())
     assert resources is not None
     assert resources['cheese'] == 'real'
Esempio n. 13
0
 def test_transformer_instance(self):
     """
     Can a transformer be added to the transformers list by instance?
     """
     config = Config({'Exporter': {'transformers': [CheeseTransformer()]}})
     exporter = self._make_exporter(config=config)
     (output, resources) = exporter.from_filename(self._get_notebook())
     assert resources is not None
     assert resources['cheese'] == 'real'
Esempio n. 14
0
 def test_preprocessor_class(self):
     """
     Can a preprocessor be added to the preprocessors list by class type?
     """
     config = Config({'Exporter': {'preprocessors': [CheesePreprocessor]}})
     exporter = self._make_exporter(config=config)
     (output, resources) = exporter.from_filename(self._get_notebook())
     assert resources is not None
     assert resources['cheese'] == 'real'
Esempio n. 15
0
 def test_extract_outputs(self):
     """
     If the ExtractOutputPreprocessor is enabled, are outputs extracted?
     """
     config = Config({'ExtractOutputPreprocessor': {'enabled': True}})
     exporter = self._make_exporter(config=config)
     (output, resources) = exporter.from_filename(self._get_notebook())
     assert resources is not None
     assert isinstance(resources['outputs'], dict)
     assert len(resources['outputs']) > 0
Esempio n. 16
0
def nb2html(nb):
    """
    Cribbed from nbviewer
    """
    config = Config()
    config.HTMLExporter.template_file = 'basic'
    config.NbconvertApp.fileext = "html"
    config.CSSHtmlHeaderTransformer.enabled = False

    C = HTMLExporter(config=config)
    return C.from_notebook_node(nb)[0]
Esempio n. 17
0
def convert_nb_html(nb):
    """
    Convert a notebooks output to HTML
    """
    nb = run_notebook(nb)
    config = Config({'HTMLExporter': {'default_template': 'basic'}})
    exportHtml = HTMLExporter(config=config)
    html, resources = exportHtml.from_notebook_node(nb)
    soup = BeautifulSoup(html)
    filters = ["output", "text_cell_render border-box-sizing rendered_html"]
    return ''.join(map(str, soup.findAll("div", {"class": filters})))
Esempio n. 18
0
 def default_config(self):
     c = Config({
         'CSSHTMLHeaderTransformer':{
             'enabled':True
             },
         'RevealHelpTransformer':{
             'enabled':True,
             },                
         })
     c.merge(super(SlidesExporter,self).default_config)
     return c
Esempio n. 19
0
def nb2html(nb):
    """
    Cribbed from nbviewer
    """
    config = Config()
    config.ConverterTemplate.template_file = 'basichtml'
    config.NbconvertApp.fileext = "html"
    config.CSSHtmlHeaderTransformer.enabled = False

    C = ConverterTemplate(config=config)
    return C.convert(nb)[0]
Esempio n. 20
0
 def default_config(self):
     c = Config({
         'CSSHTMLHeaderPreprocessor': {
             'enabled': True
         },
         'HighlightMagicsPreprocessor': {
             'enabled': True
         }
     })
     c.merge(super(HTMLExporter, self).default_config)
     return c
Esempio n. 21
0
 def compile_html(self, source, dest, is_two_file=True):
     if flag is None:
         req_missing(['ipython>=1.1.0'], 'build this site (compile ipynb)')
     makedirs(os.path.dirname(dest))
     HTMLExporter.default_template = 'basic'
     c = Config(self.site.config['IPYNB_CONFIG'])
     exportHtml = HTMLExporter(config=c)
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             nb_json = nbformat.read(in_file, current_nbformat)
         (body, resources) = exportHtml.from_notebook_node(nb_json)
         out_file.write(body)
Esempio n. 22
0
    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
Esempio n. 23
0
 def compile_html_string(self, source, is_two_file=True):
     """Export notebooks as HTML strings."""
     if flag is None:
         req_missing(['ipython[notebook]>=2.0.0'],
                     'build this site (compile ipynb)')
     HTMLExporter.default_template = 'basic'
     c = Config(self.site.config['IPYNB_CONFIG'])
     exportHtml = HTMLExporter(config=c)
     with io.open(source, "r", encoding="utf8") as in_file:
         nb_json = nbformat.read(in_file, current_nbformat)
     (body, resources) = exportHtml.from_notebook_node(nb_json)
     return body
Esempio n. 24
0
class GenericFileCheckpointsAPITest(APITest):
    """
    Run the tests from APITest with GenericFileCheckpoints.
    """
    config = Config()
    config.FileContentsManager.checkpoints_class = GenericFileCheckpoints

    def test_config_did_something(self):

        self.assertIsInstance(
            self.notebook.contents_manager.checkpoints,
            GenericFileCheckpoints,
        )
Esempio n. 25
0
def test_format_config():
    """config objects don't pretend to support fancy reprs with lazy attrs"""
    f = HTMLFormatter()
    cfg = Config()
    with capture_output() as captured:
        result = f(cfg)
    nt.assert_is(result, None)
    nt.assert_equal(captured.stderr, "")

    with capture_output() as captured:
        result = f(Config)
    nt.assert_is(result, None)
    nt.assert_equal(captured.stderr, "")
Esempio n. 26
0
 def default_config(self):
     c = Config({
         'NbConvertBase': {
             'display_data_priority' : ['text/javascript', 'text/html', 'application/pdf', 'image/svg+xml', 'text/latex', 'image/png', 'image/jpeg', 'text/plain']
             },
         'CSSHTMLHeaderPreprocessor':{
             'enabled':True
             },
         'HighlightMagicsPreprocessor': {
             'enabled':True
             }
         })
     c.merge(super(HTMLExporter,self).default_config)
     return c
Esempio n. 27
0
 def default_config(self):
     c = Config({
         'GlobalConfigurable': {
             'display_data_priority':
             ['latex', 'svg', 'png', 'jpg', 'jpeg', 'text']
         },
         'ExtractFigureTransformer': {
             'enabled': True,
             'extra_ext_map': {
                 'svg': 'pdf'
             },
         }
     })
     c.merge(super(LatexExporter, self).default_config)
     return c
Esempio n. 28
0
def nb2rst(nb, files_dir):
    """
    nb should be a NotebookNode
    """
    config = Config()
    C = ConverterRST(config=config)
    # bastardize how this is supposed to be called
    # either the API is broken, or I'm not using it right
    # why can't I set this using the config?
    C.files_dir = files_dir + "_files"
    if not os.path.exists(C.files_dir):
        os.makedirs(C.files_dir)
    # already parsed into a NotebookNode
    C.nb = nb
    return C.convert()
Esempio n. 29
0
def export_html(nb, dest_path=None, image_dir=None, image_rel_dir=None):
    """Convert notebook to HTML.

    Optionally saves HTML to dest_path.
    """
    c = Config({'ExtractOutputPreprocessor': {'enabled': True}})

    exporter = HTMLExporter(template_file='full', config=c)
    output, resources = exporter.from_notebook_node(nb)
    header = output.split('<head>', 1)[1].split('</head>', 1)[0]
    body = output.split('<body>', 1)[1].split('</body>', 1)[0]

    # Monkeypatch CSS
    header = header.replace('<style', '<style scoped="scoped"')
    header = header.replace(
        'body {\n  overflow: visible;\n  padding: 8px;\n}\n', '')
    header = header.replace("code,pre{", "code{")

    # Filter out styles that conflict with the sphinx theme.
    bad_anywhere = [
        'navbar', 'body{', 'alert{', 'uneditable-input{', 'collapse{'
    ]
    bad_anywhere.extend(['h%s{' % (i + 1) for i in range(6)])

    bad_beginning = ['pre{', 'p{margin']

    header_lines = [
        x for x in header.split('\n')
        if (not any(x.startswith(s)
                    for s in bad_beginning) and not any(s in x
                                                        for s in bad_anywhere))
    ]
    header = '\n'.join(header_lines)

    # Concatenate raw html lines
    lines = ['<div class="ipynotebook">']
    lines.append(header)
    lines.append(body)
    lines.append('</div>')
    html_out = '\n'.join(lines)

    if image_dir is not None and image_rel_dir is not None:
        html_out = export_images(resources, image_dir, image_rel_dir, html_out)

    if dest_path is not None:
        with io.open(dest_path, 'w', encoding='utf-8') as f:
            f.write(html_out)
    return html_out
Esempio n. 30
0
def test_get_connection_file():
    cfg = Config()
    with TemporaryWorkingDirectory() as d:
        cfg.ProfileDir.location = d
        cf = 'kernel.json'
        app = DummyConsoleApp(config=cfg, connection_file=cf)
        app.initialize(argv=[])
        
        profile_cf = os.path.join(app.profile_dir.location, 'security', cf)
        nt.assert_equal(profile_cf, app.connection_file)
        with open(profile_cf, 'w') as f:
            f.write("{}")
        nt.assert_true(os.path.exists(profile_cf))
        nt.assert_equal(connect.get_connection_file(app), profile_cf)
        
        app.connection_file = cf
        nt.assert_equal(connect.get_connection_file(app), profile_cf)