A minimal template for a 'command line notebook' to manage an nbdev project in Google Colaboratory.
#all_flag
# remove this cell to use with tests
from google.colab import drive
drive.mount('/content/drive')
%load_ext autoreload
%autoreload 2
!pip install nbdev
!pip install fastcore
!pip install nbd_colab

Library imports

import os
from nbdev import *
from nbdev.showdoc import *
from nbdev_colab import *
from pathlib import Path
from getpass import getpass
import urllib

Clone a new repository to Google Drive

clone_new_repo()
# after successful clone you should be in the cloned repo directory - check
!pwd
!ls
# verify the clone by printing out (and checking) settings.ini
! cat settings.ini

If the repo cloned successfully, but either git user configuration or installing git hooks failed then use the following code to perform these tasks manually.

!git config user.email "<github username>"
!git config user.name "<email>"
!nbdev_install_git_hooks

Building and testing project files with nbdev

# check the CWD is the desired project directory
% cd /content/drive/My Drive/nbd_colab
!pwd
!ls
!nbdev_build_lib
!nbdev_build_docs
!nbdev_diff_nbs

If nbdev_diff_nbs gives an output, run either notebook2script() or script2notebook() to fix the conflict.

To keep the notebook code (most common)

# from nbdev.export import notebook2script
# notebook2script()

or, to keep the script code:

# from nbdev.sync import script2notebook
# script2notebook()

Run tests. Don't forget the optional --flags attribute if you want to overide test_flags.

!nbdev_test_nbs

Push to GitHub.

# check the CWD is the desired project directory
% cd /content/drive/My Drive/nbd_colab
!pwd
!ls
!git status
!git add -A
!git status
!git commit -am "commit msg"
!git push

Occasionally we'll want to pull as well

# !git pull

Upload package to PyPi

To upload your library to PyPi as a package, there must be a valid _pypirc file in the user's home directory on Google drive (see nbdev docs to set this up)

! pip install twine
# check the CWD is the desired project directory
% cd /content/drive/My Drive/nbd_colab
!pwd
!ls
! make release

If 'make release' fails with 'file already exists' error, it is likely because of incompatible version numbers between the project repo and PyPi. Solve by bumping the repo version to match PyPi's. This can be done with the nbdev_bump or nbdev_bump_version(part="Part of version to bump") or by simply editing settings.ini to match the PyPi release number.

To bumps the repo version number by 0.0.1

# !nbdev_bump

or, to increment a part of the version number by 1

# !nbdev_bump_version(part="Part of version to bump")