2013-07-31 8 views

Antwort

4

Es gibt, können Sie tun:

import io 
from IPython.nbformat import current 

def convert(py_file, ipynb_file): 
    with io.open(py_file, 'r', encoding='utf-8') as f: 
     notebook = current.reads(f.read(), format='py') 
    with io.open(ipynb_file, 'w', encoding='utf-8') as f: 
     current.write(notebook, f, format='ipynb') 

convert('test.py', 'test.ipynb') 

Aber es ist nicht so schlau, und es wird aus der Python-Datei in eine IPython Notebook Zelle den gesamten Code platzieren. Aber Sie können immer ein bisschen analysieren.

import io 
import re 
from IPython.nbformat import current 

def parse_into_cells(py_file): 
    with io.open(py_file, 'r', encoding='utf-8') as f: 
     data = f.readlines() 
    in_cell = True 
    cell = '' 
    for line in data: 
     if line.rstrip() == '': 
      # If a blank line occurs I'm out of the current cell 
      in_cell = False 
     elif re.match('^\s+', line): 
      # Indentation, so nope, I'm not out of the current cell 
      in_cell = True 
      cell += line 
     else: 
      # Code at the beginning of the line, so if I'm in a cell just 
      # append it, otherwise yield out the cell and start a new one 
      if in_cell: 
       cell += line 
      else: 
       yield cell.strip() 
       cell = line 
       in_cell = True 
    if cell != '': 
     yield cell.strip() 

def convert(py_file, ipynb_file): 
    # Create an empty notebook 
    notebook = current.reads('', format='py') 
    # Add all the parsed cells 
    notebook['worksheets'][0]['cells'] = list(map(current.new_code_cell, 
                parse_into_cells(py_file))) 
    # Save the notebook 
    with io.open(ipynb_file, 'w', encoding='utf-8') as f: 
     current.write(notebook, f, format='ipynb') 

convert('convert.py', 'convert.ipynb') 

Edit: Erklärung der Parsing

In dem vorherigen Code eine Zelle gespalten wird ausgelöst, sobald eine Leerzeile angezeigt wird, bevor eine Modulebene Befehl (Funktion, Variablen oder Klassendefinition, Import, etc .). Das ist immer dann, wenn ich eine Zeile sehe, die nicht eingerückt ist und eine leere Zeile hat. Also:

import time 
import datetime 

Wird nur eine Zelle sein, aber:

import time 

import datetime 

Werden zwei Zellen sein, und auch

class Test(objet): 

    def __init__(self, x): 

     self.x = x 

    def show(self): 

     print(self.x) 

class Foo(object): 
    pass 

werden zwei Zellen sein, da es nur zwei Top-Level-Definitionen (Zeilen, die nicht eingerückt sind), denen eine Leerzeile vorangestellt ist (der ersten Zeile der Datei wird eine Leerzeile vorangestellt, da sie eine neue Zelle beginnen muss).

+0

Das ist nützlich. Können Sie in der Antwort eine kurze Beschreibung hinzufügen, wenn eine Zellaufteilung ausgelöst wird? – user2304916

+0

Eine einfache Erklärung hinzugefügt. –

+0

Ich habe nicht die Absicht, eine Python-Datei in ein Notizbuch zu konvertieren, sondern eine Reihe von Notizbüchern mit einem Python-Skript zu schreiben. Das IPython.nbformat.current sieht aus wie ich es wollte. Vielen Dank! – alex