2

Simply I build an .ipynb like this:

test_jupyter

And now I want to extract all python code from this .ipynb file. I have tried the File->Download as->Python (.py) menu function, and I get the following result:

# coding: utf-8

# In[13]:

def a():
    la = [1, 2, 3]
    def b():
        print 'helloworld'
        print la, len(la)
    b()
a()


# In[1]:

get_ipython().magic(u'pylab inline')


# In[6]:

a = [1, 2, 3, 4, 5]


# In[8]:

import matplotlib.pyplot as plt

# In[9]:

import numpy as np


# In[11]:

data = np.random.rand(2, 25)


# ### HelloWorld
# 1. kk
# 2. bb
# * jhah

# In[13]:


get_ipython().system(u'ipython nbconvert --to script test1.ipynb')


# In[ ]:

Is there any method to get the beautiful result (without In [*] and jupyter's magic function) like this?

# coding: utf-8

def a():
    la = [1, 2, 3]
    def b():
        print 'helloworld'
        print la, len(la)
    b()
a()


a = [1, 2, 3, 4, 5]


import matplotlib.pyplot as plt


import numpy as np


data = np.random.rand(2, 25)


# ### HelloWorld
# 1. kk
# 2. bb
# * jhah
ansvver
  • 317
  • 1
  • 12

2 Answers2

4

You may have solved this by now. If not, you could use the jupyter-nbconvert script, which comes with Jupyter.

jupyter-nbconvert --to python notebook.ipynb --stdout --TemplateExporter.exclude_input_prompt=True

This will output your code only and get rid of the input prompts (In [*]).

There are more options to exclude inputs, outputs, markdown, code and so on.

Tested in Jupyter 4.4.0.

sebastian
  • 1,438
  • 1
  • 15
  • 23
0

Here's something that I have tried in the past and it worked for me:

  1. Delete empty cells.
  2. Select all the cells.
  3. Convert all the cells type from Code to Raw NB convert.
  4. Save the file in the .py format.

Voila you get your beautiful code in the desired format.