Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter Notebooks conversion: option to import/export metadata #1487

Closed
chrisjsewell opened this issue Jan 24, 2019 · 5 comments
Closed

Jupyter Notebooks conversion: option to import/export metadata #1487

chrisjsewell opened this issue Jan 24, 2019 · 5 comments

Comments

@chrisjsewell
Copy link

I would envisage the format in the .py file being:

#~ key1:
#~   subkey1: value

#%%
#~ key2:
#~   subkey2: value
print('hallo world')

In this format #~ denotes a metadata block, and the blocks are in YAML format.
If there is a block before any cells, this is deemed to be the notebook level metadata.,
and, if its directly below a cell initialiser #%%, it is cell level metadata.

This allows for a more 1:1 mapping between the .py and .ipynb,
and would actually make editing metadata far easier than in the standard web browser environment
(akin to knitr+Sweave feature in R Studio, as @neo-anderson mentions in microsoft/vscode-python#3919 ).

My principal use case for this is with iPyPublish,
which can control (via metadata) how cells and outputs are converted to latex/html/etc
(see here)

Obviously there could be a configuration setting to turn metadata export on/off.

@rchiodo
Copy link
Contributor

rchiodo commented Jan 28, 2019

This is useful for more than just iPyPublish. Any tool that uses metadata would need this support.

@chrisjsewell
Copy link
Author

Heres a freebee: for the importer, this is the jinja template, and associated filter you'd need:

{%- extends 'null.tpl' -%}
{% block header %}
{{ nb.metadata | meta2yaml('#~~ ') }}
{% endblock header %}
{% block codecell %}
#%%
{{ super() }}
{% endblock codecell %}
{% block in_prompt %}{% endblock in_prompt %}
{% block input %}{{ cell.metadata | meta2yaml('#~~ ') }}
{{ cell.source | ipython2python }}
{% endblock input %}
{% block markdowncell scoped %}#%% [markdown]
{{ cell.metadata | meta2yaml('#~~ ') }}
{{ cell.source | comment_lines }}
{% endblock markdowncell %}
import yaml
from nbformat.notebooknode import NotebookNode


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


def meta_to_yaml(metadata, comment="#~~ "):
    # type: (NotebookNode, str) -> str
    """convert metadata json to yaml"""
    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

Since you are calling nbconvert as an executable, I'm not sure how you parse it the filter though?

@rchiodo
Copy link
Contributor

rchiodo commented Jan 29, 2019

Thanks. Not sure either. Hopefully we can just add python in the tpl file.

@chrisjsewell
Copy link
Author

FYI, I've now been pointed towards jupytext,
which more comprehensively handles the round trip conversion of notebook and python script:

>> jupytext --to py:percent notebook.ipynb         # create a notebook.py file in the double percent format
>> jupytext --to notebook notebook.py              # overwrite notebook.ipynb (remove outputs)
>> jupytext --to notebook --update notebook.py     # update notebook.ipynb (preserve outputs)

@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Nov 13, 2020
@greazer greazer removed the P2 label Jul 26, 2021
@greazer
Copy link
Contributor

greazer commented Oct 18, 2021

Thanks for the feedback! However, we don't have plans on adding this functionality at this time.

@greazer greazer closed this as completed Oct 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants