Ask Your Question

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? 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 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

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

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

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


Glad to hear it worked so well!

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

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


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

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

kapil gravatar image

I wrote a script to take a 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

import tempfile

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


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

for fl in zip_file.namelist():
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


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

Seen: 4,798 times

Last updated: Jul 08 '17