Source code for ipypublish.filters.meta_to_yaml

import yaml
from nbformat.notebooknode import NotebookNode

[docs]def recurse_convert(node): # type: (NotebookNode) -> dict """convert notebook node to dict""" dct = {} for key, val in node.items(): if isinstance(val, NotebookNode): dct[key] = recurse_convert(val) else: dct[key] = val return dct
[docs]def meta_to_yaml(metadata, comment="#~~ "): # type: (NotebookNode, str) -> str """convert metadata json to yaml Parameters ---------- metadata: nbformat.notebooknode.NotebookNode comment="": str append to the start of each line Returns ------- string: str """ metadata = recurse_convert(metadata) string = yaml.dump(metadata, default_flow_style=False) if comment: string = "\n".join([comment + s for s in string.splitlines()]) return string