Ask Your Question

kapil's profile - activity

2018-06-22 16:16:24 -0500 received badge  Necromancer (source)
2018-06-22 16:16:24 -0500 received badge  Teacher (source)
2018-06-22 11:33:37 -0500 answered a question how to (automatically) convert many .sws to .ipynb?

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')