I'm using nbconvert to execute an iPython notebook via the command line (as in this answer):
ipython nbconvert --to=html --ExecutePreprocessor.enabled=True RunMe.ipynb
Is it possible to pass command line arguments to be accessed from within the notebook (like sys.argv)?
This would let me reuse the same notebook in different contexts.
2 Answers 2
You can access environmental variables instead. I have yet to come across a way to use command line arguments directly.
4 Comments
NB_ARGS='--par1 val1 --par2 val2' jupyter nbconvert mynotebook.html --execute --stdout > myoutput.html and use argparse, os inside the notebook, as parser.parse_args(os.environ['NB_ARGS']). At least there is only one variable involved...This is my attempt at a single module for argument parsing in Python scripts and Jupyter notebooks. It only supports minimalist key=value style arguments but meets my needs.
I nbconvert it to a Python script named Args.py and use it as shown.
Then I can batch run a notebook like:
jpn mynotebook limit=5 assignment=A2