Passing Command Line Arguments to Argv in Jupyter/Ipython Notebook

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,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.

Running a Python script in Jupyter Notebook, with arguments passing

You need to use sys.argv instead of sys.stdin.read():

two_digits.py

import sys
args = sys.argv # a list of the arguments provided (str)
print("running two_digits.py", args)
a, b = int(args[1]), int(args[2])
print(a, b, a + b)

command line / jupyter magic line:

%run two_digits 3 5

or, with a slightly different output:

Note: this uses a ! prefix to indicate command line to jupyter

!ipython two_digits.py 2 3

output: (using magic line %run)

running two_digits.py ['two_digits.py', '2', '3']
2 3 5

Passing arguments in Jupyter Notebook

In Jupyter Notebook, you can create a file using the cell magic %%file. You can then send a command to the shell to run the file using the cell magic %%!.

To write out the file:

%%file ex13.py
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

To run the file:

%%!
python ex13.py first 2nd 3rd

You should see the results you are looking for. The printed output is captured and returned as a list, one element per printed line.

How to pass command line arguments to ipython

You can use one -- more option before that:

ipython  script.py -- --argument blah

Help of Ipython:

ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...

If invoked with no options, it executes the file and exits, passing the
remaining arguments to the script, just as if you had specified the same
command with python. You may need to specify `--` before args to be passed
to the script, to prevent IPython from attempting to parse them. If you
specify the option `-i` before the filename, it will enter an interactive
IPython session after running the script, rather than exiting.

Demo:

$ cat script.py 
import sys
print(sys.argv)

$ ipython script.py -- --argument blah
['script.py', '--argument', 'blah']

$ ipython script.py -- arg1 arg2
['script.py', 'arg1', 'arg2']

how to use argument parser in jupyter notebook

What you are asking seems similar to: Passing command line arguments to argv in jupyter/ipython notebook

There are two different methods mentioned in the post that were helpful. That said, I would suggest using command line tools and a Python IDE for writing scripts to run machine learning models. IPython may be helpful for visualization, fast debugging or running pre-trained models on commonly available datasets.



Related Topics



Leave a reply



Submit