Ask Your Question
2

how to (automatically) convert many .sws to .ipynb?

asked 2016-12-02 16:38:24 +0200

stan gravatar image

updated 2016-12-02 16:46:15 +0200

kcrisman gravatar image

Thanks to Volker Braun, there is a great tool to convert sagenb work sheets to jupyter ones, e.g.:

sage --notebook=export --ipynb=newname.ipynb admin:383

However, I got a folder full of exported worksheets as .sws files that are not in my .sagenb folder. Do I have to import them one by one, then find out their numbers and export using a command equivalent to the above? Surely there must be a way to automate this from the command line or convert directly. Or is there a way to pass an .sws-file to sage --notebook=export? https://github.com/vbraun/ExportSageNB does not mention such a possibility. Thanks already for your help!

edit retag flag offensive close merge delete

Comments

I think eventually we want to have a button to click to do this, at least in the Mac app, but so far this hasn't made it in. (See https://trac.sagemath.org/ticket/20316 .) Yes there would also be a way to automate this using a shell script but I don't have time to reconcoct it today - surely someone else will, though :)

kcrisman gravatar imagekcrisman ( 2016-12-02 16:45:59 +0200 )edit

If there was a way to import an .sws file into the sagenb notebook using shell syntax, it would be easy, but I haven't seen a way to do that.

stan gravatar imagestan ( 2016-12-02 17:57:02 +0200 )edit
1

I found this ticket: https://trac.sagemath.org/ticket/8473 The way to upload an .sws file from the command line is:

sage -n upload='worksheet.sws'
stan gravatar imagestan ( 2016-12-06 13:06:47 +0200 )edit

Yes. I see now why you wanted this - I didn't get that you had downloaded sws files, not ones currently in the notebook directory. Not sure why I didn't see that.

kcrisman gravatar imagekcrisman ( 2016-12-06 15:40:34 +0200 )edit
1

Also, I'm a little surprised that Volker's script doesn't take notebook file paths as an argument!

kcrisman gravatar imagekcrisman ( 2016-12-06 15:41:32 +0200 )edit

3 Answers

Sort by ยป oldest newest most voted
4

answered 2017-07-08 18:41:50 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

For a folder with a bunch of .sws files and taking advantage of what has been said above, one can make a bash shell script command, something like:

for file in *sws; do sage -sws2rst "$file" "${file%.sws}.rst"; done

and the same for rst to ipynb

for file in *rst; do sage -rst2ipynb "$file" "${file%.rst}.ipynb"; done

These is done in the folder with many .sws files.

In macOS 10.12.6 Beta with SageMath version 7.6, Release Date: 2017-03-25 the shell script worked. The conversion is note perfect!

edit flag offensive delete link more

Comments

Glad to hear it worked so well!

kcrisman gravatar imagekcrisman ( 2017-07-08 23:06:44 +0200 )edit
2

answered 2016-12-02 16:46:40 +0200

tmonteil gravatar image

updated 2016-12-02 17:33:16 +0200

You can transform .sws into .rst with the command:

sage -sws2rst file.sws file.rst

Then, once trac ticket 21514 will be merged (hopefully in the next official version of Sage), you will be able to transform .rst into .ipynb with

    sage -rst2ipynb file.rst file.ipynb

Then you should be able to loop with the find shell command.

edit flag offensive delete link more

Comments

Wow, that ticket is really great! Thanks for a glimpse into the future! Wouldn't the last command be:

sage -rst2ipynb file.rst file.ipynb

At the moment, sage -sws2rst file.sws file.rst complains that the sws file is not a bzip2 file.

stan gravatar imagestan ( 2016-12-02 17:25:35 +0200 )edit

Indeed, i made a typo, thanks.

tmonteil gravatar imagetmonteil ( 2016-12-02 17:33:30 +0200 )edit

All .sws files should be bzip2, that is weird.

kcrisman gravatar imagekcrisman ( 2016-12-02 22:52:29 +0200 )edit
1

answered 2018-06-22 15:40:27 +0200

kapil gravatar image

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')
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-12-02 16:38:24 +0200

Seen: 4,750 times

Last updated: Jul 08 '17