EventHandler

class visigoth.common.event_handler.EventHandler(js, config={})

Create an event handler based on a javascript function

Parameters
  • js (str) – javascript function of the form function(channel,event,config,sendfn) { /* body */ }

  • config (dict) – configuration passed into each invocation (must be JSON serialisable)

Notes

The function is invoked with the following parameters:

channel is the name of the channel upon which the event is recieved obj is the event value config is the configuration of the function (this can be mutated between invocations) sendfn is a function for sending events which takes parameters obj (the event value) and channel (the name of the channel to output the event to)

Example

Screenshot (Link to open SVG for interactive features):

../_images/example9.png

Source Code (Link to Github):


import os
import sys
import argparse

from visigoth.diagram import Diagram
from visigoth.common.button import Button
from visigoth.common.event_handler import EventHandler

jscode1 = """
function(channel,obj,config,sendfn)
{
    alert("Code 1: Got event channel="+channel+",value="+obj);
}
"""

jscode2 = """
function(channel,obj,config,sendfn)
{
    alert("Code 2: Got event channel="+channel+",value="+obj);
}
"""

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--outpath", help="path for output SVG", default="example.svg")
    args = parser.parse_args()

    d = Diagram(fill="white")

    b1 = Button("Click Me!")
    b2 = Button("Or Click Me!")
    
    ev1 = EventHandler(jscode1,{})
    ev2 = EventHandler(jscode2,{})

    d.add(b1)
    d.add(b2)
    d.add(ev1)
    d.add(ev2)

    d.connect(b1,ev1,"click")
    d.connect(b2,ev2,"click")

    svg = d.draw()

    f = open(args.outpath, "wb")
    f.write(svg)
    f.close()