fastai / nbdev1

nbdev v1 (old version)
https://nbdev1.fast.ai/
Apache License 2.0
1 stars 2 forks source link

Warning shows up when running notebook2html #3

Open kevin-vitro opened 3 months ago

kevin-vitro commented 3 months ago

I was running the function notebook2html and realized the following error message shows up when running the function:

/opt/conda/lib/python3.9/site-packages/nbconvert/exporters/exporter.py:348: MissingIDFieldWarning: Cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  _, nbc = validator.normalize(nbc)

I tracked this down to execute_nb and specifically this line:

nb['cells'].insert(0, _import_show_doc_cell(mods))
def _import_show_doc_cell(mods=None):
    "Add an import show_doc cell."
    source = f"from nbdev.showdoc import show_doc"
    if mods is not None:
        for mod in mods: source += f"\nfrom {get_config().lib_name}.{mod} import *"
    return {'cell_type': 'code',
            'execution_count': None,
            'metadata': {'hide_input': True}, #################Missing id column here
            'outputs': [],
            'source': source}

It appears that the issue is that we aren't including an 'id' attribute in the dictionary. I think this could be fixed by adding an id and creating a random uuid for every id or something like that. I'm not sure if this is something that would be worth updating in nbdev1 since it doesn't appear this function is used in nbdev v2, but it took me a while to dig to the bottom of this so I wanted to share it in case it saves anybody else time.

Here is an example of a "valid" cell:

{'cell_type': 'markdown',
   'id': 'infinite-strengthening',
   'metadata': {},
   'source': ''},
kevin-vitro commented 3 months ago

here is what my proposed fix would be:

from uuid import uuid4

def _import_show_doc_cell(mods=None):
    "Add an import show_doc cell."
    source = f"from nbdev.showdoc import show_doc"
    if mods is not None:
        for mod in mods: source += f"\nfrom {get_config().lib_name}.{mod} import *"
    return {'cell_type': 'code',
            'execution_count': None,
            'id': str(uuid4()),
            'metadata': {'hide_input': True},
            'outputs': [],
            'source': source}