Ask Your Question
2

Mixing %cython and @interact in one cell

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

jepstra gravatar image

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

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-02 05:23:17 +0100

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 14:25:52 +0100 )edit
0

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

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 23:01:30 +0100 )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 +0100 )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 14:03:14 +0100

Seen: 417 times

Last updated: Jan 02 '18