From 8414f38e8081ec9251e3cd149c0bfcd1c7a2ae58 2014-10-24 19:15:55 From: Thomas Kluyver Date: 2014-10-24 19:15:55 Subject: [PATCH] Support specifying requirejs modules for comm targets --- diff --git a/IPython/html/static/services/kernels/comm.js b/IPython/html/static/services/kernels/comm.js index 91f3dc8..f25517b 100644 --- a/IPython/html/static/services/kernels/comm.js +++ b/IPython/html/static/services/kernels/comm.js @@ -65,20 +65,40 @@ define([ CommManager.prototype.comm_open = function (msg) { var content = msg.content; - var f = this.targets[content.target_name]; - if (f === undefined) { - console.log("No such target registered: ", content.target_name); - console.log("Available targets are: ", this.targets); - return; - } - var comm = new Comm(content.target_name, content.comm_id); - this.register_comm(comm); - try { - f(comm, msg); - } catch (e) { - console.log("Exception opening new comm:", e, e.stack, msg); - comm.close(); - this.unregister_comm(comm); + var that = this; + + var instantiate_comm = function(target) { + var comm = new Comm(content.target_name, content.comm_id); + that.register_comm(comm); + try { + target(comm, msg); + } catch (e) { + console.log("Exception opening new comm:", e, e.stack, msg); + comm.close(); + that.unregister_comm(comm); + } + }; + + if (content.target_module) { + // Load requirejs module for comm target + require([content.target_module], function(mod) { + var target = mod[content.target_name]; + if (target !== undefined) { + instantiate_comm(target) + } else { + console.log("Comm target " + content.target_name + + " not found in module " + content.target_module); + } + }, function(err) { console.log(err); }); + } else { + // No requirejs module specified: look for target in registry + var f = this.targets[content.target_name]; + if (f === undefined) { + console.log("No such target registered: ", content.target_name); + console.log("Available targets are: ", this.targets); + return; + } + instantiate_comm(f) } }; diff --git a/IPython/kernel/comm/comm.py b/IPython/kernel/comm/comm.py index 4f5660c..3a65cb0 100644 --- a/IPython/kernel/comm/comm.py +++ b/IPython/kernel/comm/comm.py @@ -31,6 +31,8 @@ class Comm(LoggingConfigurable): return self.kernel.session target_name = Unicode('comm') + target_module = Unicode(None, allow_none=True, help="""requirejs module from + which to load comm target.""") topic = Bytes() def _topic_default(self): @@ -91,7 +93,9 @@ class Comm(LoggingConfigurable): try: self._publish_msg('comm_open', data=data, metadata=metadata, buffers=buffers, - target_name=self.target_name) + target_name=self.target_name, + target_module=self.target_module, + ) self._closed = False except: comm_manager.unregister_comm(self)