First time here? Check out the FAQ!

Ask Your Question
1

best way to convert many notebooks from sagenb to jupyter?

asked 7 years ago

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!

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 6 years ago

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)
Preview: (hide)
link
2

answered 7 years ago

eric_g gravatar image

updated 7 years ago

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.

Preview: (hide)
link

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 ( 7 years ago )

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 ( 6 years ago )

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 ( 5 years ago )

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

avp gravatar imageavp ( 5 years ago )
0

answered 7 years ago

slelievre gravatar image

There is the SageNB exporter:

Preview: (hide)
link

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: 7 years ago

Seen: 1,859 times

Last updated: Mar 30 '18