Hao Zhang
Hao Zhang

Reputation: 257

change index number in jupyter notebook

I am using Jupyter Notebook to write some tutorials. However, I met a problem here. As shown in following picture, when I made changes to my colleague's notebook program, the index is not right. how to change those in 1, in [2] to in [34], in [35]?enter image description here

Upvotes: 8

Views: 15360

Answers (3)

Shahnazi2002
Shahnazi2002

Reputation: 89

You can easily solve this problem as follows:

Jupyter Notebook Menubar > Kernel > Restart & Run All

Upvotes: 0

Alexey Grigorev
Alexey Grigorev

Reputation: 2425

This simple Python snippet would do it

import json

with open(NOTEBOOK_FILE, 'rt') as f_in:
    doc = json.load(f_in)


cnt = 1

for cell in doc['cells']:
    if 'execution_count' not in cell:
        continue

    cell['execution_count'] = cnt

    for o in cell.get('outputs', []):
        if 'execution_count' in o:
            o['execution_count'] = cnt

    cnt = cnt + 1


with open(NOTEBOOK_FILE, 'wt') as f_out:
    json.dump(doc, f_out, indent=1)

(Make sure the notebook isn't running in Jupyter)

Upvotes: 6

Mike Müller
Mike Müller

Reputation: 85512

Click the menu Cell --> Run all. This will execute all cells and you will have ordered cell index numbers. If it does not start from cell index 1, first click Kernel --> Restart and confirm the re-start.

Upvotes: 9

Related Questions