Skip to content

Commit

Permalink
Importing comms machinery from pyviz_comms (#2720)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens authored and philippjfr committed Jun 4, 2018
1 parent ff6de1f commit a631770
Show file tree
Hide file tree
Showing 15 changed files with 15 additions and 398 deletions.
1 change: 1 addition & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ requirements:
run:
- python
- param >=1.6.1,<2.0
- pyviz_comms
- numpy
- matplotlib>=2.1
- bokeh >=0.12.15,<=0.12.16
Expand Down
2 changes: 1 addition & 1 deletion doc/builder
Submodule builder updated 1 files
+1 −1 gallery.py
2 changes: 1 addition & 1 deletion doc/nbpublisher
78 changes: 2 additions & 76 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import param
from bokeh.models import (CustomJS, FactorRange, DatetimeAxis, ColumnDataSource, Selection)
from pyviz_comms import JS_CALLBACK

from ...core import OrderedDict
from ...core.util import dimension_sanitizer
Expand Down Expand Up @@ -156,82 +157,7 @@ class CustomJSCallback(MessageCallback):
to Python using a Comms instance.
"""

js_callback = """
function unique_events(events) {{
// Processes the event queue ignoring duplicate events
// of the same type
var unique = [];
var unique_events = [];
for (var i=0; i<events.length; i++) {{
var _tmpevent = events[i];
event = _tmpevent[0];
data = _tmpevent[1];
if (unique_events.indexOf(event)===-1) {{
unique.unshift(data);
unique_events.push(event);
}}
}}
return unique;
}}
function process_events(comm_status) {{
// Iterates over event queue and sends events via Comm
var events = unique_events(comm_status.event_buffer);
for (var i=0; i<events.length; i++) {{
var data = events[i];
var comm = HoloViews.comms[data["comm_id"]];
comm.send(data);
}}
comm_status.event_buffer = [];
}}
function on_msg(msg) {{
// Receives acknowledgement from Python, processing event
// and unblocking Comm if event queue empty
msg = JSON.parse(msg.content.data);
var comm_id = msg["comm_id"]
var comm_status = HoloViews.comm_status[comm_id];
if (comm_status.event_buffer.length) {{
process_events(comm_status);
comm_status.blocked = true;
comm_status.time = Date.now()+{debounce};
}} else {{
comm_status.blocked = false;
}}
comm_status.event_buffer = [];
if ((msg.msg_type == "Ready") && msg.content) {{
console.log("Python callback returned following output:", msg.content);
}} else if (msg.msg_type == "Error") {{
console.log("Python failed with the following traceback:", msg['traceback'])
}}
}}
// Initialize Comm
comm = HoloViews.comm_manager.get_client_comm("{plot_id}", "{comm_id}", on_msg);
if (!comm) {{
return
}}
// Initialize event queue and timeouts for Comm
var comm_status = HoloViews.comm_status["{comm_id}"];
if (comm_status === undefined) {{
comm_status = {{event_buffer: [], blocked: false, time: Date.now()}}
HoloViews.comm_status["{comm_id}"] = comm_status
}}
// Add current event to queue and process queue if not blocked
event_name = cb_obj.event_name
data['comm_id'] = "{comm_id}";
timeout = comm_status.time + {timeout};
if ((comm_status.blocked && (Date.now() < timeout))) {{
comm_status.event_buffer.unshift([event_name, data]);
}} else {{
comm_status.event_buffer.unshift([event_name, data]);
setTimeout(function() {{ process_events(comm_status); }}, {debounce});
comm_status.blocked = true;
comm_status.time = Date.now()+{debounce};
}}
"""
js_callback = JS_CALLBACK

code = ""

Expand Down
Loading

0 comments on commit a631770

Please sign in to comment.