Passing command line arguments to argv in jupyter/ipython notebook Passing command line arguments to argv in jupyter/ipython notebook python python

Passing command line arguments to argv in jupyter/ipython notebook


After a lot of looking around I found very cumbersome, custom libraries, but solved it with a few lines of code which I thought was pretty slick. I used nbconvert to end up with an html report as output that contains all graphics and markdown from the notebook, but accepts command line parameters just as always through a minimal python wrapper:

The python file test_args.py (which takes command line params as normal):

import sys,osIPYNB_FILENAME = 'test_argv.ipynb'CONFIG_FILENAME = '.config_ipynb'def main(argv):    with open(CONFIG_FILENAME,'w') as f:        f.write(' '.join(argv))    os.system('jupyter nbconvert --execute {:s} --to html'.format(IPYNB_FILENAME))    return Noneif __name__ == '__main__':    main(sys.argv)

The notebook contains:

import sys,os,argparsefrom IPython.display import HTMLCONFIG_FILE = '.config_ipynb'if os.path.isfile(CONFIG_FILE):    with open(CONFIG_FILE) as f:        sys.argv = f.read().split()else:    sys.argv = ['test_args.py', 'input_file', '--int_param', '12']parser = argparse.ArgumentParser()parser.add_argument("input_file",help="Input image, directory, or npy.")parser.add_argument("--int_param", type=int, default=4, help="an optional integer parameter.")args = parser.parse_args()p = args.int_paramprint(args.input_file,p)

and I can run the python notebook with arguments parsed as usual:

python test_args.py my_input_file --int_param 12

I tend to paste the block with argparse calls into the python wrapper so that command line errors are caught by the python script and -h works properly.


There are two projects I've found that do what you ask for

  • Papermill, will add a cell to your notebook with arguments that you pass to it on the command line. So this is quite straightforward, you define your defaults in the first cell (the should have parameters tag)
  • nbparameterise it is a similar concept but you don't tag your cell with defaults, it has to be first.

Here is a good resource discussing the issue: https://github.com/jupyter/help/issues/218


I think this Gist may help you : https://gist.github.com/gbishop/acf40b86a9bca2d571fa

This is an attempt at a simple argument parser for mostly key=value pairs that can be used both on the command line and in IPython notebooks. It support query parameters in notebook URLs and a Run command for notebooks.