Ask Your Question
2

Mixing %cython and @interact in one cell

asked 2018-01-01 07:03:14 -0500

jepstra gravatar image

updated 2018-01-01 15:58:50 -0500

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.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2018-01-01 22:23:17 -0500

nbruin gravatar image

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.

Anyway, you can avoid the %cython magic and calling the cython environment from python:

cython("""
def function(...):
    ...
""")

@interact
def wrappe(...):
    ....
edit flag offensive delete link more

Comments

1

@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.

jepstra gravatar imagejepstra ( 2018-01-02 07:25:52 -0500 )edit
0

answered 2018-01-01 13:00:58 -0500

Emmanuel Charpentier gravatar image

I can't see why :

%cython
def function(...):
    ....

@interact
def wrappe(...):
    function(....)

would not work.

edit flag offensive delete link more

Comments

@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!

jepstra gravatar imagejepstra ( 2018-01-01 16:01:30 -0500 )edit

The 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.

nbruin gravatar imagenbruin ( 2018-01-04 21:18:52 -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

1 follower

Stats

Asked: 2018-01-01 07:03:14 -0500

Seen: 72 times

Last updated: Jan 01