How to convert nb v3 into v4 format

hi,

I have cloned Python for Signal Processing from github. And I found the notebook version is v3, and my jupyter notebook is v4. When I opened the ipynb file in VSCode, it is said that the format is not supported.

I can start up nb server locally, and use firefox to open the ipynb file, and then use save as to save it into v4 format. However, at this time, I need to save the ipynb file one by one.

My question is that is there way to convert the format for multiple files instead one by one? especially using command to convert?

thx

Yes, you can do this a few ways that would handle it programmatically. nbformat is a great tool to have in your toolbox for working with notebooks and I’m going to demonstrate with that. See here for more about nbformat

And you don’t even need to start up a local server. You can simply point mybinder.org at the repo by putting the GitHub repo URL in the form at mybinder.org and hitting launch. (Or just click here to launch direct with the URL it generates.)

Then open a new notebook in the session and paste in the following code.

import os
import nbformat

def convert_notebooks_in_current_directory():
    """
    Iterates through all files in the current directory, identifies Jupyter
    Notebooks (.ipynb), and converts them to the current nbformat.
    """
    current_directory = os.getcwd()
    notebook_files = [
        f for f in os.listdir(current_directory) if f.endswith(".ipynb")
    ]

    if not notebook_files:
        print("No Jupyter Notebooks found in the current directory.")
        return

    print(f"Found the following notebooks: {notebook_files}")

    for filename in notebook_files:
        filepath = os.path.join(current_directory, filename)
        try:
            with open(filepath, 'r', encoding='utf-8') as f:
                nb = nbformat.read(f, as_version=4)  # Read notebook

            # nbformat.write will automatically convert to the current format
            with open(filepath, 'w', encoding='utf-8') as f:
                nbformat.write(nb, f)

            print(f"Successfully converted: {filename}")

        except Exception as e:
            print(f"Error processing {filename}: {e}")

convert_notebooks_in_current_directory()
!tar -czvf notebooks.tar.gz *.ipynb

Then you can right-click on the notebooks.tar.gz file in the file browser pane on the left and download the notebooks.tar.gz to your local system.

You could also run that code locally in a notebook. Just make sure your current working drive is the one with all the notebooks in it, and leave off the !tar -czvf notebooks.tar.gz *.ipynb command if you are doing this locally.

If you do it in a notebook it will try to convert that one, too. So after you run it, you’ll keep getting notices to reload or overwrite but you can ignore them because the one you run it in isn’t one of the ones you are interested in working with later.


Be aware though you are going to have a slog of it trying to run those. Or at least some of those.
For example, to get it to work, I took the converted Buffons_Needle_Sim.ipynb, and then changed the bottom of the .ipynb file like so to specify Python 2:

"metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

Probably advisable to save that as a new file.

Then I went here and hit ‘launch binder’ to get a session that had Python 2 and 3.

When that came up, I uploaded my edited version of Buffons_Needle_Sim.ipynb, and double-clicked it in the file browser to open it. Because I had edited the bottom metadata, it opened with the Python 2 kernel automatically.

Now skip the outdated stuff above the title ‘Buffon’s Needle’.
I still needed to edit some of the code to get it to run. Mainly in the imports in the first code cell under ’ Setting up the Simulation’. You can find the working version here.

Option for some of them(?)

Looks like someone converted some of them to Python 3 here.
The Buffons_Needle_Sim.ipynb doesn’t seem to be one of them.

1 Like