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.Fri, 05 Jan 2018 04:18:52 +0100Mixing %cython and @interact in one cellhttps://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/I'm trying to make an interactive applet to visualize the Mandelbrot Set, much in the way of the implementation of interactive fractals (wiki.sagemath.org/interact/fractal#Exploring_Mandelbrot - cannot copy full link because of not enough karma ;-))
But I'd like to embed the applet in a webpage using SageMathCell, and for this all the code must be encapsulated in just one cell, something in the following scheme
%cython
def function(...)
...
@interact
...
Unfortunately, `%cython` and `@interact` behave badly together, because the latter is unknown to the former (just try to join the code in the two cells of the example above or try the following minimal working example
%cython
def square(float x0):
return x0*x0
@interact
def _(x0=-2.5):
print(square(x0))
which, surprisingly enough, raises different errors in a online Sage Cell and in a Sage Notebook Cell)
Which would be the smartest way to mix everything up? Any suggestion is highly appreciated.Mon, 01 Jan 2018 14:03:14 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/Answer by Emmanuel Charpentier for <p>I'm trying to make an interactive applet to visualize the Mandelbrot Set, much in the way of the implementation of interactive fractals (wiki.sagemath.org/interact/fractal#Exploring_Mandelbrot - cannot copy full link because of not enough karma ;-))</p>
<p>But I'd like to embed the applet in a webpage using SageMathCell, and for this all the code must be encapsulated in just one cell, something in the following scheme</p>
<pre><code>%cython
def function(...)
...
@interact
...
</code></pre>
<p>Unfortunately, <code>%cython</code> and <code>@interact</code> behave badly together, because the latter is unknown to the former (just try to join the code in the two cells of the example above or try the following minimal working example</p>
<pre><code>%cython
def square(float x0):
return x0*x0
@interact
def _(x0=-2.5):
print(square(x0))
</code></pre>
<p>which, surprisingly enough, raises different errors in a online Sage Cell and in a Sage Notebook Cell)</p>
<p>Which would be the smartest way to mix everything up? Any suggestion is highly appreciated.</p>
https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?answer=40427#post-id-40427I can't see why :
%cython
def function(...):
....
@interact
def wrappe(...):
function(....)
would not work.Mon, 01 Jan 2018 20:00:58 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?answer=40427#post-id-40427Comment by jepstra for <p>I can't see why :</p>
<pre><code>%cython
def function(...):
....
@interact
def wrappe(...):
function(....)
</code></pre>
<p>would not work.</p>
https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40433#post-id-40433@emmanuel-charpentier I have added a minimal working example (of course the actual code is much larger) that does not compile neither in a Sage notebook nor a SageMathCell. Could you try it? Thank you very much!Mon, 01 Jan 2018 23:01:30 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40433#post-id-40433Comment by nbruin for <p>I can't see why :</p>
<pre><code>%cython
def function(...):
....
@interact
def wrappe(...):
function(....)
</code></pre>
<p>would not work.</p>
https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40466#post-id-40466The interact decorator is quite involved because it needs to generate the html to display the interact as well. I'm not so terribly surprised that it doesn't work in cython. Given the analysis it needs to do on default arguments, I would expect that you cannot decorate non python-function callables with it either.Fri, 05 Jan 2018 04:18:52 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40466#post-id-40466Answer by nbruin for <p>I'm trying to make an interactive applet to visualize the Mandelbrot Set, much in the way of the implementation of interactive fractals (wiki.sagemath.org/interact/fractal#Exploring_Mandelbrot - cannot copy full link because of not enough karma ;-))</p>
<p>But I'd like to embed the applet in a webpage using SageMathCell, and for this all the code must be encapsulated in just one cell, something in the following scheme</p>
<pre><code>%cython
def function(...)
...
@interact
...
</code></pre>
<p>Unfortunately, <code>%cython</code> and <code>@interact</code> behave badly together, because the latter is unknown to the former (just try to join the code in the two cells of the example above or try the following minimal working example</p>
<pre><code>%cython
def square(float x0):
return x0*x0
@interact
def _(x0=-2.5):
print(square(x0))
</code></pre>
<p>which, surprisingly enough, raises different errors in a online Sage Cell and in a Sage Notebook Cell)</p>
<p>Which would be the smartest way to mix everything up? Any suggestion is highly appreciated.</p>
https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?answer=40435#post-id-40435I think you shouldn't put the cython definition and the interact in the same cell, because the cython cell will call a C compiler and will be quite expensive to execute. It doesn't make sense to weigh down every execution of the interact by the C compiler call.
Anyway, you can avoid the `%cython` magic and calling the cython environment from python:
cython("""
def function(...):
...
""")
@interact
def wrappe(...):
....
Tue, 02 Jan 2018 05:23:17 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?answer=40435#post-id-40435Comment by jepstra for <p>I think you shouldn't put the cython definition and the interact in the same cell, because the cython cell will call a C compiler and will be quite expensive to execute. It doesn't make sense to weigh down every execution of the interact by the C compiler call.</p>
<p>Anyway, you can avoid the <code>%cython</code> magic and calling the cython environment from python:</p>
<pre><code>cython("""
def function(...):
...
""")
@interact
def wrappe(...):
....
</code></pre>
https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40439#post-id-40439@nbruin Works like a charm!! This was the way I needed to reduce the scope of the cython environment. I didn't know how to do it.Tue, 02 Jan 2018 14:25:52 +0100https://ask.sagemath.org/question/40417/mixing-cython-and-interact-in-one-cell/?comment=40439#post-id-40439