Ask Your Question

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

asked 2016-12-02 09:38:24 -0500

stan gravatar image

updated 2016-12-02 09:46:15 -0500

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? does not mention such a possibility. Thanks already for your help!

edit retag flag offensive close merge delete


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 .) 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 09:45:59 -0500 )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 10:57:02 -0500 )edit

I found this ticket: The way to upload an .sws file from the command line is:

sage -n upload='worksheet.sws'
stan gravatar imagestan ( 2016-12-06 06:06:47 -0500 )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 08:40:34 -0500 )edit

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 08:41:32 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2017-07-08 11:41:50 -0500

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


Glad to hear it worked so well!

kcrisman gravatar imagekcrisman ( 2017-07-08 16:06:44 -0500 )edit

answered 2016-12-02 09:46:40 -0500

tmonteil gravatar image

updated 2016-12-02 10:33:16 -0500

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


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 10:25:35 -0500 )edit

Indeed, i made a typo, thanks.

tmonteil gravatar imagetmonteil ( 2016-12-02 10:33:30 -0500 )edit

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

kcrisman gravatar imagekcrisman ( 2016-12-02 15:52:29 -0500 )edit

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


Asked: 2016-12-02 09:38:24 -0500

Seen: 1,080 times

Last updated: Jul 08 '17