fastai / nbdev

Create delightful software with Jupyter Notebooks
https://nbdev.fast.ai/
Apache License 2.0
4.93k stars 488 forks source link

nb_build_docs fails if show_doc is in different cell #25

Closed ConnorJL closed 4 years ago

ConnorJL commented 4 years ago

I was following the tutorial and after implementing the HelloSayer class, the nb_build_docs command failed with:

converting: /home/connor/Projects/nbdev-test/00_core.ipynb
An error occurred while executing the following cell:
------------------
show_doc(HelloSayer.say)

------------------

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-693d9388d4f4> in <module>()
----> 1 show_doc(HelloSayer.say)

NameError: name 'HelloSayer' is not defined
NameError: name 'HelloSayer' is not defined

This can be fixed by putting the show_doc(HelloSayer.say) in the same cell as the class definition, but this doesn't seem like intended behaviour so I thought I'd mention it.

The code leading to this error can be found here: https://github.com/ConnorJL/nbdev-test

sgugger commented 4 years ago

The fix hadn't made its way to the generated html. The cell with HelloSayer wasn't exported, so when building the docs, nbdev didn't know that function (it imports the corresponding module to build a page). Fixed by this commit.

svenstehle commented 4 years ago

Using show_docs in the index.ipynb even in the same cell as a function or class declaration does not work and throws an error while building the docs. It works when I use it inside the notebook cell itself though (core and index both!):

An error occurred while executing the following cell:
------------------
class HelloSayer:
    "Say hello to `to` using `say_hello`"
    def __init__(self, to): self.to = to
    def say_hello(self):
        "Say hello to somebody"
        return f'Hello {self.to}!'
    def say(self): print(say_hello(self.to))
show_doc(HelloSayer.say_hello)
------------------

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-7e1fcac685f1> in <module>
      6         return f'Hello {self.to}!'
      7     def say(self): print(say_hello(self.to))
----> 8 show_doc(HelloSayer.say_hello)

NameError: name 'show_doc' is not defined
NameError: name 'show_doc' is not defined

The funny thing is, it is creating a fine readme.md but not a index.html

LinkHS commented 4 years ago

The problem still exists.

alfl commented 3 years ago

I just encountered this. I also ran into a situation where nbdev_build_docs was throwing NameError: name 'show_doc' is not defined even after I had deleted the cell. I also edited the notebook and searched the directory and verified that the string 'show_doc' did not occur.

The solution was to run nbdev_build_lib before nbdev_build_docs. However, it did involve some back-and-forth of deleting .ipynb_checkpoints, deleting the generated library code, reverting the deletion, rebuilding the library, and then building the docs.