Passing command line arguments to argv in jupyter/ipython notebook

前端 未结 8 1279
-上瘾入骨i
-上瘾入骨i 2020-11-28 09:34

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

相关标签:
8条回答
  • 2020-11-28 10:26

    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.

    0 讨论(0)
  • 2020-11-28 10:29

    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.

    0 讨论(0)
提交回复
热议问题