Vorrei generare diversi taccuini da uno script python. Esiste un'API per scrivere taccuini IPython?Esiste un'API per notebook IPython?
risposta
V'è, si può fare:
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')
Ma non è così intelligente e sarà mettere tutto il codice dal file di pitone in una cella Notebook IPython. Ma puoi sempre fare un po 'di analisi.
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: Spiegando il parsing
Nel codice precedente una divisione cellulare si attiva ogni volta che viene visualizzata una riga vuota prima di un'istruzione di livello di modulo (funzione, definizione della variabile o classe, l'importazione, ecc .). Questo è ogni volta che vedo una linea che non è rientrata ed è preceduta da una riga vuota). Quindi:
import time
import datetime
sarà solo una cella, ma:
import time
import datetime
saranno due cellule, e anche
class Test(objet):
def __init__(self, x):
self.x = x
def show(self):
print(self.x)
class Foo(object):
pass
saranno due celle dato che ci sono solo due definizioni di primo livello (righe che non sono rientrate) precedute da una riga vuota (la prima riga del file è considerata preceduta da una riga vuota perché deve iniziare una nuova cella).
- 1. Editor esterno per notebook IPython
- 2. Notebook Jupyter/IPython: collegamento per "eseguire tutti"?
- 3. Notebook IPython con optirun
- 4. Collegamenti tra notebook IPython
- 5. Avvio del notebook ipython
- 6. Notebook IPython su Heroku
- 7. "Salva come" nel notebook IPython
- 8. modello predefinito per ipython notebook (utilizzando Jupyter)
- 9. Blocco appunti IPython per notebook multipli
- 10. scorciatoie vi in notebook ipython
- 11. ipython cellulari Notebook uscite multiple
- 12. Notebook IPython Sympy Math Rendering
- 13. Come chiudere correttamente Notebook IPython?
- 14. Errore Pyzmq nel notebook IPython
- 15. Come memorizzare nella cache in Notebook IPython?
- 16. "import matplotlib.pyplot as plt" nel notebook ipython
- 17. Download file di trigger nel notebook iPython
- 18. Carica dati locali nel server notebook IPython
- 19. Come si sopprime l'output in IPython Notebook?
- 20. Notebook Ipython (jupyter), opencv (cv2) e tracciamento?
- 21. Notebook Ipython: dov'è jupyter_notebook_config.py in Mac?
- 22. Verifica PEP8 nel codice del notebook iPython
- 23. Esporta singola cella nel notebook IPython/Jupyter
- 24. IPython notebook: come impostare l'intervallo cursore
- 25. Stile CSS di HTML nell'output Notebook IPython
- 26. equivalente di raw_input nel notebook Ipython
- 27. ipython: il notebook non sembra essere JSON
- 28. Modalità matematica inline sul notebook iPython
- 29. pandas sottotrama dimensione titolo nel notebook ipython
- 30. Automatizzare jupyter Standard/ipython importazioni notebook
Questo è utile. Puoi aggiungere alla risposta una breve descrizione quando viene attivata una divisione cellulare? – user2304916
Aggiunta una spiegazione semplice. –
Non ho intenzione di convertire un file python in un notebook, ma piuttosto di scrivere una serie di quaderni usando uno script python. il IPython.nbformat.current sembra quello che stavo cercando. Grazie! – alex