本文整理汇总了Python中nbformat.NO_CONVERT属性的典型用法代码示例。如果您正苦于以下问题:Python nbformat.NO_CONVERT属性的具体用法?Python nbformat.NO_CONVERT怎么用?Python nbformat.NO_CONVERT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类nbformat
的用法示例。
在下文中一共展示了nbformat.NO_CONVERT属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_can_run_examples_jupyter_notebooks
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def test_can_run_examples_jupyter_notebooks(self):
print("Examples folder ", self.examples_folder)
for filename in os.listdir(self.examples_folder):
if not filename.endswith('.ipynb'):
continue
path = os.path.join(self.examples_folder, filename)
notebook = nbformat.read(path, nbformat.NO_CONVERT)
state = {}
for cell in notebook.cells:
if cell.cell_type == 'code' and not is_matplotlib_cell(cell):
try:
exec(strip_magics_and_shows(cell.source), state)
# coverage: ignore
except:
print('Failed to run {}.'.format(path))
raise
示例2: get_pipeline_parameters
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def get_pipeline_parameters(request, source_notebook_path):
"""Get the pipeline parameters tagged in the notebook."""
# read notebook
log = request.log if hasattr(request, "log") else logger
try:
notebook = nbformat.read(source_notebook_path,
as_version=nbformat.NO_CONVERT)
params_source = parser.get_pipeline_parameters_source(notebook)
if params_source == '':
raise ValueError("No pipeline parameters found. Please tag a cell"
" of the notebook with the `pipeline-parameters`"
" tag.")
# get a dict from the 'pipeline parameters' cell source code
params_dict = ast.parse_assignments_expressions(params_source)
except ValueError as e:
log.exception("Value Error during parsing of pipeline parameters")
raise RPCInternalError(details=str(e), trans_id=request.trans_id)
# convert dict in list so its easier to parse in js
params = [[k, *v] for k, v in params_dict.items()]
log.info("Pipeline parameters:")
for ln in tabulate(params, headers=["name", "type", "value"]).split("\n"):
log.info(ln)
return params
示例3: get_pipeline_metrics
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def get_pipeline_metrics(request, source_notebook_path):
"""Get the pipeline metrics tagged in the notebook."""
# read notebook
log = request.log if hasattr(request, "log") else logger
try:
notebook = nbformat.read(source_notebook_path,
as_version=nbformat.NO_CONVERT)
metrics_source = parser.get_pipeline_metrics_source(notebook)
if metrics_source == '':
raise ValueError("No pipeline metrics found. Please tag a cell"
" of the notebook with the `pipeline-metrics`"
" tag.")
# get a dict from the 'pipeline parameters' cell source code
metrics = ast.parse_metrics_print_statements(metrics_source)
except ValueError as e:
log.exception("Failed to parse pipeline metrics")
raise RPCInternalError(details=str(e), trans_id=request.trans_id)
log.info("Pipeline metrics: {}".format(metrics))
return metrics
示例4: append_scrapbook_commands
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def append_scrapbook_commands(input_nb_path, output_nb_path, scrap_specs):
notebook = nbf.read(input_nb_path, as_version=nbf.NO_CONVERT)
scrapbook_cells = []
# Always need to import nteract-scrapbook
scrapbook_cells.append(nbf.v4.new_code_cell(source="import scrapbook as sb"))
# Create a cell to store each key and value in the scrapbook
for k, v in scrap_specs.items():
source = "sb.glue(\"{0}\", {1})".format(k, v.code)
scrapbook_cells.append(nbf.v4.new_code_cell(source=source))
# Append the cells to the notebook
[notebook['cells'].append(c) for c in scrapbook_cells]
# Write out the new notebook
nbf.write(notebook, output_nb_path)
示例5: list_examples
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def list_examples(self):
all_examples = []
for category in self.get_categories():
directory = os.path.join(self.sample_notebook_dir, category)
filepaths = glob(os.path.join(directory, '**', '*.ipynb'), recursive=True)
examples = [{'filepath': os.path.abspath(fp)} for fp in filepaths]
for example in examples:
node = nbformat.read(example['filepath'], nbformat.NO_CONVERT)
example['filename'] = os.path.basename(example['filepath'])
example['metadata'] = node.metadata
example['category'] = category
example['basename'] = os.path.basename(example['filepath'])
example['supporting_items'] = self.get_supporting_items(example['filepath'], example['filename'])
notebook_folder_location = os.path.split(example['filepath'])[0]
example['notebook_folder_name'] = os.path.split(notebook_folder_location)[1]
all_examples.extend(examples)
return all_examples
示例6: convert
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def convert(self, notebook_file, sos_file, args=None, unknown_args=None):
'''
Convert a ipython notebook to sos format.
'''
if unknown_args:
raise ValueError(f'Unrecognized parameter {unknown_args}')
exporter = SoS_Exporter()
notebook = nbformat.read(notebook_file, nbformat.NO_CONVERT)
output, _ = exporter.from_notebook_node(notebook, {})
if not sos_file:
sys.stdout.write(output)
elif isinstance(sos_file, str):
with open(sos_file, 'w') as sos:
sos.write(output)
env.logger.info(f'SoS script saved to {sos_file}')
else:
sos_file.write(output)
#
# notebook to HTML
#
示例7: read_source_script
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [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
示例8: render_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def render_notebook(filename: Path, replacements: Dict[str, str]) -> str:
""" Takes path to the notebook, returns executed and rendered version
:param filename: notebook
:param replacements: dictionary with text replacements done before executing
:return: notebook, rendered as string
"""
with filename.open('r') as f:
nb_as_str = f.read()
for original, replacement in replacements.items():
nb_as_str = nb_as_str.replace(original, replacement)
nb = nbformat.read(StringIO(nb_as_str), nbformat.NO_CONVERT)
ep = ExecutePreprocessor(timeout=60, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': str(filename.parent.absolute())}})
result_as_stream = StringIO()
nbformat.write(nb, result_as_stream)
return result_as_stream.getvalue()
示例9: append_scrapbook_commands
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def append_scrapbook_commands(input_nb_path, output_nb_path, scrap_specs):
notebook = nbf.read(input_nb_path, as_version=nbf.NO_CONVERT)
scrapbook_cells = []
# Always need to import nteract-scrapbook
scrapbook_cells.append(nbf.v4.new_code_cell(source='import scrapbook as sb'))
# Create a cell to store each key and value in the scrapbook
for k, v in scrap_specs.items():
source = "sb.glue(\"{0}\", {1})".format(k, v)
scrapbook_cells.append(nbf.v4.new_code_cell(source=source))
# Append the cells to the notebook
[notebook['cells'].append(c) for c in scrapbook_cells]
# Write out the new notebook
nbf.write(notebook, output_nb_path)
示例10: test_tutorial_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def test_tutorial_notebook():
pytest.importorskip('nbformat')
pytest.importorskip('nbconvert')
pytest.importorskip('matplotlib')
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
rootdir = os.path.join(aospy.__path__[0], 'examples')
with open(os.path.join(rootdir, 'tutorial.ipynb')) as nb_file:
notebook = nbformat.read(nb_file, as_version=nbformat.NO_CONVERT)
kernel_name = 'python' + str(sys.version[0])
ep = ExecutePreprocessor(kernel_name=kernel_name)
with warnings.catch_warnings(record=True):
ep.preprocess(notebook, {})
示例11: clear_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def clear_notebook(old_ipynb, new_ipynb):
with io.open(old_ipynb, 'r') as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
remove_outputs(nb)
with io.open(new_ipynb, 'w', encoding='utf8') as f:
nbformat.write(nb, f, nbformat.NO_CONVERT)
示例12: test_can_run_examples_jupyter_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def test_can_run_examples_jupyter_notebook(path):
notebook = nbformat.read(path, nbformat.NO_CONVERT)
state = {} # type: Dict[str, Any]
for cell in notebook.cells:
if cell.cell_type == 'code' and not is_matplotlib_cell(cell):
try:
exec(strip_magics_and_shows(cell.source), state)
# coverage: ignore
except:
print('Failed to run {}.'.format(path))
raise
示例13: load_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def load_notebook(filename):
try:
return nbformat.read(filename, nbformat.NO_CONVERT)
except FileNotFoundError:
raise Exception(f"{filename} Not found in current directory")
示例14: _save_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def _save_notebook(self, path, nb):
"""
Uploads notebook to GCS.
:param path: blob path.
:param nb: :class:`nbformat.notebooknode.NotebookNode` instance.
:return: created :class:`google.cloud.storage.Blob`.
"""
bucket_name, bucket_path = self._parse_path(path)
bucket = self._get_bucket(bucket_name, throw=True)
data = nbformat.writes(nb, version=nbformat.NO_CONVERT)
blob = bucket.blob(bucket_path)
blob.upload_from_string(data, "application/x-ipynb+json")
return blob
示例15: _save_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import NO_CONVERT [as 别名]
def _save_notebook(self, path, nb):
"""Save a notebook to an os_path."""
s = nbformat.writes(nb, version=nbformat.NO_CONVERT)
self._pyfilesystem_instance.writetext(path, s)