Correct export syntax in nbdev 2

I’m trying to run nbdev export for the course script, but it seems that there have been not one but two major version upgrades for nbdev since the last recordings.

First I run into notebook2script not working anymore.

from nbdev.export import notebook2script
notebook2script()

Then in the forum I found some help to change this into:

from nbdev.export import nb_export
nb_export('app.ipynb', '.')

There are two problems with this:

  1. I got a warning about default_exp missing, which I fixed by adding #|default_exp app at the very top of the script.
  2. The more important problem is that it just silently executes and doesn’t create any files. No error, no warning now, just no file output.

Can you tell me what is the minimal code required to make an export using nbdev 2?

1 Like

There are multiple ways to go about it.

First, you need a #|default_exp foo cell on the top of the nb, then mark the cells you wish to export with the #|export flag and then the final cell in your nb can be from nbdev import nbdev_export; nbdev_export() or run nbdev_export from the root of your repo in the terminal.

It doesn’t work. I made a minimal example, it reproduces it. Simply no file is generated.

I’m not able to reproduce this error. I just tried it on a nb and it worked as expected for me.

In order to make this work (as of Jan '24), here’s what I found (running inline in a jupyter notebook):

In order to output to file foo.py in the current/working directory:
First cell:

 #|default_exp foo

Final/export cell:

> import nbdev
> nbdev.export.nb_export('current_notebook_file.ipynb', '.')

In order to output foo.py in the ‘bar’ directory:

First cell:

#|default_exp bar.foo

Final/export cell:

import nbdev
nbdev.export.nb_export('current_notebook_file.ipynb', '.')

Unfortunately, this still doesn’t work for me. I installed via macOS with the following setup:

  1. Run brew install --cask miniconda to get the python conda package manager. You should never use the system python as it can interfere with system tasks.
  2. Run conda init zsh to get your PATH setup etc.
  3. Run conda install pytorch torchvision -c pytorch to install PyTorch
  4. Run conda install -c fastai fastai to install FastAI
  5. Run conda install jupyter to install Jupyter Notebooks
  6. Run conda install -c fastai nbdev to install the notebook helper to auto export sections of a notebook into scripts.
  7. Run conda install -c conda-forge gradio to install the Gradio package for running on HugginFace Spaces.
  8. Run jupyter notebook --no-browser to start Jupyter.

I I also tried installing jupyterlab-quarto via python -m pip install jupyterlab-quarto. Any clues as to what I could be missing?

My setup is the same as @hyperknot 's above. Same result. No error. No output.

OK, I seem to have this now working. It appears Quarto wasn’t installed correctly.

For my Apple Silicon Mac, the setup steps are:

  1. Run brew install --cask miniconda to get the python conda package manager. You should never use the syste, python as it can interfere with system tasks.
  2. Run brew install --cask quarto to get Quarto publishing package. Used to extract/export runnable scripts from jupyter notebooks via #|export directives.
  3. Run conda init zsh to get your PATH setup etc.
  4. Run conda install pytorch torchvision -c pytorch to install PyTorch
  5. Run conda install -c fastai fastai to install FastAI
  6. Run conda install jupyter to install Jupyter Notebooks
  7. Run conda install -c fastai nbdev to install the notebook helper to auto export sections of a notebook into scripts.
  8. Run conda install -c conda-forge gradio to install the Gradio package for running on HuggingFace Spaces.
  9. Run jupyter notebook --no-browser to start Jupyter.

Then, in the note book:

  1. Add a new first cell with the contents #|default_exp app. This defines the filename of the exported file.
  2. Add #|export to the top of each cell you wish to export.
  3. export via:
    import nbdev
    nbdev.export.nb_export('NOTEBOOK_NAME.ipynb','.')
    
  4. This should create an app.py file in the same directory as your notebook when run. Also, with my setup at least, I found I needed to save the notebook via a command-S to ensure changes to the export directives (#|default_exp app, #|export, etc.) took effect.