Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def open(self):
"""Open a comm to the frontend if one isn't already open."""
if self.comm is None:
state, buffer_keys, buffers = self._split_state_buffers(self.get_state())
args = dict(target_name='jupyter.widget', data=state)
if self._model_id is not None:
args['comm_id'] = self._model_id
self.comm = Comm(**args)
if buffers:
# FIXME: workaround ipykernel missing binary message support in open-on-init
# send state with binary elements as second message
self.send_state()
def _on_action_details(self, msg):
params = msg['content']['data']['content']
graphics_object = None
for item in self.chart.graphics_list:
if item.uid == params['itemId']:
graphics_object = item
action_type = params['params']['actionType']
if action_type == 'onclick' or action_type == 'onkey':
self.details = GraphicsActionObject(graphics_object, params['params'])
arguments = dict(target_name='beakerx.tag.run')
comm = Comm(**arguments)
msg = {'runByTag': params['params']['tag']}
state = {'state': msg}
comm.send(data=state, buffers=[])
def __init__(self, id):
# Use comm to send a message from the kernel
self.comm = Comm(target_name=id, data={})
def connect():
"""
establish connection to frontend notebook
"""
if not is_notebook():
print('Python session is not running in a Notebook Kernel')
return
global _comm
kernel=get_ipython().kernel
kernel.comm_manager.register_target('tdb',handle_comm_opened)
# initiate connection to frontend.
_comm=Comm(target_name='tdb',data={})
# bind recv handler
_comm.on_msg(None)
def _run_by_tag(self, tag):
arguments = dict(target_name='beakerx.tag.run')
comm = Comm(**arguments)
msg = {'runByTag': tag}
state = {'state': msg}
comm.send(data=state, buffers=[])
outputs = self.model.predict(inputs, batch_size=batch_size)
## Shape the outputs:
if self.num_target_layers == 1:
shape = self[self.output_bank_order[0]].shape
try:
outputs = outputs[0].reshape(shape).tolist()
except:
outputs = outputs[0].tolist() # can't reshape; maybe a dynamically changing output
else:
shapes = [self[layer_name].shape for layer_name in self.output_bank_order]
## FIXME: may not be able to reshape; dynamically changing output
outputs = [outputs[i].reshape(shapes[i]).tolist() for i in range(len(self.output_bank_order))]
if visualize and get_ipython():
if not self._comm:
from ipykernel.comm import Comm
self._comm = Comm(target_name='conx_svg_control')
if self._comm.kernel:
for layer in self.layers:
if layer.visible and layer.model:
image = self.propagate_to_image(layer.name, input, batch_size, visualize=False)
data_uri = self._image_to_uri(image)
self._comm.send({'class': "%s_%s" % (self.name, layer.name), "href": data_uri})
return outputs
else:
outputs = list(self[layer_name]._output(np.array([input]))[0])
else:
inputs = [np.array(x, "float32") for x in input]
# get just inputs for this layer, in order:
inputs = [inputs[self.input_layer_order.index(name)] for name in self[layer_name].input_names]
if len(inputs) == 1:
inputs = inputs[0]
if batch_size is not None:
outputs = list(self[layer_name]._output(inputs, batch_size=batch_size)[0])
else:
outputs = list(self[layer_name]._output(inputs)[0])
if self.visualize:
if not self._comm:
from ipykernel.comm import Comm
self._comm = Comm(target_name='conx_svg_control')
array = np.array(outputs)
image = self._make_image(layer_name, array, colormap=self[layer_name].colormap)
data_uri = self._image_to_uri(image)
self._comm.send({'id': "%s_%s" % (self.name, layer_name), "href": data_uri})
return outputs
def create_comm(target: str,
data: dict = None,
callback: callable = None,
**kwargs):
"""Create ipykernel message comm."""
# create comm on python site
comm = Comm(target_name=target, data=data, **kwargs)
comm.on_msg(callback)
return comm
ValueError
The IPython kernel does not contain a communication manager (the
default IPython shell rather than a notebook or a QT console).
"""
try:
from IPython import get_ipython
from ipykernel.comm import Comm
from traitlets.traitlets import TraitError
if get_ipython() is None:
raise ValueError('menpowidgets can only be used from inside '
'a Jupyter notebook. We were unable to detect '
'an active IPython session. Please re-run your '
'code from inside a Jupyter Notebook.')
try:
Comm()
except TraitError:
raise ValueError('menpowidgets can only be used from inside '
'a Jupyter notebook. We were unable to detect '
'a valid active kernel. Please ensure your code '
'is running inside a Jupyter notebook and not '
'a standalone script or the default IPython '
'shell.')
except ImportError:
raise ValueError('menpowidgets can only be used from inside '
'a Jupyter notebook. We were unable to import '
def init_comm(self) -> None:
self.close()
self.current_comm = Comm(target_name=self.comm_name)
self.current_comm.on_msg(self.on_msg)
atexit.register(lambda: self.close())