How do I reset the Jupyter/IPython input prompt numbering?

前端 未结 9 1091
孤街浪徒
孤街浪徒 2021-02-02 05:08

I just wrote my first extensive Python tutorial using IPython notebooks. All went well, except I did a lot of testing and moving blocks around. How do I reset the In [

相关标签:
9条回答
  • 2021-02-02 05:27

    I'm a bit too late, but I had the same problem, and since my notebook had cells with execution time up to 5 minutes, I had to wait a long time until Restart & Run All finished.

    So I've made a Python script to make this task for me:

    import json
    
    file = '/your/notebook/path/Notebook.ipynb'
    
    # Since every notebook is actually a JSON (JavaScript
    # Object Notation), then its contents can be represented
    # in a dictionary (or a list of dictionaries)
    with open(file, encoding='utf-8') as f:
        nb = json.load(f)
    
    count = 1
    for cell in nb['cells']:
        # Markdown cells doesn't have execution count,
        # so apply this only to cells that have one
        if 'execution_count' in cell:
            cell['execution_count'] = count
            count += 1        
    
        # Not all code cells have output, such as functions
        # that return None or simple declarations, so apply
        # this only to cells that have some output
        try:
            for output in cell['outputs']:
                if 'execution_count' in output:
                    output['execution_count'] = cell['execution_count']
    
        except KeyError:
            continue
    
    with open(file, 'w+') as f:
        json.dump(nb, f, indent=2, ensure_ascii=False)
    

    But be careful with the execution order and the variables in your cells, since applying the script above on your notebook can generate a different output if you run the notebook again. For example, let's suppose your notebook have the following cells with the execution order in square brackets:

    In [2]: a = 1
    
    In [1]: a = 2
    
    In [3]: a
    
    Out[3]: 1
    

    If you apply the above script into your notebook, it'll show the following:

    In [1]: a = 1
    
    In [2]: a = 2
    
    In [3]: a
    
    Out[3]: 1
    

    But if you run the notebook again, it'll show the following:

    In [1]: a = 1
    
    In [2]: a = 2
    
    In [3]: a
    
    Out[3]: 2
    

    This can be a bit confusing for people who are downloading your notebook via GitHub for example, since they can see an output in the repository, but when they run on their machine, the output will be different.

    0 讨论(0)
  • 2021-02-02 05:30

    You can reset the kernel (shortcut: C-m .) and re-run the whole notebook.

    Quitting and reloading doesn't work because the code is not re-evaluated.

    0 讨论(0)
  • 2021-02-02 05:31
    'Kernel' -> 'Restart & Run All'
    

    Just make sure you saved your Notebook. You can also bind/assign keyboard key for running this command.

    'Help' -> 'Edit Keyboard Shortcuts'
    
    0 讨论(0)
  • 2021-02-02 05:32

    Restart & Run All isn't a good solution, because simply I don't want to run all (and that's the purpose of a notebook to run things cell by cell).

    Anyways, I found this solution more plausible:

    Main Menu > Cell  > All Output > Clear
    
    0 讨论(0)
  • 2021-02-02 05:38

    Cell > All Output > Clear Clear all In []: numbers but do not reset them back to 1 for the next cell you run.

    Kernel > Restart & Clear Output Restart the kernel, clear output, clear In []: numbers and reset them to 1, and clear output.

    0 讨论(0)
  • 2021-02-02 05:39

    Every .ipynb file can be opened in an editor. Everything written there is in plain text (JSON). For each cell which has the "cell_type": "code" there'd be another key-value pair as "execution_count": <number>. As you might have guessed, that is the prompt numbering. Hence, if the notebook contains code which will take time to execute (as was, in my case) this method would be time efficient.

    Now, either you can manually change each execution_count or write a simple script to get the numbering right. To check the results just refresh the notebook in the browser without stopping the kernel. And, everything will be as per your needs, even all the variables/loaded data will remain in the environment.

    0 讨论(0)
提交回复
热议问题