def __init__(self, target_name='', data=None, metadata=None, buffers=None, **kwargs): self.primary = True # Am I the primary or secondary Comm? self.target_name = target_name # requirejs module from which to load comm target self.target_module = kwargs.get('target_module', None) self.open_hook = None self._closed = True self._close_callback = None self._msg_callback = None try: self.kernel = kwargs['kernel'] except KeyError: if Kernel.initialized(): self.kernel = Kernel.instance() else: self.kernel = None try: self.comm_id = kwargs['comm_id'] except KeyError: self.comm_id = uuid.uuid4().hex self.topic = kwargs.get('topic', ('comm-%s' % self.comm_id).encode('ascii')) self.log = log.get_logger() if self.kernel: if self.primary: # I am primary, open my peer. self.open(data=data, metadata=metadata, buffers=buffers) else: self._closed = False
def hold_comm_open(kernel=None): if kernel is None: if not Kernel.initialized(): raise ValueError( 'No kernel passed, and current kernel not initialized') kernel = Kernel.instance() comm_manager = getattr(kernel, 'comm_manager', None) if comm_manager is None: raise RuntimeError("Comms cannot be opened without a kernel " "and a comm_manager attached to that kernel.") patched_comms = [] messages = [] buffers = [] def patched_register_comm(comm): original_publish_msg = comm._publish_msg def patched_publish_msg(msg_type, **kwargs): if msg_type != 'comm_open': original_publish_msg(msg_type, **kwargs) msg, msg_bufs = _convert_message(comm, **kwargs) messages.append(msg) buffers.extend(msg_bufs) patched_comms.append((comm, original_publish_msg)) comm._publish_msg = patched_publish_msg original_register_comm = comm_manager.register_comm comm_manager.register_comm = patched_register_comm try: yield finally: comm_manager.register_comm = original_register_comm for comm, original_publish_msg in patched_comms: comm._publish_msg = original_publish_msg data = dict(messages=messages, ) args = dict(target_name='jupyter.widget-tunnel', data=data, buffers=buffers, metadata={'version': __protocol_version__}) comm = Comm(**args) comm.close()
def apply(self, expr, args, evaluation): 'Manipulate[expr_, args__]' if (not _jupyter) or (not Kernel.initialized()) or (Kernel.instance() is None): return evaluation.message('Manipulate', 'jupyter') instantiator = _WidgetInstantiator() # knows about the arguments and their widgets for arg in args.get_sequence(): try: if not instantiator.add(arg, evaluation): # not a valid argument pattern? return except IllegalWidgetArguments as e: return evaluation.message('Manipulate', 'widgetargs', strip_context(str(e.var))) except JupyterWidgetError as e: return evaluation.message('Manipulate', 'widgetmake', e.err) clear_output_callback = evaluation.output.clear display_data_callback = evaluation.output.display # for pushing updates try: clear_output_callback(wait=True) except NotImplementedError: return evaluation.message('Manipulate', 'imathics') def callback(**kwargs): clear_output_callback(wait=True) line_no = evaluation.definitions.get_line_no() vars = [Expression('Set', Symbol(name), value) for name, value in kwargs.items()] evaluatable = Expression('ReleaseHold', Expression('Module', Expression('List', *vars), expr)) result = evaluation.evaluate(evaluatable, timeout=settings.TIMEOUT) if result: display_data_callback(data=result.result, metadata={}) evaluation.definitions.set_line_no(line_no) # do not increment line_no for manipulate computations widgets = instantiator.get_widgets() if len(widgets) > 0: box = _interactive(instantiator.build_callback(callback), widgets) # create the widget formatter = IPythonDisplayFormatter() if not formatter(box): # make the widget appear on the Jupyter notebook return evaluation.message('Manipulate', 'widgetdisp') return Symbol('Null') # the interactive output is pushed via kernel.display_data_callback (see above)
def _default_kernel(self): if Kernel.initialized(): return Kernel.instance()