nbdev
nbdev setup on WSL
On Ubuntu 22.04
- Install
Ubuntu 22.04on WSL in Windows if you have not already done so. - Open the WSL window
- Setup your github SSH keys.
- Install
fastsetuppackage from fastAI.git clone https://github.com/fastai/fastsetup.git
cd fastsetup
source setup-conda.sh
. ~/.bashrc
conda install -yq mamba - Setup your python environment
conda create -n nbdev python=3.9 - (Optional) Add the alias
caforconda activatein your .bashrcalias ca="conda activate ''" - Install
nbdevpip install nbdev - (Optional) Create
.tmux.conffile in your home directory and copy the content of this dot file. - (Optional) Use
tmuxto work on your development.
Development with nbdev
- If
Jupyter notebookis not installed. Install it first.pip install notebook - Create a git repo. (Initializing it in github and cloning would be ideal as nbdev will automatically pickup information from there.)
- Check the next steps from this tutorial.
nbdev + Colab
Pushing a new notebook to github and then pulling it to colab is a tedious process. Since Colab is a transient environment, you can't really use SSH keys to push/pull from github. Hence, we need to use Personal Access Tokens (PATs) and clone a repo on the Google Drive with https.
To do these git operations, we need to use terminal in Colab and the terminal is only accessible in Colab PRO accounts.
To upgrade to Colab Pro, you need to upgrade your current Colab subscription. GQC already has a Colab Pro subscription.
Once you have the Colab Pro Subscription, you can access the terminal from the bottom left corner.
This assumes we already have setup a git repo with nbdev template. Written on the basis of migrating already existing colab notebook to nbdev. Requires a
Colab PROaccount as we need terminal access in Colab.
In Colab
- Open a new
Colabnotebookand run following.from google.colab import drive
drive.mount('/content/drive')
!pip install nbdev - Open the terminal in
Colaband goto/content/drive/MyDrive/reposdirectory. - Clone the required repo via
HTTPS(Pick the git clone command which has thehttpsaddress). Provide your github user-name when asked for that and provide a token-key which you can create from github.com as Password. (See here if you don't already have a github token) - Create and checkout a new branch for yourself. Name it like
colab-pavan,colab-deven, etc. - Using
Google Drive, move any colab notebooks to the appropriate sub-folder in repo'snbsfolder. - Rename the
Colab notebooksif necessary. - Then
git addthe new notebooks,git committhem and finallygit pushthem. - After the data is pushed to the new branch, we have to merge the new branch to the main branch. That can be done by either of,
- Creating a
Pull Requestthrough GitHub. - Merging the changes manually by pulling both
mainandcolab-<your_name>branch toWSL
- Creating a
- Continue working on cleaning up on the notebooks through
WSLby pulling in the latest changes to it.
In WSL
nbdev has a slightly steep learning curve.
Before you start developing with nbdev, it is best that you either finish the written tutorial or video tutorial completely.
nbdevis a python package not a command.- If you wish to see if
nbdevis installed in your virtual environment, typepip show nbdev. - If you were to run
nbdev --helpin your terminal, you will get a command not found error. - Once,
nbdevis installed; in the terminal, runnbdev_helpto see all the available commands. - Sometimes, running nbdev commands on notebooks with outputs can throw errors and result in nbdev CI failing. This can be hard to trace back with the non-descriptive errors. In such cases, try clearing outputs before using nbdev_export or nbdev_clean.
- Pull in the latest changes to the repo.
- If not done already, create a new python virtual environment for the repo.
For example:
conda create -n gqc_utils python=3.9 - Activate the python environment.
ca gqc_utils - If running for the first time, install
nbdevand upgrade quarto in the virtual environment.pip install nbdev
nbdev_install_quarto
nbdev_install_hooks - If not already done, install the dependancies of the current package (which are specified in the
settings.ini) byor install submodule specific dependencies viapip install -e .pip install -e '.[vision]' - Useful nbdev commands (See a comphensive list here)
nbdev_clean: Cleans notebook runtime metadata which causes merge conflicts if not cleaned.nbdev_preview: Locally builds and serves docs.nbdev_export: export the notebooks to py files- Add the
#|default_exp fooas the first cell on top of each notebook. (This will export the contents of the notebook to a py file calledfoo.py) - Add the
#|exportflag on top of each cell that you wish to export. - Finally, from the root of the repo, run the command
nbdev_exportto export the notebooks to py files. - Optional: Learn about other nbdev flags → here
- Add the
nbdev_prepare: To export, test and clean all the notebooks.