本文整理汇总了Python中nbconvert.PythonExporter方法的典型用法代码示例。如果您正苦于以下问题:Python nbconvert.PythonExporter方法的具体用法?Python nbconvert.PythonExporter怎么用?Python nbconvert.PythonExporter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbconvert
的用法示例。
在下文中一共展示了nbconvert.PythonExporter方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_source_script
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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
示例2: export_python
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
def export_python(nb, destfn):
exporter = PythonExporter()
body, resources = exporter.from_notebook_node(nb)
with open(destfn, 'w') as f:
f.write(body)
示例3: export_python
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
def export_python(nb, destfn):
exporter = PythonExporter()
body, resources = exporter.from_notebook_node(nb)
with open(destfn, 'w') as f:
f.write(body)
示例4: test_run_notebooks
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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})
示例5: get_hyperopt_model_string
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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
示例6: apply_preprocessors
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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
示例7: export_to_python
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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
示例8: parse_ipynb
# 需要导入模块: import nbconvert [as 别名]
# 或者: from nbconvert import PythonExporter [as 别名]
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