Ask Your Question
3

How can I share code between worksheets?

asked 2011-09-03 03:05:20 -0500

skoorc8 gravatar image

I have written and tested some code in a worksheet and I want to reuse it in another worksheet. How can I do it?

For e.g. I want to be able to do the following:

Worksheet 1: def tested_code_i_want_to_reuse(...): ...

Worksheet 2: tested_code_i_want_to_reuse(...)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2011-09-05 16:10:51 -0500

Felix Lawrence gravatar image

You can write your reuseable functions into a .sage or .py file, store it anywhere on your computer (or the web), and then somewhere in the worksheet (e.g. the top), have a cell with

%auto
attach('/path/to/my/file.py')

attach will run all the code in your file, every time the file changes, so the functions in the file will be available from the worksheet.

edit flag offensive delete link more

Comments

@Felix Lawrence Okay... so how do I do this if I'm using Sage 4.7 for Windows in Virtual Box??? I don't know how the file system works at all. Thanks!

G-Sage gravatar imageG-Sage ( 2011-09-10 12:01:35 -0500 )edit

The best way to do it would be to let Virtual Box see the windows directories that you're saving the files in, and give that as the path. I don't have Windows, but on my Mac version of VirtualBox there is a Devices menu with a "Shared Folders..." item, that will set this up. Hopefully you can then work out the path of the shared folder - I guess you should ask a separate question for that, someone with more specific knowledge may be able to help.

Felix Lawrence gravatar imageFelix Lawrence ( 2011-09-10 17:32:29 -0500 )edit

@Felix Lawrence I'm trying what you've suggested I'm getting a weird error: NameError: name 'Integer' is not defined. However, if I simply copy paste the code into the notebook there are no errors and everything runs as suggested. Do you have any suggestions.

twoerd gravatar imagetwoerd ( 2015-07-20 08:45:11 -0500 )edit
4

answered 2011-09-04 00:48:00 -0500

IMHO worksheets are good for some rapid prototyping, but what you talk about, essentially, developing your own mini-library of functions, is better done in shell.


What you can do anyway, if you control the Sage installation, is adding the Sage code you want to share between worksheets somewhere to Sage library. E.g. suppose you added a Python function my_blahblah to the file $SAGEROOT/devel/sage/sage/matrix/constructor.py Then you run sage -b to rebuild the library, and after that it is there:

sage: from sage.matrix.constructor import my_blahblah
sage: my_blahblah()
'foo'

If you want to have it available without importing, you can add this import statement to $SAGEROOT/devel/sage/sage/matrix/all.py, then this new function will be known right from the start of Sage.

edit flag offensive delete link more

Comments

This was really helpful. Thank you very much.

skoorc8 gravatar imageskoorc8 ( 2011-09-04 04:00:49 -0500 )edit

@skoorc8: If you think this was a "correct" answer, feel free to click the check mark and give Dima the credit he's earned by answering your question :)

kcrisman gravatar imagekcrisman ( 2011-09-05 02:43:53 -0500 )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

2 followers

Stats

Asked: 2011-09-03 03:05:20 -0500

Seen: 1,068 times

Last updated: Sep 05 '11