Ask Your Question
3

how to move from notebook() to jupyter

asked 2020-09-15 21:14:22 +0100

gg@g gravatar image

Seems that I missed a lot since I used sage the last time. I had to learn the hard way that notebook() will not work anymore in 9.1. With some hickups I managed to start jupyter using

sage -n jupyter --ip 127.0.0.1

but the main jupyter page leaves me clueless. It shows a listing of my sage root directory. How to configure it to look at ~/.sage (which is outside of this tree)? And, asking in advance, how to use my old notebooks? It seems that the notebook() function has changed to export something (for jupyter?), but instead it just complains about "ValueError: History is empty, cannot export". I moved my old .sage dir, the message is not related to the old content.

I didn not find suitable documentation. Where can I find a guide how to move from sageNB to jupyter and how to use it?

thanks

edit retag flag offensive close merge delete

Comments

An important change from the old sagenb to Jupyter is the notion of "present working directory" which exists in Jupyter notebook (see the description of ticket https://trac.sagemath.org/ticket/17203 which includes a citation of Fernando Perez on the philosophie of the Jupyter notebook which was then named IPython notebook). Each Jupyter notebook lives in a directory and you can choose it by calling sage -n jupyter from that directory or by giving it as an option --notebook-dir=~/path/to/chosen/directory

Sébastien gravatar imageSébastien ( 2020-09-16 10:57:42 +0100 )edit

3 Answers

Sort by » oldest newest most voted
2

answered 2020-09-18 17:49:35 +0100

gg@g gravatar image

Thanks for these Tips! Initially it seemed that 9.1 doesn't have the export function because a) I again have to specify --ip 127.0.0.1 to get rid of an error message but then b) there is nothing for the browser. After struggling with v8.9 (compilation failed on my openSuse, but the Ubuntu tarball did it) I realized that the behaviour is just the same.

That said, it seems to be a command line tool with little documentation on the options.

./sage -n export --ip=127.0.0.1 --list

creates a list of notebooks, e.g.

Unique ID       | Notebook Name
-------------------------------------------------------------------------------
_sage_:0        | Welcome to the Sage Tutorial! -- Sage Tutorial v5.11

This works in 8.9 and 9.1 (even if compiled for Python3). From there I'm digging deeper, there seems to be no "batch" conversion. Following https://ask.sagemath.org/question/41680/best-way-to-convert-many-notebooks-from-sagenb-to-jupyter/ I did this for a batch conversion of all notebooks, creating descriptive names:

# go to a new space
mkdir ~/.sage/jupyter_notebooks 
cd ~/.sage/jupyter_notebooks 
# make a list of IDs and worksheet names
/opt/sage-9.1/sage -n export --ip=127.0.0.1 --list > sagenb_list.txt
# prepare the arguments for the conversion
cat sagenb_list.txt | tail -n +3 | awk '{$2="" ; print "--ipynb=\""$0".ipynb\" "$1}' > map_list.txt
# run the conversion (sequentially, use)
cat map_list.txt |xargs -L 1 /opt/sage-9.1/sage -n export

And then started the notebook server with

/opt/sage-9.1/sage -n jupyter --notebook-dir="~/.sage/jupyter_notebooks" --ip=127.0.0.1

to view it in the browser at http://127.0.0.1:8888/tree. From my example above, the name of the exported notebook changed to

_sage_:0 Welcome to the Sage Tutorial! -- Sage Tutorial v5.11.ipynb

Magically I can even open them, but handling seems to be a bit different as well...I'll figure it out. Thanks again! :-)

edit flag offensive delete link more

Comments

Congratulations. Don't hesitate to accept your own answer as it can help other users who have the same question.

slelievre gravatar imageslelievre ( 2020-09-18 19:11:59 +0100 )edit

Thanks! I couldn't see the buttons on the left side with a white on black layout. I would have never noticed...

I add that the converted notebooks should be marked "Trusted" after opening them for the first time. Then they mostly work as before with very few adjustments (my stuff is from v6.5 and older). Happy to have this tool :-)

gg@g gravatar imagegg@g ( 2020-09-21 19:45:02 +0100 )edit
2

answered 2020-09-16 06:53:53 +0100

slelievre gravatar image

updated 2020-09-16 07:04:38 +0100

Expanding on Eric's answer.

Solution 1.

  • install Sage built for Python 2 or compile it from source (possible up to Sage 9.1)
  • then run sage -n which will offer a few choices:
    • run the old SageNB
    • export all SageNB worksheets (.sws) to Jupyter worksheets (.ipynb)
    • run Jupyter Notebook
  • or directly run sage -n export

Solution 2.

  • zip the .sage folder that exists in your user account on your computer
  • open an account on CoCalc if you don't have one,
  • create a new CoCalc project,
  • upload the zipped .sage to that new CoCalc project
  • unzip it there
  • in the CoCalc project's settings tab, change "software image" from "default" to "Ubuntu 18.04"
  • in a CoCalc terminal in that project, run sage_select 8.9
  • in a CoCalc terminal in that project, run sage -n export
  • then download the resulting .ipynb files back to your computer
edit flag offensive delete link more
1

answered 2020-09-15 22:00:33 +0100

eric_g gravatar image

updated 2020-09-15 22:20:27 +0100

Run

sage -n export

and look at the section Convert old notebooks to Jupyter. You should see the list of your old Sage notebooks (i.e. all those that are stored in ~/.sage). Clicking on any of them will convert it to Jupyter format.

NB: it seems that the conversion only works with Sage versions <= 8.9, i.e. with versions that were running Python 2 by default. For instance, you can run Sage 8.9 on CoCalc.

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

Stats

Asked: 2020-09-15 21:14:22 +0100

Seen: 1,143 times

Last updated: Sep 18 '20