本文整理汇总了Python中ipykernel.comm.Comm类的典型用法代码示例。如果您正苦于以下问题:Python Comm类的具体用法?Python Comm怎么用?Python Comm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Comm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WidgetCommSocket
class WidgetCommSocket(CommSocket):
"""
CustomCommSocket provides communication between the IPython
kernel and a matplotlib canvas element in the notebook.
A CustomCommSocket is required to delay communication
between the kernel and the canvas element until the widget
has been rendered in the notebook.
"""
def __init__(self, manager):
self.supports_binary = None
self.manager = manager
self.uuid = str(uuid.uuid4())
self.html = "<div id=%r></div>" % self.uuid
def start(self):
try:
# Jupyter/IPython 4.0
from ipykernel.comm import Comm
except:
# IPython <=3.0
from IPython.kernel.comm import Comm
try:
self.comm = Comm('matplotlib', data={'id': self.uuid})
except AttributeError:
raise RuntimeError('Unable to create an IPython notebook Comm '
'instance. Are you in the IPython notebook?')
self.comm.on_msg(self.on_message)
self.comm.on_close(lambda close_message: self.manager.clearup_closed())
示例2: NbAggCommSocket
class NbAggCommSocket(CommSocket):
"""
NbAggCommSocket subclasses the matplotlib CommSocket allowing
the opening of a comms channel to be delayed until the plot
is displayed.
"""
def __init__(self, manager, target=None):
self.supports_binary = None
self.manager = manager
self.target = uuid.uuid4().hex if target is None else target
self.html = "<div id=%r></div>" % self.target
def start(self):
try:
# Jupyter/IPython 4.0
from ipykernel.comm import Comm
except:
# IPython <=3.0
from IPython.kernel.comm import Comm
try:
self.comm = Comm('matplotlib', data={'id': self.target})
except AttributeError:
raise RuntimeError('Unable to create an IPython notebook Comm '
'instance. Are you in the IPython notebook?')
self.comm.on_msg(self.on_message)
self.comm.on_close(lambda close_message: self.manager.clearup_closed())
示例3: buttonCallback
def buttonCallback(self, *args):
if len(args) > 0:
args[0].actionPerformed()
arguments = dict(target_name='beakerx.tag.run')
comm = Comm(**arguments)
msg = {'runByTag': args[0].tag}
state = {'state': msg}
comm.send(data=state, buffers=[])
示例4: _on_action_details
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=[])
示例5: exec_javascript
def exec_javascript(source, **kwargs):
"""Execute the passed javascript source inside the notebook environment.
Requires a prior call to ``init_comms()``.
"""
global exec_js_comm
if exec_js_comm is None:
from ipykernel.comm import Comm
comm = Comm("flowly_exec_js", {})
if kwargs:
source = source.format(**kwargs)
comm.send({"source": source})
示例6: JupyterComm
class JupyterComm(Comm):
"""
JupyterComm provides a Comm for the notebook which is initialized
the first time data is pushed to the frontend.
"""
template = """
<script>
function msg_handler(msg) {{
var msg = msg.content.data;
{msg_handler}
}}
if ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null)) {{
comm_manager = Jupyter.notebook.kernel.comm_manager;
comm_manager.register_target("{comm_id}", function(comm) {{ comm.on_msg(msg_handler);}});
}}
</script>
<div id="fig_{comm_id}">
{init_frame}
</div>
"""
def init(self):
from ipykernel.comm import Comm as IPyComm
if self._comm:
return
self._comm = IPyComm(target_name=self.id, data={})
self._comm.on_msg(self._handle_msg)
@classmethod
def decode(cls, msg):
"""
Decodes messages following Jupyter messaging protocol.
If JSON decoding fails data is assumed to be a regular string.
"""
return msg['content']['data']
def send(self, data=None, buffers=[]):
"""
Pushes data across comm socket.
"""
if not self._comm:
self.init()
self.comm.send(data, buffers=buffers)
示例7: connect
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)
示例8: open
def open(self):
"""Open a comm to the frontend if one isn't already open."""
if self.comm is None:
args = dict(target_name='ipython.widget',
data=self.get_state())
if self._model_id is not None:
args['comm_id'] = self._model_id
self.comm = Comm(**args)
示例9: urlArg
def urlArg(self, argName):
arguments = dict(target_name='beakerx.geturlarg')
comm = Comm(**arguments)
state = {
'name': 'URL_ARG',
'arg_name': argName
}
data = {
'state': state,
'url': self._url,
'type': 'python'
}
comm.send(data=data, buffers=[])
data = self._queue.get()
params = json.loads(data)
return params['argValue']
示例10: __init__
def __init__(self, session=None, json=None, auth=None):
self.session = session
self.id = json.get('id')
self.auth = auth
if self.session.lgn.ipython_enabled:
from ipykernel.comm import Comm
self.comm = Comm('lightning', {'id': self.id})
self.comm_handlers = {}
self.comm.on_msg(self._handle_comm_message)
示例11: __init__
def __init__(self):
"""Constructor"""
self._calls = 0
self._callbacks = {}
# Push the Javascript to the front-end.
with open(os.path.join(os.path.split(__file__)[0], 'backend_context.js'), 'r') as f:
display(Javascript(data=f.read()))
# Open communication with the front-end.
self._comm = Comm(target_name='BrowserContext')
self._comm.on_msg(self._on_msg)
示例12: _send_comm_message
def _send_comm_message(self, msg_type, content):
"""
Sends a ipykernel.Comm message to the KBaseJobs channel with the given msg_type
and content. These just get encoded into the message itself.
"""
msg = {
'msg_type': msg_type,
'content': content
}
if self._comm is None:
self._comm = Comm(target_name='KBaseJobs', data={})
self._comm.on_msg(self._handle_comm_message)
self._comm.send(msg)
示例13: open
def open(self):
"""Open a comm to the frontend if one isn't already open."""
if self.comm is None:
state, buffer_paths, buffers = _remove_buffers(self.get_state())
args = dict(target_name='jupyter.widget',
data={'state': state, 'buffer_paths': buffer_paths},
buffers=buffers,
metadata={'version': __protocol_version__}
)
if self._model_id is not None:
args['comm_id'] = self._model_id
self.comm = Comm(**args)
示例14: open
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()
示例15: start
def start(self):
try:
# Jupyter/IPython 4.0
from ipykernel.comm import Comm
except:
# IPython <=3.0
from IPython.kernel.comm import Comm
try:
self.comm = Comm('matplotlib', data={'id': self.uuid})
except AttributeError:
raise RuntimeError('Unable to create an IPython notebook Comm '
'instance. Are you in the IPython notebook?')
self.comm.on_msg(self.on_message)
self.comm.on_close(lambda close_message: self.manager.clearup_closed())