6

With apologies in advance for the "I can't get it to work" question: How should I load a .py file into ipython notebook? I want to convert python code to notebooks (first simple scripts and later scripts that include nbconvert directives embedded as comments-- see bottom of the linked file.)

Perhaps I'm doing it wrong, but perhaps there's something wrong with my set-up. When I drag a .py file to the Notebook's file list, I get the message

Invalid file type: Uploaded notebooks must be .ipynb files.

I even tried changing the extension to .ipynb (keeping the python script unmodified); reasonably enough, I got an error:

Error loading notebook: Bad request

Any idea what's going wrong?

System information: I'm on OS X (10.8, Mountain Lion), using Firefox 28.0 and Anaconda 1.9.2 (x86_64), which supplies python 2.7.6 and ipython 2.0. Anaconda is not on the default PATH; I add it in a bash session from which I then launch notebook with ipython notebook, and I'm able to open and edit .ipynb files normally in the browser.

But I do get some curious behavior: When exporting from notebook as a .py file, I don't get the control comments documented here but a simpler format, without version number:

# coding: utf-8

# In[ ]:

print "This is a slide"

## Top-level title 

### Second-level heading 

#### Third-level heading

# This is some `markdown` text. 
# 
# And some more here.

Any idea what's going on here?

The same format is generated by ipython nbconvert. However, if I start the notebook server with ipython notebook --script (which exports the notebook as a python script every time it is saved), the result contains the nbconvert directives we need to convert back to a notebook!

alexis
  • 48,685
  • 16
  • 101
  • 161
  • possible duplicate of [Converting to (not from) ipython Notebook format](http://stackoverflow.com/questions/23292242/converting-to-not-from-ipython-notebook-format) – Cristian Ciupitu Aug 05 '14 at 03:30
  • @Cristian, the questions are related but they're not duplicates; that's why I asked both of them. The *answer* to this one is that there's no way to do it in the GUI, which is the only reason the other question becomes relevant. – alexis Aug 08 '14 at 19:32
  • Ok, I've retracted my close vote. – Cristian Ciupitu Aug 08 '14 at 21:38

3 Answers3

10

I had the same problem. This post helped: How to load/edit/run/save text files (.py) into an IPython notebook cell?

Basically, we just have to use the following command in the cell. And the .py file has to be in the same directory.

%load filename.py
Community
  • 1
  • 1
noob4life
  • 415
  • 2
  • 5
  • 10
  • Thanks, that's a useful feature to know about but not what I needed. I've solved the problem in the meantime, I should have self-answered sooner. – alexis Jun 28 '14 at 07:34
5

I'm not sure why notebook doesn't support this natively, but I've concluded that the answer is: It can't be done from the command line or notebook GUI.

Control comments like <markdowncell> can only be interpreted by accessing notebook's API through python, as shown by @CliffordVienna in this answer to my related question.

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

Edit: The above method does not work with the current version (v4) of the Notebook API, so I have added this self-answer to show how it's done.

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161
-2

If you only need to import a local file, first use:

sys.path.append(os.getcwd())

to place the .pynb file's directory in sys.path, and then import the local file.

  • Thanks for trying to help, but do you know what an ipython _notebook_ is? `sys.path` is irrelevant to the question. – alexis Dec 14 '15 at 20:00
  • Actuallly, it is relevant. If the python file is in a different directory, you need to use sys.path. For your specific case, where the file is in the same directory, you don't need to use sys.path. – Vinicius Miana Sep 24 '20 at 23:00