Skip to content
This repository

Add encoding header to Python files downloaded from notebooks. #1159

Closed
wants to merge 1 commit into from

3 participants

Bradley M. Froehle Thomas Kluyver Fernando Perez
Bradley M. Froehle
Collaborator

Closes gh-1156.

Bradley M. Froehle

This prevents the encoding header from appearing in the notebook interface. Is this the desired behavior?

Thomas Kluyver
Collaborator

Snap: PR #1158. ;-)

I'm pretty sure it is the desired behaviour - we execute code as unicode strings, and Python complains if it gets an encoding declaration in a unicode string. But we should remove it consistently (I've done it using the regex from PEP 263).

Fernando Perez
Owner

Wow, we have dueling PRs! Neat!! BTW, we're on IRC right now if you guys want to jump in..

Bradley M. Froehle bfroehle closed this December 15, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 15, 2011
Bradley M. Froehle Add encoding header to Python files downloaded from notebooks.
Closes gh-1156.
faa2d7f
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 5 additions and 1 deletion. Show diff stats Hide diff stats

  1. 6  IPython/nbformat/v2/nbpy.py
6  IPython/nbformat/v2/nbpy.py
@@ -40,6 +40,8 @@ def to_notebook(self, s, **kwargs):
40 40
         for line in lines:
41 41
             if line.startswith(u'# <nbformat>'):
42 42
                 pass
  43
+            elif line.startswith(u'# -*-'):
  44
+                pass
43 45
             elif line.startswith(u'# <codecell>'):
44 46
                 cell = self.new_cell(state, cell_lines)
45 47
                 if cell is not None:
@@ -111,7 +113,9 @@ class PyWriter(NotebookWriter):
111 113
 
112 114
     def writes(self, nb, **kwargs):
113 115
         lines = []
114  
-        lines.extend([u'# <nbformat>2</nbformat>',''])
  116
+        lines.extend([u'# -*- coding: utf-8 -*-',
  117
+                      u'# <nbformat>2</nbformat>',
  118
+                      u''])
115 119
         for ws in nb.worksheets:
116 120
             for cell in ws.cells:
117 121
                 if cell.cell_type == u'code':
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.