def _get_javascript_runner(self, mode): """Returns an appropriate function that executes the given javascript.""" if mode == PERSISTENT: # Note: want lazy binding, particularly for tests to be able # to inject custom handling. # pylint: disable=unnecessary-lambda return lambda x: _publish.javascript(x) elif mode == EVAL: # Note: we don't want javascript value on python side # unless user specifically requests it using .eval(). # This allows us to properly chain function and javascript class # (since those are not serializable) and eliminates the need to wait # for frontend to return. return lambda x: _js.eval_js('(()=>{' + x + '})()', ignore_result=True) else: raise JsException('Invalid mode: %r.' % mode)
def eval(self): """Evals the content on javascript side and returns result. Note: if the result of this javascript computation is not json serializable (e.g. it is a function or class) this will fail. This function does not affect the underlying ipynb. Usage example: # this is executed on javascript side, x is opaque reference to # the result of my_function computation. x = output.js_global.my_class.my_function(1, 2, 3) # This gets the value to python print x.eval() This works with any javascript mode. Returns: evaled javascript. """ return _js.eval_js(self._js_value())