Restart snippet numbering in the middle of a notebook?

49 views
Skip to first unread message

insearcho...@gmail.com

unread,
Dec 18, 2017, 3:41:49 PM12/18/17
to Project Jupyter
If I am writing a book chapter with many sections, is there any way for me to start over with snippet #1 at the beginning of each section? Or do I need to use separate notebooks for each section to accomplish that?

So basically, I want to forget about the snippets earlier in the notebook and begin from scratch with each new section.

Roland Weber

unread,
Dec 19, 2017, 1:48:54 AM12/19/17
to Project Jupyter
Restart the kernel before each section. That will clear all state in memory, and reset the execution count.

Fernando Perez

unread,
Dec 20, 2017, 5:33:30 AM12/20/17
to Project Jupyter
On Mon, Dec 18, 2017 at 10:48 PM, Roland Weber <rolw...@de.ibm.com> wrote:
Restart the kernel before each section. That will clear all state in memory, and reset the execution count

You can also force that number to be any value with:

ip = get_ipython()
ip.history_manager.enabled = False  # needed to avoid duplicate line # error messages
ip.execution_count = 0  # your number here

and it will start counting at that number from then on.  OP could put that in a little utility/magic that resets it to 1, and then drop those cells from the book output with a tag.

Visual example:

Inline image 1

cheers,

f

insearcho...@gmail.com

unread,
Dec 26, 2017, 9:41:10 AM12/26/17
to Project Jupyter
Thanks Fernando!

insearcho...@gmail.com

unread,
Dec 26, 2017, 9:42:23 AM12/26/17
to Project Jupyter
But once I ship out the notebook to others, they'd need to do the same as they execute cells, correct?

insearcho...@gmail.com

unread,
Dec 26, 2017, 9:43:25 AM12/26/17
to Project Jupyter
Can we programmatically have the notebook "forget" all prior code snippets as well so we effectively have a "clean" environment for a new section?


On Wednesday, 20 December 2017 05:33:30 UTC-5, Fernando Perez wrote:

M Pacer

unread,
Dec 26, 2017, 2:09:46 PM12/26/17
to jup...@googlegroups.com
If you want a clean environment you can include a cell that runs 

%reset -f

 That will reset your environment to a clean state if you don’t want any of the variables to carry over. But then people would need to execute that cell.

--
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.
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/9bfd0c07-0fc6-4dc2-8574-426c8f76c040%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

M Pacer

unread,
Dec 26, 2017, 2:25:42 PM12/26/17
to jup...@googlegroups.com
Also, if you wanted a static version that would drop all those cells in addition to a tag you could use the regexRemovePreprocessor (with an nbconvert config option) to automatically remove all cells that match a regular expression that matches exactly those characters.

Roland Weber

unread,
Dec 27, 2017, 1:44:00 AM12/27/17
to Project Jupyter
On Tuesday, December 26, 2017 at 3:42:23 PM UTC+1, insearcho...@gmail.com wrote:
But once I ship out the notebook to others, they'd need to do the same as they execute cells, correct?

Once you ship out the notebook to others, they can execute and re-execute cells in whatever order they want to. In reverse order, if they have a mind to.
If you want to give them something that never changes, then make an HTML export or a screenshot.
If you want to give them something that starts numbering from scratch with each chapter, then give them a separate notebook for each chapter.

Notebooks are interactive, everyone will have their personal instance of them. The execution count shows how someone works with their notebook. You're trying to control what others will do with their copies of your notebook(s). It's a waste of time. It's like trying to control in which order people read the different articles in a newspaper, or on a news website.

cheers,
  Roland

Jason Grout

unread,
Dec 28, 2017, 10:24:41 AM12/28/17
to jup...@googlegroups.com

You might consider putting each section in a separate notebook. Then it very naturally starts with a fresh kernel, is self-contained, etc.

Jason


--
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.
To post to this group, send email to jup...@googlegroups.com.

insearcho...@gmail.com

unread,
Jan 3, 2018, 11:52:33 AM1/3/18
to Project Jupyter
I agree. That's what I am doing now. As we go to review with our book, I am curious whether instructors/students will find that burdensome, which is why I am asking about resets in a single notebook. 

insearcho...@gmail.com

unread,
Jan 3, 2018, 11:53:11 AM1/3/18
to Project Jupyter
Thanks Roland.

insearcho...@gmail.com

unread,
Jan 3, 2018, 11:53:37 AM1/3/18
to Project Jupyter
Thanks.

Jason Grout

unread,
Jan 4, 2018, 12:05:15 AM1/4/18
to jup...@googlegroups.com
Another direction we can take is to make it easier to navigate in a collection of notebooks. For example, make it easy to make previous/next/up links in each notebook, plus an index. We do some things like this for the ipywidgets documentation.

Jason


Reply all
Reply to author
Forward
0 new messages