def _log_load(self, where, ob): n1, n2, n3 = len(ob.settings), len(ob.records) if n1 > 0 or n2 > 0 or n3 > 0: console.log("Loading from " + where + ": " + [n1, n2, n3])
def _receive_command(self, command): """ Process a command send from the server. """ cmd = command[0] if cmd == 'PING': # Used for roundtrip stuff, do at least one iter loop here ... window.setTimeout(self.send_command, 10, 'PONG', command[1]) elif cmd == 'INIT_DONE': window.flexx.spin(None) while len(self._pending_commands): self._receive_raw_command(self._pending_commands.pop(0)) self._pending_commands = None # print('init took', time() - self._init_time) elif cmd == 'PRINT': (window.console.ori_log or window.console.log)(command[1]) elif cmd == 'EXEC': eval(command[1]) elif cmd == 'EVAL': x = None if len(command) == 2: x = eval(command[1]) elif len(command) == 3: x = eval('this.instances.' + command[1] + '.' + command[2]) console.log(str(x)) # print (and thus also sends back result) elif cmd == 'EVALANDRETURN': try: x = eval(command[1]) except Exception as err: x = str(err) eval_id = command[2] # to identify the result in Python self.send_command("EVALRESULT", x, eval_id) elif cmd == 'INVOKE': id, name, args = command[1:] ob = self.instances.get(id, None) if ob is None: console.warn('Cannot invoke %s.%s; ' 'session does not know it (anymore).' % (id, name)) elif ob._disposed is True: pass # deleted, but other end might not be aware when command was send else: ob[name](*args) elif cmd == 'INSTANTIATE': self.instantiate_component( *command[1:]) # module, cname, id, args, kwargs elif cmd == 'DISPOSE': id = command[1] c = self.instances.get(id, None) if c is not None and c._disposed is False: # else: no need to warn c._dispose() self.send_command('DISPOSE_ACK', command[1]) self.instances.pop(id, None) # Drop local reference now elif cmd == 'DISPOSE_ACK': self.instances.pop(command[1], None) # Drop reference elif cmd == 'DEFINE': #and command[1] == 'JS' or command[1] == 'DEFINE-JS-EVAL '): kind, name, code = command[1:] window.flexx.spin() address = window.location.protocol + '//' + self.ws_url.split( '/')[2] code += '\n//# sourceURL=%s/flexx/assets/shared/%s\n' % (address, name) if kind == 'JS-EVAL': eval(code) elif kind == 'JS': # With this method, sourceURL does not work on Firefox, # but eval might not work for assets that don't "use strict" # (e.g. Bokeh). Note, btw, that creating links to assets does # not work because these won't be loaded on time. el = window.document.createElement("script") el.id = name el.innerHTML = code window.flexx.asset_node.appendChild(el) elif kind == 'CSS': el = window.document.createElement("style") el.type = "text/css" el.id = name el.innerHTML = code window.flexx.asset_node.appendChild(el) else: window.console.error('Dont know how to DEFINE ' + name + ' with "' + kind + '".') elif cmd == 'OPEN': window.win1 = window.open(command[1], 'new', 'chrome') else: window.console.error('Invalid command: "' + cmd + '"') return command
def _receive_command(self, command): """ Process a command send from the server. """ cmd = command[0] if cmd == 'PING': # Used for roundtrip stuff, do at least one iter loop here ... window.setTimeout(self.send_command, 10, 'PONG', command[1]) elif cmd == 'INIT_DONE': window.flexx.spin(None) while len(self._pending_commands): self._receive_raw_command(self._pending_commands.pop(0)) self._pending_commands = None # print('init took', time() - self._init_time) elif cmd == 'PRINT': (window.console.ori_log or window.console.log)(command[1]) elif cmd == 'EXEC': eval(command[1]) elif cmd == 'EVAL': x = None if len(command) == 2: x = eval(command[1]) elif len(command) == 3: x = eval('this.instances.' + command[1] + '.' + command[2]) console.log(str(x)) # print (and thus also sends back result) elif cmd == 'EVALANDRETURN': try: x = eval(command[1]) except Exception as err: x = str(err) eval_id = command[2] # to identify the result in Python self.send_command("EVALRESULT", x, eval_id) elif cmd == 'INVOKE': id, name, args = command[1:] ob = self.instances.get(id, None) if ob is None: console.warn('Cannot invoke %s.%s; ' 'session does not know it (anymore).' % (id, name)) elif ob._disposed is True: pass # deleted, but other end might not be aware when command was send else: ob[name](*args) elif cmd == 'INSTANTIATE': self.instantiate_component(*command[1:]) # module, cname, id, args, kwargs elif cmd == 'DISPOSE': id = command[1] c = self.instances.get(id, None) if c is not None and c._disposed is False: # else: no need to warn c._dispose() self.send_command('DISPOSE_ACK', command[1]) self.instances.pop(id, None) # Drop local reference now elif cmd == 'DISPOSE_ACK': self.instances.pop(command[1], None) # Drop reference elif cmd == 'DEFINE': #and command[1] == 'JS' or command[1] == 'DEFINE-JS-EVAL '): kind, name, code = command[1:] window.flexx.spin() address = window.location.protocol + '//' + self.ws_url.split('/')[2] code += '\n//# sourceURL=%s/flexx/assets/shared/%s\n' % (address, name) if kind == 'JS-EVAL': eval(code) elif kind == 'JS': # With this method, sourceURL does not work on Firefox, # but eval might not work for assets that don't "use strict" # (e.g. Bokeh). Note, btw, that creating links to assets does # not work because these won't be loaded on time. el = window.document.createElement("script") el.id = name el.innerHTML = code window.flexx.asset_node.appendChild(el) elif kind == 'CSS': el = window.document.createElement("style") el.type = "text/css" el.id = name el.innerHTML = code window.flexx.asset_node.appendChild(el) else: window.console.error('Dont know how to DEFINE ' + name + ' with "' + kind + '".') elif cmd == 'OPEN': window.win1 = window.open(command[1], 'new', 'chrome') else: window.console.error('Invalid command: "' + cmd + '"') return command