How to convert python scripts into ipynb file

32,927 views
Skip to first unread message

Bond

unread,
Jul 21, 2016, 7:00:33 AM7/21/16
to Project Jupyter
Hi

Is it possible to convert python script into ipynb file. The script may be in file or as a string ?

Thomas Kluyver

unread,
Jul 21, 2016, 9:10:29 AM7/21/16
to Project Jupyter
On 21 July 2016 at 12:00, Bond <arunku...@gmail.com> wrote:
Is it possible to convert python script into ipynb file. The script may be in file or as a string ?

You can manually convert a few files by copying and pasting the code.

If you want to do it automatically for many scripts, here's a starting point:
https://gist.github.com/takluyver/c7eacd6d52aad4d75cbd48e447f6ad20

Bond

unread,
Jul 22, 2016, 2:07:52 AM7/22/16
to Project Jupyter
Thanks for the response. With the code it is able to convet into ipynb file. But no kernel is getting opened. any suggestions ?

Hai Nguyen

unread,
Jul 22, 2016, 3:55:57 AM7/22/16
to jup...@googlegroups.com
open a blank notebook, put "hello world" in 1st cell, run and save it, then open by your text editor, then replace "hello world" by your real code.
That's my quick hack without thinking too much here: https://github.com/arose/nglview/blob/master/nglview/scripts/nglview.py#L17-L57

Hai

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/ef7719f7-f2b4-4667-840a-ac70f98c9f1e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Damon Allen

unread,
Jul 22, 2016, 8:47:58 AM7/22/16
to Project Jupyter

For a quick one-off way, use the %load magic command in a blank cell in a notebook.

%load mycode.py

Thomas Kluyver

unread,
Jul 23, 2016, 8:43:30 AM7/23/16
to Project Jupyter
On 22 July 2016 at 07:07, Bond <arunku...@gmail.com> wrote:
Thanks for the response. With the code it is able to convet into ipynb file. But no kernel is getting opened. any suggestions ?

The kernel is determined by metadata in the notebook - have a look at the metadata in an existing notebook to see what it contains, and adapt the script to add metadata before writing the notebook.

Ignatius ME Ezeani

unread,
Oct 23, 2017, 6:48:28 PM10/23/17
to Project Jupyter
Wow this is amazing. Thanks.

Alessandro Surace

unread,
Feb 16, 2018, 5:46:18 AM2/16/18
to Project Jupyter
Thanks a lot it made my life easier! :-)

Raimi bin Karim

unread,
Mar 6, 2019, 11:35:43 PM3/6/19
to Project Jupyter
You can try a Python package called p2j. Run 

pip install p2j

and

p2j script.py



On Thursday, July 21, 2016 at 7:00:33 PM UTC+8, Bond wrote:
Hi

Is it possible to convert python script into ipynb file. The script may be in file or as a string ?

On Thursday, July 21, 2016 at 7:00:33 PM UTC+8, Bond wrote:
Hi

Is it possible to convert python script into ipynb file. The script may be in file or as a string ?

Elisa Bortoli

unread,
Jul 12, 2019, 5:35:03 AM7/12/19
to Project Jupyter
Wow, thank u very much!
Reply all
Reply to author
Forward
0 new messages