KeyError: 'state' when using nbdev_preview

After adding a blog to my nbdev project, I cannot use nbdev_preview due to the following error:

(base) kappa@keepo:~/git/delicious-nbdev$ nbdev_preview 
concurrent.futures.process._RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/process.py", line 243, in _process_worker
    r = call_item.fn(*call_item.args, **call_item.kwargs)
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/process.py", line 202, in _process_chunk
    return [fn(*args) for args in chunk]
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/process.py", line 202, in <listcomp>
    return [fn(*args) for args in chunk]
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/parallel.py", line 46, in _call
    return g(item)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/serve_drv.py", line 22, in main
    if src.suffix=='.ipynb': exec_nb(src, dst, x)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/serve_drv.py", line 16, in exec_nb
    cb()(nb)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/processors.py", line 231, in __call__
    def __call__(self, nb): return self.nb_proc(nb).process()
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/process.py", line 126, in process
    for proc in self.procs: self._proc(proc)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/process.py", line 120, in _proc
    if hasattr(proc,'end'): proc.end()
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/processors.py", line 210, in end
    widgets = {**old, **new, 'state': {**old['state'], **new['state']}}
KeyError: 'state'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/kappa/miniconda3/bin/nbdev_preview", line 8, in <module>
    sys.exit(nbdev_preview())
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/script.py", line 119, in _f
    return tfunc(**merge(args, args_from_prog(func, xtra)))
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/quarto.py", line 270, in nbdev_preview
    cache,cfg,path = _pre_docs(path, n_workers=n_workers, **kwargs)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/quarto.py", line 174, in _pre_docs
    cache = proc_nbs(path, n_workers=n_workers, **kwargs)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/nbdev/serve.py", line 76, in proc_nbs
    parallel(nbdev.serve_drv.main, files, n_workers=n_workers, pause=0.01, **kw)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/parallel.py", line 117, in parallel
    return L(r)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/foundation.py", line 98, in __call__
    return super().__call__(x, *args, **kwargs)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/foundation.py", line 106, in __init__
    items = listify(items, *rest, use_list=use_list, match=match)
  File "/home/kappa/miniconda3/lib/python3.9/site-packages/fastcore/basics.py", line 66, in listify
    elif is_iter(o): res = list(o)
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/process.py", line 559, in _chain_from_iterable_of_lists
    for element in iterable:
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/_base.py", line 609, in result_iterator
    yield fs.pop().result()
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/_base.py", line 446, in result
    return self.__get_result()
  File "/home/kappa/miniconda3/lib/python3.9/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
KeyError: 'state'

It started to work when I took my blogs directory out of nbs directory and place it in the root of the repository.
However, another problem with this approach is that changes to files do not take any effect.
For instance, this file, [3/6] blog/posts/2022-11-07-alien-vs.-ghost-pt1/alien_vs_ghost.qmd, does not exist anymore, but nbdev_preview still wants to preview the file that doesn’t exist.

Preparing to preview
[1/6] index.ipynb
[2/6] blog/index.qmd
[3/6] blog/posts/2022-11-07-alien-vs.-ghost-pt1/alien_vs_ghost.qmd

Is there any way to reset nbdev without reinstalling?
I have tried nbdev_clean, but it did not work.
Funny thing is, my blog works fine on github.

Thank you.

Can you try the new version I just released?

2 Likes

Thank you very much Jeremy.
It worked!!
You are the best!

I deserve no credit for this one - I didn’t fix the bug :slight_smile:

Thanks for the suggestion. I had no idea where to begin.
And thanks for fastai and nbdev! I’m having a lot of fun with those! :smile:

2 Likes