Ask Your Question

best way to convert many notebooks from sagenb to jupyter?

asked 2018-03-21 08:36:13 -0500

stan gravatar image

I moved from sagenb to jupyter a while ago, but I still have a large number of sagenb notebooks that I have been converting one-by-one on demand. Is there a way to convert all of them at once and save them to a dedicated folder? Currently, when I start sage -notebook, I get to an interface where I can see all my sagenb notebooks and I have the option to click on any of them, convert and open in jupyter. I would actually like to convert all of them without opening them. This is related to, but I suppose that there is a better way now. Thanks already for your help!

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2018-03-30 04:12:22 -0500

stan gravatar image

Certainly not the best way, but here is how I did it using the answer of @eric_g:

In a terminal, show a list of all notebooks:

sage -n export --list

Copy the output into the clipboard and paste into a text editor that allows block selection, e.g. geany in linux. Then create a new directory (e.g. 'fromsagenb') to make sure that you don't overwrite existing files and copy the block just containing the names into a new file in the empty folder, e.g. 'list.txt'.

Now execute the following code to convert all sage notebooks in that list into jupyter notebooks:

import os
f = open('list.txt')
str1 =
str2 = str1.splitlines()
for name in str2:
    command = '~/Programs/SageMath/sage -n export --ipynb={0}.ipynb {0}'.format(name)
edit flag offensive delete link more

answered 2018-03-21 09:22:19 -0500

eric_g gravatar image

updated 2018-03-21 09:30:46 -0500

Just to elaborate on @slelievre's answer: you don't have to install the SageNB exporter: it is already incorporated in recent versions of Sage; you can run it via the command sage -n export. More precisely, to list all your sagenb notebooks, run

sage -n export --list

Then to transform the notebook listed as admin:12 (say), run

sage -n export --ipynb=output.ipynb admin:12

Instead of admin:12, you can also use directly the notebook name (without any extension, i.e. as it appears in the output of sage -n export --list):

sage -n export --ipynb=notebook_name.ipynb notebook_name

I guess you can easily put this into a loop to treat all your sagenb notebooks at once.

edit flag offensive delete link more


Does this deal only with worksheets that live in the user's SageNB Notebook, hidden in ~/.sage/sage_notebook.sagenb/home/...?

What about converting a collection of .sws files in a different folder?

slelievre gravatar imageslelievre ( 2018-03-27 04:13:27 -0500 )edit

Thanks, but sage -n export --list returns a table that looks pretty ugly in a text file: 'Unique ID | Notebook Name\n-------------------------------------------------------------------------------\n_sage_:0 | doc_browser_0\n_sage_:1 | doc_browser_0\n_sage_:2 | doc_browser_0\n_sage_:3 | doc_browser_0\n_sage_:4 and so on. Any idea how to get a list of tuples such as [('admin:11', 'output11'), ('admin:12', 'output12')]?

stan gravatar imagestan ( 2018-03-30 03:32:13 -0500 )edit

answered 2018-03-21 09:08:43 -0500

slelievre gravatar image

There is the SageNB exporter:

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: 2018-03-21 08:36:13 -0500

Seen: 71 times

Last updated: Mar 30