Python nbconvert.PythonExporter() Examples
The following are 9
code examples of nbconvert.PythonExporter().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
nbconvert
, or try the search function
.
Example #1
Source File: parsing_utils.py From hyperparameter_hunter with MIT License | 7 votes |
def read_source_script(filepath): """Read the contents of `filepath` Parameters ---------- filepath: Str Absolute path to a Python file. Expected to end with '.py', or '.ipynb' Returns ------- source: Str The contents of `filepath`""" if filepath.endswith(".ipynb"): with open(filepath, "r") as f: from nbconvert import PythonExporter import nbformat notebook = nbformat.reads(f.read(), nbformat.NO_CONVERT) exporter = PythonExporter() source, _ = exporter.from_notebook_node(notebook) else: with open(filepath, "r") as f: source = f.read() return source
Example #2
Source File: notebook_sphinxext.py From mdentropy with MIT License | 5 votes |
def export_python(nb, destfn): exporter = PythonExporter() body, resources = exporter.from_notebook_node(nb) with open(destfn, 'w') as f: f.write(body)
Example #3
Source File: notebook_sphinxext.py From deepchem with MIT License | 5 votes |
def export_python(nb, destfn): exporter = PythonExporter() body, resources = exporter.from_notebook_node(nb) with open(destfn, 'w') as f: f.write(body)
Example #4
Source File: notebooks_test.py From batchflow with Apache License 2.0 | 5 votes |
def test_run_notebooks(path, microbatch, device): """ There are a lot of examples in different notebooks, and all of them should be working. Parameters ---------- path : str Location of notebook to run. microbatch : int or None If None, then no microbatch is applied. If int, then size of microbatch used. device : str or None If None, then default device behaviour is used. If str, then any option of device configuration from :class:`.tf.TFModel` is supported. Notes ----- `device` is moved to separate parameter in order to work properly with `parametrize`. """ # pylint: disable=exec-used if path.startswith(TUTORIALS_DIR) and 'CPU' not in device: pytest.skip("Tutorials don't utilize device config.") with warnings.catch_warnings(): warnings.simplefilter("ignore") from nbconvert import PythonExporter code, _ = PythonExporter().from_filename(path) code_ = [] for line in code.split('\n'): if not line.startswith('#'): flag = sum([name in line for name in BAD_PREFIXES]) if flag == 0: code_.append(line) code = '\n'.join(code_) exec(code, {'MICROBATCH': microbatch, 'DEVICE': device})
Example #5
Source File: optim.py From hyperas with MIT License | 5 votes |
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) data_string = retrieve_data_string(data, verbose, data_args) model = hyperopt_keras_model(model_string, parts, aug_parts, verbose) temp_str = temp_string(imports, model, data_string, functions_string, space) return temp_str
Example #6
Source File: testnotebooks.py From holoviews with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #7
Source File: command.py From holoviews with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #8
Source File: run_tutorials.py From botorch with MIT License | 5 votes |
def parse_ipynb(file: Path) -> str: with open(file, "r") as nb_file: nb_str = nb_file.read() nb = nbformat.reads(nb_str, nbformat.NO_CONVERT) exporter = PythonExporter() script, _ = exporter.from_notebook_node(nb) return script
Example #9
Source File: notebook_sphinxext.py From msmexplorer with MIT License | 5 votes |
def export_python(nb, destfn): exporter = PythonExporter() body, resources = exporter.from_notebook_node(nb) with open(destfn, 'w') as f: f.write(body)