Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I wrote a script to take a download_worksheets.zip file from the current directory and write the converted .ipynb files to the directory converted. It also prints the name of the temporary directory containing sagenb storage heirarchy. This directory needs to be deleted after the process is over. It would be nice if the script can be suitably modified to work with command line arguments and possibly with a directory of files rather than a .zip file.

import os
import zipfile
oldwd=os.getcwd()
zip_file=zipfile.ZipFile(oldwd+'/download_worksheets.zip')
ipd=oldwd+'/converted'
os.mkdir(ipd)

import tempfile
wd=tempfile.mkdtemp()
os.chdir(wd)
print(wd)

from sagenb.notebook.notebook import load_notebook
from sagenb_export.actions import action_print, action_convert_ipynb
from sagenb_export.sagenb_reader import NotebookSageNB

nb=load_notebook(wd+'/sage_notebook')
nb.create_default_users('totallysecret')

for fl in zip_file.namelist():
    zip_file.extract(fl)
    ws = nb.import_worksheet(fl,'admin')
    os.unlink(fl)

nb.save()

for fl in zip_file.namelist():
    nfl=fl[:-4]
    sagenb=NotebookSageNB.find(wd,nfl)
    action_convert_ipynb(sagenb,ipd+'/'+nfl+'.ipynb')