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.

2
  • @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 Commented May 3, 2014 at 4:42
  • 1
    Why wouldn't something like this work? Commented May 3, 2014 at 5:56

2 Answers 2

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')
Sign up to request clarification or add additional context in comments.

5 Comments

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.
@Phox That'd be easy to remedy. Just change the startswith to '# In['
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.
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.
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.
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 %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.