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.Tue, 10 Sep 2019 14:12:56 +0200How to re-use value from interact?https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/ I have 3 subsequent SageMath code cells C1, C2, C3 in a Jupyter notebook.
1. C1 sets a value a=1
2. C2 has an interact widget (say a slider for a1) using the value of a as default
3. C3 should assign the current value of a1 to a and proceed.
What do I have to write in C3 to access the current value of a1 in C2?Tue, 10 Sep 2019 12:17:24 +0200https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/Answer by tmonteil for <p>I have 3 subsequent SageMath code cells C1, C2, C3 in a Jupyter notebook.</p>
<ol>
<li>C1 sets a value a=1 </li>
<li>C2 has an interact widget (say a slider for a1) using the value of a as default</li>
<li>C3 should assign the current value of a1 to a and proceed.</li>
</ol>
<p>What do I have to write in C3 to access the current value of a1 in C2?</p>
https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/?answer=47815#post-id-47815You can try with global variables, something like:
Cell 1
a = 3
Cell 2
@interact(n=a)
def square(n):
global a1
print("{} squared is {}".format(n, n*n))
a1 = n
Cell 3
a1
Tue, 10 Sep 2019 13:58:49 +0200https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/?answer=47815#post-id-47815Comment by Ingo for <p>You can try with global variables, something like:</p>
<p>Cell 1</p>
<pre><code>a = 3
</code></pre>
<p>Cell 2</p>
<pre><code>@interact(n=a)
def square(n):
global a1
print("{} squared is {}".format(n, n*n))
a1 = n
</code></pre>
<p>Cell 3</p>
<pre><code>a1
</code></pre>
https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/?comment=47816#post-id-47816Great, thanks!Tue, 10 Sep 2019 14:12:56 +0200https://ask.sagemath.org/question/47813/how-to-re-use-value-from-interact/?comment=47816#post-id-47816