Cells can be hidden (with #hide). Cells with code that we want to export get an #export flag and will be placed into whatever file is specified in the first cell (core.py
in this case). All other cells are treated as tests and will be executed as part of the CI included with NBDEV. In some cases we don't want to run cells every time - they can be marked with #slow or #skiptest and going forward will then only be run if we explicitly decide to do this.
Let's start by exporting some demo code:
We can run this code:
demo_code()
To make this a 'test' is simple:
assert str(demo_code()) == 'demo'
Here's a cell that I want to run now but would be too slow for testing. In fact, it's one I never want to run:
import time # This import should really be in a separate cell, especially if time is used elsewhere
time.sleep(10)
We can export the code from these notebooks and build the docs from the command line, but it is also useful to export the code from within the notebook. So the last cell here does just that:
from nbdev.export import notebook2script; notebook2script()