How to use the nbconvert.PythonExporter function in nbconvert

To help you get started, we’ve selected a few nbconvert examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github holoviz / holoviews / tests / ipython / testnotebooks.py View on Github external
def apply_preprocessors(preprocessors, nbname):
    notebooks_path = os.path.join(os.path.split(__file__)[0], 'notebooks')
    with open(os.path.join(notebooks_path, nbname)) as f:
        nb = nbformat.read(f, nbformat.NO_CONVERT)
        exporter = nbconvert.PythonExporter()
        for preprocessor in preprocessors:
            exporter.register_preprocessor(preprocessor)
        source, meta = exporter.from_notebook_node(nb)
    return source
github rodluger / starry / tests / test_notebooks.py View on Github external
def test_notebooks():
    """Run all notebooks in /docs/tutorials/ as tests."""
    # Get the notebook names
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    path = os.path.join(root, 'docs', 'tutorials')
    notebooks = glob.glob(os.path.join(path, '*.ipynb'))

    # Convert them to python scripts
    exporter = PythonExporter()
    for notebook in notebooks:
        # Get the script as a string
        script, _ = exporter.from_filename(notebook)

        # Get rid of %matplotlib inline commands
        script = script.replace("get_ipython().magic('matplotlib inline')", "")
        script = script.replace(
            "get_ipython().run_line_magic('matplotlib', 'inline')", "")

        # Get rid of %run commands
        script = re.sub("get_ipython\(\).magic\('run (.*)'\)", r"#", script)
        script = re.sub("get_ipython\(\).run_line_magic\('run', '(.*)'\)",
                        r"#", script)

        # Remove the %time wrappers
        script = re.sub("get_ipython\(\).magic\('time (.*)'\)", r"\1", script)
github msmbuilder / mdentropy / docs / sphinxext / notebook_sphinxext.py View on Github external
def export_python(nb, destfn):
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)
    with open(destfn, 'w') as f:
        f.write(body)
github holoviz / holoviews / holoviews / util / command.py View on Github external
def export_to_python(filename=None,
         preprocessors=[OptsMagicProcessor(),
                        OutputMagicProcessor(),
                        StripMagicsProcessor()]):

    filename = filename if filename else sys.argv[1]
    with open(filename) as f:
        nb = nbformat.read(f, nbformat.NO_CONVERT)
        exporter = nbconvert.PythonExporter()
        for preprocessor in preprocessors:
            exporter.register_preprocessor(preprocessor)
        source, meta = exporter.from_notebook_node(nb)
        return source
github pyviz-dev / nbsite / examples / sites / holoviews / holoviews / util / command.py View on Github external
def export_to_python(filename=None,
         preprocessors=[OptsMagicProcessor(),
                        OutputMagicProcessor(),
                        StripMagicsProcessor()]):

    filename = filename if filename else sys.argv[1]
    with open(filename) as f:
        nb = nbformat.read(f, nbformat.NO_CONVERT)
        exporter = nbconvert.PythonExporter()
        for preprocessor in preprocessors:
            exporter.register_preprocessor(preprocessor)
        source, meta = exporter.from_notebook_node(nb)
        return source
github msmbuilder / msmbuilder / docs / sphinxext / notebook_sphinxext.py View on Github external
def export_python(nb, destfn):
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)
    with open(destfn, 'w') as f:
        f.write(body)
github maxpumperla / hyperas / hyperas / optim.py View on Github external
def get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack, data_args):
    model_string = inspect.getsource(model)
    model_string = remove_imports(model_string)

    if notebook_name:
        notebook_path = os.getcwd() + "/{}.ipynb".format(notebook_name)
        with open(notebook_path, 'r') as f:
            notebook = nbformat.reads(f.read(), nbformat.NO_CONVERT)
            exporter = PythonExporter()
            source, _ = exporter.from_notebook_node(notebook)
    else:
        calling_script_file = os.path.abspath(inspect.stack()[stack][1])
        with open(calling_script_file, 'r') as f:
            source = f.read()

    cleaned_source = remove_all_comments(source)
    imports = extract_imports(cleaned_source, verbose)

    parts = hyperparameter_names(model_string)
    aug_parts = augmented_names(parts)

    hyperopt_params = get_hyperparameters(model_string)
    space = get_hyperopt_space(parts, hyperopt_params, verbose)

    functions_string = retrieve_function_string(functions, verbose)