2

I'm trying to download an ipython notebook file as a .py file. It's working fairly well, except the .py file is interspersed with "In: []" at cell boundaries. I could live with them being there, but I'd rather they weren't.
Any simple fix?

Example output (what I see in the .py file):

# In[4]:

# Get names of all files
text_files = glob.glob('hw3data/*')
#print text_files


# In[5]:

def file_contents(file_name):
    with open(file_name) as f:
        return f.read()

Edit: Essentially, I'm wondering if it's possible to make notebook itself not output #In[ ]. Is there a commandline option, or a utility, or some kind of %magic?

Edit: Going by https://github.com/ipython/ipython/issues/5780, it looks like the suggested solution is just to use a custom template. From minrk:

It's just a marker that indicates where the cells were. It is by design, but it has no effect on the Python, since it is a comment. If you want to remove them, you can use a custom exporter template that doesn't add these comments.

Jason_L_Bens
  • 374
  • 3
  • 14
  • @GoBrewers14 That's okay for small files, but if I were working on something large, it would be easy to miss some. Besides, computers exist to serve us, not the other way around – Jason_L_Bens May 03 '14 at 04:42
  • 1
    Why wouldn't something like [this](http://stackoverflow.com/questions/8206280/delete-all-lines-beginning-with-a-from-a-file) work? – Thane Brimhall May 03 '14 at 05:56

2 Answers2

1

try

def rc(in_, out_):

    in_ = open(in_, 'r')
    out_ = open(out_,'w')
    out_.write(in_.readline())

    for line in in_:
        if not line.lstrip().startswith('# In['):
            out_.write(line)

    in_.close()
    out_.close()

rc('~/in_file.py', '~/out_file.py')
o-90
  • 17,045
  • 10
  • 39
  • 63
  • This ends up deleting all comments as well, I think. I'll edit the question to be a bit more clear about what I'd like to do. – Jason_L_Bens May 03 '14 at 05:46
  • @Phox That'd be easy to remedy. Just change the `startswith` to `'# In['` – Thane Brimhall May 03 '14 at 05:54
  • It is, yeah, and I'm looking to see if notebook has any hooks I could use to run something like this when it exports to a .py file. I'm wondering, though, if notebook can be configured to not put them in at all. It seems really strange, to me at least, that this is included in the output. – Jason_L_Bens May 03 '14 at 05:57
  • 1
    u might also try putting all the code in one cell in your notebook and putting `%%file name_of_file.py` at the top of the cell. – o-90 May 03 '14 at 06:10
  • That's a start in the right direction, but it still requires I modify the notebook before I export it. I have a hack I'll throw in another answer, but I'm starting to think that there isn't really an answer that doesn't involve either running a script manually after every export, or changing the notebook like you said. – Jason_L_Bens May 03 '14 at 06:28
0

As a temporary hack, I found that removing lines seven through nine of the python export template of nbconvert (ipython/IPython/nbconvert/templates/python.tpl in site-packages) stops the input prompt from being output. This still isn't ideal, as I'm modifying site packages.

Affected lines:

{% block in_prompt %}
# In[{{ cell.prompt_number if cell.prompt_number else ' ' }}]:
{% endblock in_prompt %}
Jason_L_Bens
  • 374
  • 3
  • 14