81

I'm wondering if it's possible to populate sys.argv (or some other structure) with command line arguments in a jupyter/ipython notebook, similar to how it's done through a python script.

For instance, if I were to run a python script as follows:

python test.py False

Then sys.argv would contain the argument False. But if I run a jupyter notebook in a similar manner:

jupyter notebook test.ipynb False

Then the command line argument gets lost. Is there any way to access this argument from within the notebook itself?

justadampaul
  • 979
  • 1
  • 7
  • 11
  • 1
    No. Notebooks are often loaded from the notebook dashboard, so it wouldn't make much sense for them to rely on command line arguments. If you're interested in passing input variables into a notebook, have a look at [nbparameterise](https://github.com/takluyver/nbparameterise), which has a different take on how to do it. – Thomas K Jun 02 '16 at 16:09

11 Answers11

44

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,os
IPYNB_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 None

if __name__ == '__main__':
    main(sys.argv)

The notebook contains:

import sys,os,argparse
from IPython.display import HTML
CONFIG_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_param
print(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.

Steve Molly
  • 464
  • 5
  • 2
  • Hey, cool! Thanks for stopping by. I had long since moved on to other projects and I'm sure I found some easy way around my problem back then, or else employed one of the cumbersome libraries you mention. But this is a neat little fix and I'll mark it as the answer for any future enquirers. – justadampaul Feb 01 '18 at 06:57
  • In test_args.py, you might want to remove `IPYNB_FILENAME` and receive the notebook's filename as the first command line argument. This will make test_args.py a generic script to apply to any notebook requiring arguments. Otherwise, you need an additional script per notebook. – amka66 Jan 07 '21 at 23:35
23

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

Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
  • 3
    Papermill looks excellent. Thanks for the updated information. – justadampaul Nov 15 '18 at 20:30
  • 1
    Thank you for sharing Papermill, after lots of searching online I wouldn't have found this library otherwise! – UrbanConor Mar 01 '21 at 11:44
  • 1
    papermill is great and actually solved my problem needing to run jupyter notebook with arguments. I recommend this blog for quick start: https://towardsdatascience.com/introduction-to-papermill-2c61f66bea30 – Rachel Shalom Apr 27 '21 at 14:27
17

If the goal is to run a notebook with configurable arguments passed from commandline, I think the easiest way is to use environment variables, like this:

NB_ARGS=some_args jupyter nbconvert --execute --to html --template full some_notebook.ipynb

Then in the notebook, you can import os and use os.environ['NB_ARGS']. The variable value can be some text that contains key-value pairs or json for example.

shaoyl85
  • 1,854
  • 18
  • 30
  • Note that the syntax to set the environment variable will be different on Windows. In a batch file, you can have `set NB_ARGS=some_args` followed by the `jupyter` command on the next line. – Joseph Nov 19 '20 at 09:56
  • 1
    This is by far the simplest and cleanest way to pass arguments to a Jupyter Notebook – M.E. Feb 03 '22 at 00:35
13

At the top of the Jupyter cell, put a line like:

%%python - --option1 value1 --option2 value2 --etc

In your example:

%%python - True

This will run your script like in a command line with the args provided.

Example:

%%python - --option1 value1 --option2 value2 --etc

import sys

if __name__ == '__main__':
    print(sys.argv)

will output:

['-', '--option1', 'value1', '--option2', 'value2', '--etc']

Hope it helps.

Whatwatt
  • 346
  • 3
  • 6
  • This one worked without the need of `'-'` as the first element in the string. – Prakhar Sharma Nov 04 '22 at 13:37
  • 1
    Thanks for your feedback. Without the `-` I get the following error: `unknown option --option1 usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Try 'python -h' for more information.` – Whatwatt Nov 09 '22 at 11:06
10

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.

Spawnrider
  • 1,727
  • 1
  • 19
  • 32
  • 2
    A link to a solution is welcome, but your answer must be self-contained and useful even with the link: please *quote or summarize* the relevant information from the page you're linking to, in case that page becomes unavailable. – Cody Gray - on strike Sep 29 '17 at 15:13
10

Using args = parser.parse_args(args=[]) would work.

or for testing, you can declare it as class format.

class Args:
  data = './data/penn'
  model = 'LSTM'
  emsize = 200
  nhid = 200

args=Args()
sngjuk
  • 927
  • 1
  • 10
  • 24
2

sys.argv yields a list, so I used

sys.argv.append('hello')

in a jupyter notebook, which allowed me to append extra members and pretend like I'm passing in arguments from the command line.

crytting
  • 181
  • 1
  • 9
2

I tried out the answers listed above, and came up with a different solution.

My original code was

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
ap.add_argument("-y", "--yolo", required=True, help="base path to YOLO directory")
ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections")
ap.add_argument("-t", "--threshold", type=float, default=0.3, help="threshold when applying non-maxima suppression")
args = vars(ap.parse_args())

I tried to make a Class as

Class Args():
    image='photo.jpg'
    yolo='yolo-coco'
    confidence=0.5
    threshold=0.3

args=Args()

but futher code snippets were producing an error.

So I printed args after vars(ap.parse_args()) and found that it was a dictionary.

So just create a dictionary for the original args:

args={"image":  'photo.jpg', "yolo":  'yolo-coco', "confidence": 0.5,"threshold": 0.3}
RugsB3
  • 41
  • 1
1

A workaround is to make the jupyter notebook read the arguments from a file. From the command line, modify the file and run the notebook.

Tomas G.
  • 3,784
  • 25
  • 28
1

I assume that you just want to parse some arguments to the notebooks, but it's not necessary to use the command line.

If you want to parse commands like.

python script.py --a A --b B

You can use the following code in the notebook:

cmd = '--a A --b B'
args = args = parser.parse_args(cmd)

For parse_args, you can find more information here.

Muzhi
  • 109
  • 1
  • 5
0

A simple and naïve solution is to put the following snippet at the first line of your program:

import sys
sys.argv = "your expected command line arguments here".split()

After executing this command, packages like argparse will work well.

So, you can just run your scripts in the jupyter lab server, without opening a terminal and typing your arguments.