Ich möchte einige Notizbücher aus einem Python-Skript generieren. Gibt es eine API zum Schreiben von IPython-Notebooks?Gibt es ein IPython Notebook api?
Antwort
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).
- 1. IPython Notebook mit optirun
- 2. Doppelklicken Sie auf ein ipython Notebook
- 3. Disable ipython Notebook Autoscrolling
- 4. IPython Notebook Mehrere Checkpoints
- 5. IPython-Notebook auf Heroku
- 6. ipython Notebook benötigt Javascript
- 7. Es ist möglich, ein IPython-Notebook aus PythonAnywhere zu bedienen
- 8. IPython Notebook Sicherheit
- 9. pip Mit ipython Notebook
- 10. Ipython: Debugging-Code im Notebook
- 11. Wie in IPython Notebook zwischenspeichern?
- 12. "Zebra-Tabellen" in IPython Notebook?
- 13. Gibt es ein Ipython-Äquivalent für Erlang?
- 14. "Speichern unter" in IPython notebook
- 15. iPython Notebook/Jupyter Autosave fehlgeschlagen
- 16. Echt LaTeX in ipython Notebook
- 17. Gelöschte Ipython/Jupyter Notebook wiederherstellen?
- 18. IPython Notebook wird die Konfigurationsdatei
- 19. Hat das IPython Notebook "Initialisierungszellen"?
- 20. Anpassen IPython Notebook/Jupyter Diashow
- 21. ipython Notebook Plots anordnen horizontal
- 22. IPython Notebook Sympy Math Rendering
- 23. ipython Notebook-Terminals nicht verfügbar
- 24. Deaktivieren Autosave in IPython Notebook
- 25. Running IPython Notebook Viewer lokal
- 26. Plot Breiteneinstellungen in ipython Notebook
- 27. Weird Symbole in IPython Notebook
- 28. ipython notebook - $ DISPLAY variabler Fehler
- 29. Escaping Dollarzeichen in Ipython Notebook
- 30. Jupyter (IPython) Notebook nicht plotten
Das ist nützlich. Können Sie in der Antwort eine kurze Beschreibung hinzufügen, wenn eine Zellaufteilung ausgelöst wird? – user2304916
Eine einfache Erklärung hinzugefügt. –
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