Search code examples
pythontextjupyter-notebookipython

Convert raw Ipython Notebook txt to Ipynb


I have a textfile containing the source code for an Ipython Notebook.

How can I convert this file using Ipython / Python to an Ipython Notebook?

e.g. :

source.txt:

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test"
   ]
  },
}
...

The questions on SE deal with converting an actual notebook to a .py or otherwise. I'm looking just to get to the notebook in the first place.

Edit: I'm on Mac


Solution

  • You need to first install iPython package

    pip install ipython
    

    Save your current file as .py file using notepad++ Then try below code

    import IPython.nbformat.current as convert
    conv = convert.read(open('source.py', 'r'), 'py')
    convert.write(conv, open('source.ipynb', 'w'), 'ipynb')