Skip to content
This repository

Add coding header when notebook exported to .py file. #1158

Merged
merged 5 commits into from over 2 years ago

3 participants

Thomas Kluyver Bradley M. Froehle Fernando Perez
Thomas Kluyver
Collaborator

Closes gh-1156

We probably want to strip the encoding declaration out when we load a .py file, as well. I'll add that to this PR.

IPython/nbformat/v2/nbpy.py
@@ -111,7 +114,7 @@ class PyWriter(NotebookWriter):
111 114
 
112 115
     def writes(self, nb, **kwargs):
113 116
         lines = []
114  
-        lines.extend([u'# <nbformat>2</nbformat>',''])
  117
+        lines.extend([u'# coding: utf-8', u'# <nbformat>2</nbformat>',''])
1
Fernando Perez Owner
fperez added a note December 15, 2011

I think this should be done in two separate lines. Let' s not mix encoding with nbformat info. Instead, initialize the lines list with the encoding declaration:

lines = ['# coding: utf-8'']
# rest of the code as it was before this PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Thomas Kluyver
Collaborator

Updated.

Bradley M. Froehle
Collaborator

Looks good to me, is there any benefit to # -*- coding: utf-8 -*- as Fernando originally suggested over # coding: utf-8 ?

Fernando Perez fperez merged commit 688cc09 into from December 15, 2011
Fernando Perez fperez closed this December 15, 2011
Fernando Perez fperez referenced this pull request from a commit January 10, 2012
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7  IPython/nbformat/v2/nbpy.py
@@ -16,6 +16,7 @@
16 16
 # Imports
17 17
 #-----------------------------------------------------------------------------
18 18
 
  19
+import re
19 20
 from .rwbase import NotebookReader, NotebookWriter
20 21
 from .nbbase import new_code_cell, new_text_cell, new_worksheet, new_notebook
21 22
 
@@ -23,6 +24,8 @@
23 24
 # Code
24 25
 #-----------------------------------------------------------------------------
25 26
 
  27
+_encoding_declaration_re = re.compile(r"^#.*coding[:=]\s*([-\w.]+)")
  28
+
26 29
 class PyReaderError(Exception):
27 30
     pass
28 31
 
@@ -38,7 +41,7 @@ def to_notebook(self, s, **kwargs):
38 41
         cell_lines = []
39 42
         state = u'codecell'
40 43
         for line in lines:
41  
-            if line.startswith(u'# <nbformat>'):
  44
+            if line.startswith(u'# <nbformat>') or _encoding_declaration_re.match(line):
42 45
                 pass
43 46
             elif line.startswith(u'# <codecell>'):
44 47
                 cell = self.new_cell(state, cell_lines)
@@ -110,7 +113,7 @@ def split_lines_into_blocks(self, lines):
110 113
 class PyWriter(NotebookWriter):
111 114
 
112 115
     def writes(self, nb, **kwargs):
113  
-        lines = []
  116
+        lines = [u'# -*- coding: utf-8 -*-']
114 117
         lines.extend([u'# <nbformat>2</nbformat>',''])
115 118
         for ws in nb.worksheets:
116 119
             for cell in ws.cells:
3  IPython/nbformat/v2/tests/nbexamples.py
@@ -81,7 +81,8 @@
81 81
     metadata=md
82 82
 )
83 83
 
84  
-nb0_py = """# <nbformat>2</nbformat>
  84
+nb0_py = """# -*- coding: utf-8 -*-
  85
+# <nbformat>2</nbformat>
85 86
 
86 87
 # <htmlcell>
87 88
 
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.