Ask Your Question
1

best way to convert many notebooks from sagenb to jupyter?

asked 2018-03-21 14:36:13 +0100

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 https://ask.sagemath.org/question/358..., 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
2

answered 2018-03-30 11:12:22 +0100

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
os.chdir('/path_to_fromsagenb/')
f = open('list.txt')
str1 = f.read()
str2 = str1.splitlines()
for name in str2:
    command = '~/Programs/SageMath/sage -n export --ipynb={0}.ipynb {0}'.format(name)
    os.system(command)
edit flag offensive delete link more
2

answered 2018-03-21 15:22:19 +0100

eric_g gravatar image

updated 2018-03-21 15:30:46 +0100

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

Comments

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 11:13:27 +0100 )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 10:32:13 +0100 )edit

I'm not sure I believe this answer. I'm running 8.7, and when I try that command I get

$ ./sage -n export -list
usage: sagenb-export [-h] [--log LOG] [--dot-sage DOT_SAGE] [--list]
                     [--ipynb IPYNB] [--print]
                     [sagenb]
sagenb-export: error: unrecognized arguments: -list

So I can't quite figure out what the correct commands are.

thethinker gravatar imagethethinker ( 2019-06-19 20:31:49 +0100 )edit

@thethinker I think you left out the second dash in front of list. This worked for me: sage -n export --list

avp gravatar imageavp ( 2019-10-26 23:01:06 +0100 )edit
0

answered 2018-03-21 15:08:43 +0100

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

2 followers

Stats

Asked: 2018-03-21 14:36:13 +0100

Seen: 1,804 times

Last updated: Mar 30 '18