Exemple #1
0
def script_post_save(model, os_path, contents_manager, **kwargs):
    if (model['type'] != 'notebook'):
        return

    # auto save .py
    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log_py = contents_manager.log

    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log_py.info("Saving script /%s",
                to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

    # auto save html
    global _html_exporter
    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)
    log_h5 = contents_manager.log

    base, ext = os.path.splitext(os_path)
    script, resources = _html_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log_h5.info("Saving html /%s",
                to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter
    from nbconvert.exporters.html import HTMLExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    global _html_exporter
    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

    # save html
    base, ext = os.path.splitext(os_path)
    script, resources = _html_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving html /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
Exemple #3
0
    def render_notebook(self):
        # Get the requested tool
        tool, tid = self._get_tool()

        # If this is an IPython notebook, render it into HTML
        if not isinstance(tool, JupyterNotebookTransformer):
            return exc.HTTPNotAcceptable(
                detail='Tool is not a Jupyter notebook')

        # Get whether to download or view in HTML
        output_style = self.request.GET.get('format', 'html')

        # Load in the notebook
        if output_style == 'html':
            # Parse the notebook as an notebook object
            nb = nbformat.reads(tool.notebook, nbformat.NO_CONVERT)
            add_data(nb, None, None, use_placeholder=True)

            # Render it as HTML
            ex = HTMLExporter()
            output, _ = ex.from_notebook_node(nb)
            return Response(output)

        elif output_style == 'file':
            return Response(content_type='application/force-download',
                            content_disposition='attachment; filename=%s.%s' %
                            (tool.name, 'ipynb'),
                            body=str(tool.write_notebook(None)))
        else:
            return exc.HTTPBadRequest(detail='Format not recognized: ' +
                                      output_style)
def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter
    from nbconvert.exporters.html import HTMLExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    global _html_exporter
    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

    # save html
    base, ext = os.path.splitext(os_path)
    script, resources = _html_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving html /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
def script_post_save(model, os_path, contents_manager, **kwargs):
    import os
    from notebook.utils import to_api_path
    import io
    """convert notebooks to Python script after save with nbconvert
    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.python import PythonExporter
    from nbconvert.exporters.html import HTMLExporter

    if model['type'] != 'notebook':
        return

    global _python_exporter
    #if _python_exporter is None:
    _python_exporter = PythonExporter(parent=contents_manager)
    log = contents_manager.log

    global _html_exporter
    #if _html_exporter is None:
    _html_exporter = HTMLExporter(parent=contents_manager)
    log = contents_manager.log

    # save .py file
    base, ext = os.path.splitext(os_path)
    script, resources = _python_exporter.from_filename(os_path)
    script_fname = base + '.py'
    log.info("Saving python /%s",
             to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)

    import subprocess
    subprocess.Popen("/bin/mv {0} {0}.tmp".format(script_fname), shell=True)
    subprocess.Popen(
        "cat {0}.tmp | grep -v get_ipython | grep -v '# In\[' | cat -s > {0}".
        format(script_fname),
        shell=True)
    subprocess.Popen("rm {0}.tmp".format(script_fname), shell=True)

    # save html
    base, ext = os.path.splitext(os_path)
    script, resources = _html_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.html')
    log.info("Saving html /%s",
             to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
Exemple #6
0
def ipynb2html(message: dict) -> dict:
    """convert jupyter notebook

    TODO: make this work from stdin

    Args:
        content: the notebook contents to convert

    Returns:
        html: str: the html representation for the requested jupyter notebook file.

    Note:
        this function requires nbconvert

    """
    from nbconvert.nbconvertapp import NbConvertApp
    from nbconvert.exporters.html import HTMLExporter

    # create an NbConvertApp:
    app = NbConvertApp.instance()
    # initialize the app with the arguments
    app.initialize(["--template=basic"])
    # create an exporter
    app.exporter = HTMLExporter(config=app.config)
    # get html output
    message["content"], _ = app.export_single_notebook(
        notebook_filename=None,
        resources=None,
        input_buffer=io.StringIO(message["content"]),
    )
    message["encoding"] = "html"
    message = filter_urls(message)
    return message
Exemple #7
0
def save_as_html(os_path, contents_manager):
    from nbconvert.exporters.html import HTMLExporter

    global _html_exporter

    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)

    log = contents_manager.log
    base, ext = os.path.splitext(os_path)
    html_fname = base + '.html'
    html, resources = _html_exporter.from_filename(os_path)

    if resources.get('output_extension') == '.txt':
        # prevent annoying behaviour of also saving as file.txt
        return

    html_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving HTML /%s",
             to_api_path(html_fname, contents_manager.root_dir))

    with io.open(html_fname, 'w', encoding='utf-8') as f:
        f.write(html)
    def generate_render(self):
        self.obj.file.open()
        output, resource = HTMLExporter().from_file(self.obj.file)
        soup = BeautifulSoup(output, 'html.parser')
        excludes = ['require.min.js', 'jquery.min.js']
        js = ' '.join([
            str(row) for row in soup.find_all('script')
            if all(exclude not in str(row) for exclude in excludes)
        ])

        result = [
            mark_safe(
                '<div class="review-ipynb">' +
                js + str(row) +
                '</div>'
            ) for row in soup.find_all(class_='cell')]
        return result
Exemple #9
0
def script_post_save(model, os_path, contents_manager, **kwargs):
    if model['type'] != 'notebook':
        return

    from nbconvert.exporters.script import ScriptExporter
    from nbconvert.exporters.html import HTMLExporter

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)

    global _html_exporter
    if _html_exporter is None:
        _html_exporter = HTMLExporter(parent=contents_manager)
    log = contents_manager.log

    #export_script(_html_exporter, model, os_path, contents_manager, **kwargs)
    export_script(_script_exporter,   model, os_path, contents_manager, **kwargs)
Exemple #10
0
    def from_notebook_node(self, nb, resources=None, **kw):

        nb_copy = copy.deepcopy(nb)

        nb_copy['metadata']['metatab'] = self.metadata

        # get the Normal HTML output:
        output, resources = HTMLExporter(config=self.config).from_notebook_node(nb_copy)

        resources['unique_key'] = 'notebook'

        # Get all of the image resources
        nb_copy, resources = self.extract_resources(nb_copy, resources)

        # Add resources for the html and markdown version of the notebook

        self.add_pdf(nb_copy, resources)
        self.add_markdown_doc(nb_copy, resources)
        self.add_html_doc(nb_copy, resources)
        self.add_basic_html_doc(nb_copy, resources)

        return output, resources
Exemple #11
0
def render_page(nbname, config={}):

    # Combine base config with any provided overrides.
    config = dict(flask_app.base_config, **config)

    global runner

    if not nbmanager.notebook_exists(nbname):
        print "Notebook %s does not exist." % nbname
        flask.abort(404)

    print "Loading notebook %s" % nbname
    #nbmanager.trust_notebook(nbname)
    nb = nbmanager.get_notebook(nbname)

    if config['run']:
        print "Making runner..." ''

        # This is an ugly little bit to deal with a sporadic
        #  'queue empty' bug in jupyter that only seems to
        #  happen on the integration servers...
        #  see https://github.com/paulgb/runipy/issues/36
        N_RUN_RETRIES = 4
        from Queue import Empty

        for i in range(N_RUN_RETRIES):
            try:
                if runner is None:
                    make_notebook_runner_thread.join()

                # Do as complete of a reset of the kernel as we can.
                # Unfortunately, this doesn't really do a 'hard' reset
                # of any modules...
                class ResetCell(dict):
                    """Simulates just enough of a notebook cell to get this
                    'reset cell' executed using the existing runipy
                     machinery."""
                    input = "get_ipython().reset(new_session=True)"

                runner.run_cell(ResetCell())
                runner.nb = nb
                print "Running notebook"
                runner.run_notebook(skip_exceptions=True)
                break
            except Empty as e:
                print "WARNING: Empty bug happened."
                if i >= (N_RUN_RETRIES - 1):
                    raise
        nb = runner.nb
    # else:
    #     nb = nb['content']
    print "Exporting notebook"
    exporter = HTMLExporter(config=Config({
        'HTMLExporter': {
            'template_file':
            config['template'],
            'template_path':
            ['.', os.path.join(os.path.split(__file__)[0], 'templates')]
        }
    }))
    output, resources = exporter.from_notebook_node(
        convert(nb, current_nbformat))
    print "Returning."
    return output
Exemple #12
0
def write_nb_to_html(filename):
    nbc = NbConvertApp()
    nbc.exporter = HTMLExporter()
    nbc.writer = FilesWriter()
    nbc.convert_single_notebook(filename)
Exemple #13
0
def render_page(nbname, config={}):

    # Combine base config with any provided overrides.
    config = dict(flask_app.base_config, **config)

    global runner

    if not nbmanager.notebook_exists(nbname):
        print "Notebook %s does not exist." % nbname
        flask.abort(404)

    print "Loading notebook %s" % nbname
    #nbmanager.trust_notebook(nbname)
    nb = nbmanager.get_notebook(nbname)

    if config['run']:
        print "Making runner..."''

        # This is an ugly little bit to deal with a sporadic
        #  'queue empty' bug in jupyter that only seems to
        #  happen on the integration servers...
        #  see https://github.com/paulgb/runipy/issues/36
        N_RUN_RETRIES = 4
        from Queue import Empty

        for i in range(N_RUN_RETRIES):
            try:
                if runner is None:
                    make_notebook_runner_thread.join()

                # Do as complete of a reset of the kernel as we can.
                # Unfortunately, this doesn't really do a 'hard' reset
                # of any modules...
                class ResetCell(dict):
                    """Simulates just enough of a notebook cell to get this
                    'reset cell' executed using the existing runipy
                     machinery."""
                    input = "get_ipython().reset(new_session=True)"
                runner.run_cell(ResetCell())
                runner.nb = nb
                print "Running notebook"
                runner.run_notebook(skip_exceptions=True)
                break
            except Empty as e:
                print "WARNING: Empty bug happened."
                if i >= (N_RUN_RETRIES - 1):
                    raise
        nb = runner.nb
    # else:
    #     nb = nb['content']
    print "Exporting notebook"
    exporter = HTMLExporter(
        config=Config({
            'HTMLExporter': {
                'template_file': config['template'],
                'template_path': ['.', os.path.join(os.path.split(__file__)[0], 'templates')]
            }
        })
    )
    output, resources = exporter.from_notebook_node(
        convert(nb, current_nbformat)
    )
    print "Returning."
    return output
Exemple #14
0
    def add_html_doc(self, nb, resources):
        html_exp = HTMLExporter(config=self.config, template_file='hide_input_html.tpl')

        (html_full_body, _) = html_exp.from_notebook_node(nb)

        resources['outputs'][self.base_name+'.html'] = html_full_body.encode('utf-8')
Exemple #15
0
from nbformat import reader, converter
from nbconvert.exporters.html import HTMLExporter
from traitlets.config import Config
from IPython.display import display, HTML
from IPython.core.interactiveshell import InteractiveShell

UNBOUND_HELP_DOCSTRING_TMPL = 'Run "{name}()" to see this example. Run "%inject {name}()" to inject the example into your notebook.'
BOUND_HELP_DOCSTRING_TMPL = 'Run this function to see a rich example.'
API_DOCSTRING_TMPL = 'Run {name}.help() for a detailed example.'
MODULE_API_DOCSTRING_TMPL = 'Run help() for a detailed example.'
MODULE_DOCSTRING_TMPL = 'Run this function to see rich help about this importable notebook.'

export_html = HTMLExporter(config=Config({
    'CSSHTMLHeaderPreprocessor': {
        'enabled': False
    },
    'HTMLExporter': {
        'template_file': 'basic'
    }
}))


def rich_help():
    def _rich_help(*args):
        with warnings.catch_warnings():
            # ignore warnings about pandoc, nodejs, etc.
            warnings.filterwarnings('ignore', message='Node.js')
            output, resources = export_html.from_notebook_node(
                _rich_help.__richdoc__)
        # include style in every output; tried it global on import, but if the import is
        # re-run it's lost because there is no output on a repeat module import
        output = '''<style>