Ask Your Question

Mixing %cython and @interact in one cell

asked 2018-01-01 14:03:14 +0200

jepstra gravatar image

updated 2018-01-01 22:58:50 +0200

I'm trying to make an interactive applet to visualize the Mandelbrot Set, much in the way of the implementation of interactive fractals ( - 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

def function(...)


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

def square(float x0):
    return x0*x0

def _(x0=-2.5):

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

answered 2018-01-02 05:23:17 +0200

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:

def function(...):

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



@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 14:25:52 +0200 )edit

answered 2018-01-01 20:00:58 +0200

Emmanuel Charpentier gravatar image

I can't see why :

def function(...):

def wrappe(...):

would not work.

edit flag offensive delete link more


@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 23:01:30 +0200 )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-05 04:18:52 +0200 )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


Asked: 2018-01-01 14:03:14 +0200

Seen: 302 times

Last updated: Jan 02 '18