ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 20 Jul 2015 15:45:11 +0200How can I share code between worksheets?https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/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(...)Sat, 03 Sep 2011 10:05:20 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/Answer by Felix Lawrence for <p>I have written and tested some code in a worksheet and I want to reuse it in another worksheet. How can I do it?</p>
<p>For e.g. I want to be able to do the following:</p>
<p>Worksheet 1: def tested_code_i_want_to_reuse(...): ...</p>
<p>Worksheet 2: tested_code_i_want_to_reuse(...)</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?answer=12627#post-id-12627You 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.Mon, 05 Sep 2011 23:10:51 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?answer=12627#post-id-12627Comment by twoerd for <p>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</p>
<pre><code>%auto
attach('/path/to/my/file.py')
</code></pre>
<p><code>attach</code> 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.</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=28652#post-id-28652@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.Mon, 20 Jul 2015 15:45:11 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=28652#post-id-28652Comment by Felix Lawrence for <p>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</p>
<pre><code>%auto
attach('/path/to/my/file.py')
</code></pre>
<p><code>attach</code> 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.</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21268#post-id-21268The 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.Sun, 11 Sep 2011 00:32:29 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21268#post-id-21268Comment by G-Sage for <p>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</p>
<pre><code>%auto
attach('/path/to/my/file.py')
</code></pre>
<p><code>attach</code> 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.</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21270#post-id-21270@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!Sat, 10 Sep 2011 19:01:35 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21270#post-id-21270Answer by Dima for <p>I have written and tested some code in a worksheet and I want to reuse it in another worksheet. How can I do it?</p>
<p>For e.g. I want to be able to do the following:</p>
<p>Worksheet 1: def tested_code_i_want_to_reuse(...): ...</p>
<p>Worksheet 2: tested_code_i_want_to_reuse(...)</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?answer=12620#post-id-12620IMHO 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.
Sun, 04 Sep 2011 07:48:00 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?answer=12620#post-id-12620Comment by kcrisman for <p>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.</p>
<hr/>
<p>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: </p>
<pre><code>sage: from sage.matrix.constructor import my_blahblah
sage: my_blahblah()
'foo'
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21279#post-id-21279@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 :)Mon, 05 Sep 2011 09:43:53 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21279#post-id-21279Comment by skoorc8 for <p>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.</p>
<hr/>
<p>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: </p>
<pre><code>sage: from sage.matrix.constructor import my_blahblah
sage: my_blahblah()
'foo'
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21283#post-id-21283This was really helpful. Thank you very much.Sun, 04 Sep 2011 11:00:49 +0200https://ask.sagemath.org/question/8303/how-can-i-share-code-between-worksheets/?comment=21283#post-id-21283