Callback to sage from javascript?

63 views
Skip to first unread message

Reinhard Oldenburg

unread,
Sep 28, 2015, 5:06:20 AM9/28/15
to sage-notebook
Hi all, I wonder if it is possible to have a a Javascript/HTML5 canvas inside a sage notebook such that javascript can handle mouse motion events by calling back into sage. In particular, I want to have some objects that can be moved with the mouse and in reaction sage recalculates the coordinates of other objects.
I have done similar things in Mathematica and other combinations of software but a web based solution would b optimal for the educational purposes I have in mind.
Maybe jupyter is a good option too, but I got no repose so far on their mailing list.

Reinhard Oldenburg

Harald Schilly

unread,
Sep 28, 2015, 5:25:07 AM9/28/15
to sage-notebook
Hi, in a sagews worksheet, you can have @interact decorated functions, which add handlers to a plot. This is quite generic, and the following example only shows you the basics:

@interact
def f0(fun=x*sin(x^2), mousemove='', click='(0,0)'):
    click = sage_eval(click)
    g = plot(fun, (x,0,5), zorder=0) + point(click, color='red', pointsize=100, zorder=10)
    ymax = g.ymax(); ymin = g.ymin()
    m = fun.derivative(x)(x=click[0])
    b =  fun(x=click[0]) - m*click[0]
    g += plot(m*x + b, (click[0]-1,click[0]+1), color='red', zorder=10)
    def h(p):
        f0.mousemove = p
    def c(p):
        f0(click=p)
    show(g, events={'click':c, 'mousemove':h}, svg=True, gridlines='major', ymin=ymin, ymax=ymax)


​I'm pretty certain, something similar is possible for ipython notebooks, although you might have to look at various 3rd party ​libraries. Maybe bokeh?
In contrast to sagews, this bokeh gives you drag & zoomable plot areas. 

Maybe this:
(just a guess, I've never done something sophisticated with bokeh so far, just those plots)

-- harald




Reinhard Oldenburg

--
You received this message because you are subscribed to the Google Groups "sage-notebook" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-noteboo...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-notebook.
For more options, visit https://groups.google.com/d/optout.

kcrisman

unread,
Sep 28, 2015, 5:50:44 AM9/28/15
to sage-notebook

Hi, in a sagews worksheet, you can have @interact decorated functions, which add handlers to a plot. This is quite generic, and the following example only shows you the basics:


Note this is in the Sage Math Cloud, not in the Sage notebook.  I don't think this is quite implemented there. 

Harald Schilly

unread,
Sep 28, 2015, 9:41:08 AM9/28/15
to sage-notebook

On Mon, Sep 28, 2015 at 11:50 AM, kcrisman <kcri...@gmail.com> wrote:
Note this is in the Sage Math Cloud, not in the Sage notebook.  

​oh, you are right, I mixed up all these support emails :-)

-- h


Andrey Novoseltsev

unread,
Sep 28, 2015, 3:45:57 PM9/28/15
to sage-n...@googlegroups.com
SageMathCell also supports it (not sure how different are details from
the cloud)

>
> -- h

Reinhard Oldenburg

unread,
Sep 30, 2015, 8:52:17 AM9/30/15
to sage-notebook
Thanks a lot for the help. Somehow, however, I did not manage to get Haralds code to work under sage6.8 and chrome.
First, the option svg=True caused an error message. After removing it, notheing at all happened. No error message, just nothing - but the computation ran forever.
Seems not to be so easy.
Reinhard 

Andrey Novoseltsev

unread,
Oct 1, 2015, 4:42:11 PM10/1/15
to sage-notebook

kcrisman

unread,
Oct 5, 2015, 10:17:47 AM10/5/15
to sage-notebook, jason...@creativetrax.com, William Stein
YOU ARE MY HERO

But I assume this wouldn't be easy/possible with the current sagenb?   William, does this sort of thing work more or less the same in SMC or is there a different syntax needed?

William Stein

unread,
Oct 5, 2015, 12:36:33 PM10/5/15
to kcrisman, sage-notebook, Jason Grout
No.

William


--
William (http://wstein.org)

Norbert Domes

unread,
Oct 6, 2015, 6:26:08 PM10/6/15
to sage-notebook
Very cool!
Is there any documentation?
What kind of events are implemented?

My example: https://sagecell.sagemath.org/?q=vvxuej

Norbert

Andrey Novoseltsev

unread,
Oct 6, 2015, 7:09:39 PM10/6/15
to sage-notebook
On Tuesday, 6 October 2015 16:26:08 UTC-6, Norbert Domes wrote:
Very cool!
Is there any documentation?
 
Unfortunately no, if was Jason's experimental stuff which is still "experimental", I guess, but it works ;-)

What kind of events are implemented?

Not sure, but perhaps whatever you can have in JavaScript? I think I only used onclick, but onmove works as well, perhaps others. Feel free to experiment and report!
 
Reply all
Reply to author
Forward
0 new messages