def _plot_figure(self, idx, fig_format='json'): """ Returns the figure in html format on the first call and """ self.plot.update(idx) if self.embed or fig_format == 'html' or bokeh_lt_011: return self.renderer.html(self.plot, fig_format) else: doc = self.plot.document if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: doc.add_root(self.plot.state) handle = _CommsHandle(get_comms(doc.last_comms_target), doc, doc.to_json()) doc.last_comms_handle = handle to_json = doc.to_json() if handle.doc is not doc: msg = dict(doc=to_json) else: msg = Document._compute_patch_between_json(handle.json, to_json) if isinstance(handle._json, dict): handle._json[doc] = to_json else: handle._json = to_json handle.comms.send(json.dumps(msg))
def _plot_figure(self, idx, fig_format='json'): """ Returns the figure in html format on the first call and """ self.plot.update(idx) if self.embed or fig_format == 'html': html = self.renderer.html(self.plot, fig_format) return html else: doc = self.plot.document if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: handle = _CommsHandle(get_comms(doc.last_comms_target), doc, doc.to_json()) doc.last_comms_handle = handle plotobjects = [ h for handles in self.plot.traverse(lambda x: x.current_handles) for h in handles ] msg = compute_static_patch(doc, plotobjects) handle.comms.send(json.dumps(msg)) return 'Complete'
def notebook_show(obj, doc, target): """ Displays bokeh output inside a notebook and returns a CommsHandle. """ publish_display_data({'text/html': notebook_div(obj, target)}) handle = _CommsHandle(get_comms(target), doc, doc.to_json()) return handle
def _plot_figure(self, idx, fig_format='json'): """ Returns the figure in html format on the first call and """ self.plot.update(idx) if self.embed or fig_format == 'html' or bokeh_lt_011: return self.renderer.html(self.plot, fig_format) else: doc = self.plot.document if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: doc.add_root(self.plot.state) handle = _CommsHandle(get_comms(doc.last_comms_target), doc, doc.to_json()) doc.last_comms_handle = handle to_json = doc.to_json() if handle.doc is not doc: msg = dict(doc=to_json) else: msg = Document._compute_patch_between_json(handle.json, to_json) if isinstance(handle._json, dict): handle._json[doc] = to_json else: handle._json = to_json handle.comms.send(json.dumps(msg)) return 'Complete'
def update(self, ranges): if not self.comms: self.comms = _CommsHandle(get_comms(self.ref), self.doc, self.doc.to_json()) self.redraw_image(ranges) to_json = self.doc.to_json() msg = Document._compute_patch_between_json(self.comms.json, to_json) self.comms._json[self.doc] = to_json self.comms.comms.send(json.dumps(msg))
def doRender(self, handlerId): clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true" if not clientHasBokeh: output_notebook(hide_banner=True) data = self.entity.getNextData() if data is None: return x = None y = None if isinstance(data, (list, np.ndarray)): x = list( range(self.windowSize) ) if self.glyphRenderer is None else self.glyphRenderer.data_source.data[ 'x'] y = data if self.glyphRenderer is None else self._concatArrays( self.glyphRenderer.data_source.data['y'], data) if len(y) < self.windowSize: y = [0] * (self.windowSize - len(y)) + y elif len(y) > self.windowSize: y = self._delWindowElements(y) elif isinstance(data, pandas.core.frame.DataFrame): pd = pd.drop(pd.index[[0]]) #pd.index = list(range(len(pd.index))) pd['x'] = list(range(len(pd.index))) else: x = data[0] y = data[1] if self.glyphRenderer is None: self.glyphRenderer = self.createGlyphRenderer(self.figure, x, y) else: self.updateGlyphRenderer(self.figure, self.glyphRenderer) if self.glyphRenderer is None: print("Error: no glyphRenderer found") return self.glyphRenderer.data_source.data['x'] = x self.glyphRenderer.data_source.data['y'] = y if not self.handleId: self.handleId = make_id() if self.figure not in _state.document.roots: _state.document.add_root(self.figure) target = notebook_div(self.figure, self.handleId) from IPython.display import display as ipythonDisplay, HTML, Javascript ipythonDisplay(HTML(target)) self.comms_handle = _CommsHandle(get_comms(self.handleId), _state.document, _state.document.to_json()) else: push_notebook(handle=self.comms_handle)
def serialize(self, models): """ Serializes any Bokeh plot objects passed to it as a list. """ documents = {plot.document for plot in self.plots} for doc in documents: if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: handle = _CommsHandle(get_comms(doc.last_comms_target), doc, None) doc.last_comms_handle = handle msg = compute_static_patch(doc, models) handle.comms.send(serialize_json(msg)) return 'Complete'
def serialize(self, models): """ Serializes any Bokeh plot objects passed to it as a list. """ documents = {plot.document for plot in self.plots} for doc in documents: json = None if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: json = doc.to_json() handle = _CommsHandle(get_comms(doc.last_comms_target), doc, json) doc.last_comms_handle = handle msg = compute_static_patch(doc, models, json) handle.comms.send(serialize_json(msg)) return 'Complete'
def update(self, ranges): """ Update the image datasource based on the new ranges, serialize the data to JSON and send to notebook via a new or existing notebook comms handle. Parameters ---------- ranges : dict(xmin=float, xmax=float, ymin=float, ymax=float, h=int, w=int) Dictionary with of x/y-ranges, width and height. """ if not self.comms_handle: self.comms_handle = _CommsHandle(get_comms(self.ref), self.doc, {}) self.update_image(ranges) msg = self.get_update_event() self.comms_handle.comms.send(msg)
def _plot_figure(self, idx, fig_format='json'): """ Returns the figure in html format on the first call and """ self.plot.update(idx) if self.embed or fig_format == 'html': html = self.renderer.html(self.plot, fig_format) return html else: doc = self.plot.document if hasattr(doc, 'last_comms_handle'): handle = doc.last_comms_handle else: handle = _CommsHandle(get_comms(doc.last_comms_target), doc, doc.to_json()) doc.last_comms_handle = handle plotobjects = [h for handles in self.plot.traverse(lambda x: x.current_handles) for h in handles] msg = compute_static_patch(doc, plotobjects) handle.comms.send(json.dumps(msg)) return 'Complete'
def doRender(self, handlerId): clientHasBokeh = self.options.get("nostore_bokeh", "false") == "true" if not clientHasBokeh: output_notebook(hide_banner=True) data = self.entity.getNextData() if data is None: return x = None y = None if isinstance(data, (list, np.ndarray)): x = list( range(self.windowSize) ) if self.glyphRenderer is None else self.glyphRenderer.data_source.data[ 'x'] y = data if self.glyphRenderer is None else self._concatArrays( self.glyphRenderer.data_source.data['y'], data) if len(y) < self.windowSize: y = [0] * (self.windowSize - len(y)) + y elif len(y) > self.windowSize: y = self._delWindowElements(y) elif isinstance(data, pandas.core.frame.DataFrame): pd = pd.drop(pd.index[[0]]) #pd.index = list(range(len(pd.index))) pd['x'] = list(range(len(pd.index))) else: x = data[0] y = data[1] if self.glyphRenderer is None: self.glyphRenderer = self.createGlyphRenderer(self.figure, x, y) else: self.updateGlyphRenderer(self.figure, self.glyphRenderer) if self.glyphRenderer is None: print("Error: no glyphRenderer found") return self.glyphRenderer.data_source.data['x'] = x self.glyphRenderer.data_source.data['y'] = y if not self.handleId: self.handleId = make_id() if self.figure not in _state.document.roots: _state.document.add_root(self.figure) activesStreamingEntities[self.getPrefix()] = self target = """ <div pd_refresh_rate="2000"> <pd_script> from pixiedust.display.streamingDisplay import * displayHandler = activesStreamingEntities["{prefix}"] displayHandler.render() </pd_script> </div> <div id="target{prefix}"> {chart} </div> """.format(chart=notebook_div(self.figure, self.handleId), prefix=self.getPrefix()) from IPython.display import display as ipythonDisplay, HTML ipythonDisplay(HTML(target)) self.comms_handle = _CommsHandle(get_comms(self.handleId), _state.document, _state.document.to_json()) else: push_notebook(handle=self.comms_handle)