stop executing cells programatically

12,500 views
Skip to first unread message

John Marino

unread,
Feb 15, 2017, 3:11:02 PM2/15/17
to Project Jupyter
As I develop a notebook, I like to store tester code down at the bottom.  But this means I can't Run All or Run All Below without the tester code running.  (Yes, I can scroll down and Run All Above.)  Is there something I can enter into a cell that would keep the notebook from continuing to run cells?  I don't want to halt the kernel or interrupt what is running (i.e. I don't want an exception thrown).  I'm thinking a magic, e.g. %stop, would do the trick.  (I looked at the existing magics, but didn't find anything like this.)

Paul Hobson

unread,
Feb 15, 2017, 3:44:07 PM2/15/17
to jup...@googlegroups.com
One option would be raise an exception in a new cell above your tester cell.

Another option would be to a variable defined in the notebooks first cell (TESTING=False). You can then wrap your tests in an if testing: block, setting testing to True if you actually want them to run.

On Wed, Feb 15, 2017 at 12:11 PM, John Marino <john.d...@gmail.com> wrote:
As I develop a notebook, I like to store tester code down at the bottom.  But this means I can't Run All or Run All Below without the tester code running.  (Yes, I can scroll down and Run All Above.)  Is there something I can enter into a cell that would keep the notebook from continuing to run cells?  I don't want to halt the kernel or interrupt what is running (i.e. I don't want an exception thrown).  I'm thinking a magic, e.g. %stop, would do the trick.  (I looked at the existing magics, but didn't find anything like this.)

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/75cbcc60-69d6-4bc3-bccc-70dfce541c40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Marino

unread,
Feb 16, 2017, 8:05:43 AM2/16/17
to Project Jupyter
It's ugly, but it works:
# don't go beyond here with Run All
assert False

Nick Mowry

unread,
Mar 6, 2019, 9:00:52 PM3/6/19
to Project Jupyter
My search turned up this thread while trying to find an alternative to your second suggestion of using a  boolean toggle. This works fine for the most part, and I use it often. However, I cannot display the contents of a pandas data frame the way I'd like to from within the "if block". To follow your code,


include pandas as pd
data = pd.read_excel(xlsPath + xlsFile)
TESTING = True

# this shows all data with no formatting,
print(data)

# this formats the data into a table and displays it
# (how I'd like it to look),
data

# but when I try this, I get no output whatsoever.
if TESTING:
    data


Here's my version information. Any suggestions would be greatly appreciated. I'm wondering if this may be a bug in the pandas library?

Server Information:

You are using Jupyter notebook.

The version of the notebook server is: 5.7.4
The server is running on this version of Python:

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)]

Current Kernel Information:

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help.


On Wednesday, February 15, 2017 at 3:44:07 PM UTC-5, Paul Hobson wrote:
One option would be raise an exception in a new cell above your tester cell.

Another option would be to a variable defined in the notebooks first cell (TESTING=False). You can then wrap your tests in an if testing: block, setting testing to True if you actually want them to run.
On Wed, Feb 15, 2017 at 12:11 PM, John Marino <john.d...@gmail.com> wrote:
As I develop a notebook, I like to store tester code down at the bottom.  But this means I can't Run All or Run All Below without the tester code running.  (Yes, I can scroll down and Run All Above.)  Is there something I can enter into a cell that would keep the notebook from continuing to run cells?  I don't want to halt the kernel or interrupt what is running (i.e. I don't want an exception thrown).  I'm thinking a magic, e.g. %stop, would do the trick.  (I looked at the existing magics, but didn't find anything like this.)

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

Paul Hobson

unread,
Mar 7, 2019, 11:25:30 AM3/7/19
to jup...@googlegroups.com
You could try the built-in `print` function or ipython's display module.



Roland Weber

unread,
Mar 8, 2019, 1:25:38 AM3/8/19
to Project Jupyter
Use the ternary operator as the last statement in the cell...

data if TESTING else None

Reply all
Reply to author
Forward
0 new messages