#!/usr/bin/env python # coding: utf-8 # # Creating command line scripts from a notebook # # With the `importnb` extension installed by default IPython can run notebooks as modules. The notebook can be a great interactive interface for develiping applications. # ## [Click](http://click.pocoo.org) command line library # In[1]: import click # ## [A simple example](http://click.pocoo.org/5/commands/) # In[2]: @click.group() @click.option('--debug/--no-debug', default=False) def cli(debug): click.echo('Debug mode is %s' % ('on' if debug else 'off')) @cli.command() def sync(): click.echo('Synching') # ## Running the application # # The command line application will be run in the __main__ context and it will have a __file__ attribute. Otherwise, we test the function on the command line. We must use IPython and know that our `importnb` loaders and included. # In[2]: if globals().get('__file__', None) and __name__ == '__main__': cli() def _demonstrate_click_parser_for_a_notebook(): from IPython import get_ipython get_ipython().system('ipython -m deathbeds.2018-07-15-click-arguments-from-a-notebook -- --debug sync') # In[ ]: