I am using jupyter 4 with python and I would need my script to do a ' relaunch all the cells above' when a crash occurs. Is it possible ?
2nd question : If I need to relaunch all some cells, can I ask python to execute them according to some cell-id ? I could then create a list of the cells id which have to be re-executed when catching an exception...
Thx.
2nd question: NO
...at least not very reliably, because any ID of a cell would change if you insert or remove any other cell.
According to Execute specific cells through widgets and conditions on github:
And further down on the same post:
1st question: YES
...but it's not 100% certain that it will solve your error handling needs as per the details in your question. But we'll get to that in a bit. Because the good news is that the answer to the question as it stands in the title
is YES WE CAN!
The hard (maybe even impossible) part of this question is to implement it as a robust error handling method. If you're only interested in that, skip to the section
The hard part
at the end of my answer. For now, let's go on with theeasy part
that is to programmatically run the menu optionCell > Run All
(as described in the answer by Nic Cottrell). You have two options:Option 1 - Run all cells above by executing a cell:
If you insert the following snippet in a cell and run it, all cells above will be executed:
Option 2 - Run all cells above by clicking a button:
If you insert the following snippet in a cell and run it, all cells above will be executed when you click the appearing button:
Snippet:
Output:
THE HARD PART
So, how can we set this up to handle an error when a crash occurs? I'm not an expert on this, but I think I've been able to make a setup that will work for you. But it will most likely depend on the type of error in question and the rest of your work flow.
The following example builds on two different error messages. The first is a
NameError
that occurs when you try to assign a value to a variable that does not exist. And this will be useful since re-running some cells after an error will need an iterator that resets only when the notebook is restarted completely, and not when a cell is re-run as part of an error handling method. The name error will only occur when the kernel is restarted upon a fresh restart of your notebook. As part of the error handling, the value0
is assigned tox1
. When the cell is only re-runx1
will increase by1
.The second error will serve as a proxy for your error, and is an AssignmentError that occurs each time you try to delete an element from a list that does not exist. And this leads us to the real challenge, since if your error handler re-runs all cells above every time the error is triggered, you'll quickly end up in a bad loop. But we'll handle that with a counter that exits the looping execution of cells after a few runs.
It's also a bit problematic that there does not seem to exist a functionality to rerun your existing cell, or the cell from where the
run cells above
functionality is initialized. But we'll handle that with another suggestion from the same github post as earlier:Notebook with suggested work-flow:
Insert the four following snippets below in four cells. Click the menu option
Cell > Run all
once, and we're good to go!Snippet 1 - Imports and setup
Snippet 2 - Proxy for your error
Snippet 3 - Cell to rerun all cells above as error handler
Snippet 4 - Cell to rerun the whole thing with en error probability of 20%
Screenshot after a few test runs:
I'll gladly add more details if the comments in the snippets are unclear. But if you run the notebook a few times by clicking
Run Again!
and at the same time have a look at the output of cell 3, you'll quickly grasp how the whole thing is put together:I'm running Notebook server 5.4.0 and I have an option
Cell > Run All Above
which seems to do exactly this.