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?
Once you've entered your specific folder with Windows Explorer, you can simply press ALT + D, type in cmd and press Enter. You can then type jupyter notebook to launch Jupyter Notebook within that specific folder.
Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With