Ask Your Question
0

Transferring variables between worksheets?

asked 2014-02-09 07:08:05 +0100

Alasdair gravatar image

How can I save an expression from one worksheet and load it in another? I can certainly do

    save(myvar,"myvar")

so that it will be available for future uses of the worksheet, but I want to open it up in another worksheet (basically I don't want to clog up this second worksheet with all the machinery used to create this variable). Can this be done easily?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-02-10 07:40:51 +0100

slelievre gravatar image

updated 2014-02-10 18:43:45 +0100

  1. Yes, it is easy to save and load Sage objects across worksheets, and outside worksheets (in command-line mode for instance). Sage objects are saved in .sobj files.

  2. You can load an object using either a url to the sage object file, or the file's full path. Now all we need is to either (a) save the file to a chosen location, or (b) understand where the file gets saved by default. Here are several options to achieve (a) and (b).

  3. You can specify a full path, instead of just a name, when saving your object. For instance,

    save(myvar,"/Users/me/Desktop/myvar.sobj")
    

    will save it on your desktop (adapt the path to your system if necessary).

  4. If, as in your example, you just type

    save(myvar,"myvar")
    

    in a notebook cell and evaluate the cell, the Sage object myvar gets saved as myvar.sobj somewhere.

    You probably noticed a link 'myvar.sobj' just below the worksheet cell with your save instruction. Click on that link and your browser will offer you to save the file myvar.sobj to the location of your choice. You can also right-click this link and choose 'Save link target as...' and pick a location to save it.

  5. The above-described right-click will also let you 'Copy link url'. Then, just paste the url to check the url to your file. The url you get should look like

    http://localhost:8080/home/admin/336/cells/2/myvar.sobj
    

    (in this example, I was using worksheet number 336 in my Sage notebook, and saved the object from cell number 2). You can then use this url to load your file.

  6. From the url above, you can also figure out the path to the file in your filesystem, which is somewhere in your your .sage folder. A little exploration reveals your file is at

    /Users/me/.sage/sage_notebook.sagenb/home/admin/336/cells/2/myvar.sobj
    

    (possibly slightly different depending on your system and Sage version). You can use this full path to load the object in another worksheet.

  7. You can find some examples of use of save and load by using the built-in help: type

    save?
    

    or

    load?
    

    in Sage. The examples in the documentation for save show some nice tricks, such as: giving a short name to the string that will contain the url or the file's full path; using os.path.join to concatenate the path to a directory and a filename (adds slashes as appropriate); using SAGE_TMP to save the file in a temporary location (gets cleaned up regularly, good for stuff you don't want to keep long-term).

edit flag offensive delete link more

Comments

As it was I found an even easier way: in a worksheet enter `print myvar` and simply copy and paste that output into any other worksheet. Simple; no need for extra files; does the job!

Alasdair gravatar imageAlasdair ( 2014-02-10 17:32:45 +0100 )edit

I should also have said: thank you VERY much for your long and detailed explanation of save/load.

Alasdair gravatar imageAlasdair ( 2014-02-10 17:33:27 +0100 )edit

@Alasdair: For some objects, the output of `print myvar` may be insufficient to input the object again. But when it is, you are pefectly right: simplest, best.

slelievre gravatar imageslelievre ( 2014-02-10 19:11:34 +0100 )edit

@Alasdair: When an answer solves an ask-sage question, the original poster can click the 'Accept' button. Three things happen then: on the list of questions on the main page of ask-sage, the question is displayed as solved; on the question's page, the accepted answer is highlighted and stands out among other answers; the person who provided the accepted answer gets extra karma points.

slelievre gravatar imageslelievre ( 2014-02-10 19:11:55 +0100 )edit

@Alasdair: I should also have said: thanks for your thanks! Glad if it helps.

slelievre gravatar imageslelievre ( 2014-02-10 19:14:24 +0100 )edit

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: 2014-02-09 07:08:05 +0100

Seen: 857 times

Last updated: Feb 10 '14