Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 812
Chuck Avatar asked Apr 02 '19 13:04

Chuck


1 Answers

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')
like image 86
DeshDeep Singh Avatar answered Oct 22 '22 18:10

DeshDeep Singh