Notebook Ipython/Jupyter how to make cell optional

If you want to have cell in Notebook Ipython/Jupyter which is optional and depends on the user decision whether to be executed or not you can use:

For python 2:

choice = raw_input('Would you like to write to CSV file?\n')
if(choice == 'y'):
    with open(csv_file, 'w+') as csv:
    for msg in ['a', 'b', 'c', 'd', 'e']:
        csv.write(msg + '\n')

For python 3:

choice = input('Would you like to write to CSV file?\n')
if(choice == 'y'):
    with open(csv_file, 'w+') as csv:
    for msg in ['a', 'b', 'c', 'd', 'e']:
        csv.write(msg + '\n')

This will create new window for the user answer and depending of it the rest of the code will be executed:

Notebook Ipython/Jupyter optional cell

Related Article