def _bulkget(docid, typename=None): clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) all_models = clientdoc._models.values() if typename is not None: attrs = clientdoc.dump(*[x for x in all_models \ if x.__view_model__==typename]) attrs = [x['attributes'] for x in attrs] return make_json(protocol.serialize_json(attrs)) else: attrs = clientdoc.dump(*all_models) return make_json(protocol.serialize_json(attrs))
def get_data(self, transform): json = protocol.serialize_json(transform) response = requests.get(self._url(), data=json) self._is_ok(response) data = response.json() self.metadata = data.pop('metadata', {}) return data
def __call__(self, obj, fmt=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ plot, fmt = self._validate(obj, fmt) info = {'file-ext': fmt, 'mime_type': MIME_TYPES[fmt]} if isinstance(plot, tuple(self.widgets.values())): return plot(), info elif fmt == 'html': html = self.figure_data(plot) html = '<center>%s</center>' % html return self._apply_post_render_hooks(html, obj, fmt), info elif fmt == 'json': plotobjects = [ h for handles in plot.traverse(lambda x: x.current_handles) for h in handles ] data = dict(data=[]) if not bokeh_lt_011: data['root'] = plot.state._id data['data'] = models_to_json(plotobjects) return self._apply_post_render_hooks(serialize_json(data), obj, fmt), info
def fields(self): json = protocol.serialize_json({}) response = requests.get(self._url("fields"), data=json) self._is_ok(response) data = response.json() self._trigger_events() return data
def _get_bokeh_info(request, docid): doc = docs.Doc.load(request.registry.servermodel_storage, docid) bokehuser = request.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction( request, bokehuser, doc, 'r', temporary_docid=temporary_docid ) t.load() clientdoc = t.clientdoc all_models = clientdoc._models.values() log.info("num models: %s", len(all_models)) all_models = clientdoc.dump(*all_models) returnval = {'plot_context_ref' : doc.plot_context_ref, 'docid' : docid, 'all_models' : all_models, 'apikey' : t.apikey} returnval = protocol.serialize_json(returnval) #i don't think we need to set the header here... result = make_json(returnval, headers={"Access-Control-Allow-Origin": "*"}) return result
def update(docid, typename, id): """we need to distinguish between writing and patching models namely in writing, we shouldn't remove unspecified attrs (we currently don't handle this correctly) """ doc = docs.Doc.load(bokeh_app.servermodel_storage, docid) bokehuser = bokeh_app.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction(bokehuser, doc, 'rw', temporary_docid=temporary_docid) t.load() modeldata = protocol.deserialize_json(request.data.decode('utf-8')) ### horrible hack, we need to pop off the noop object if it exists modeldata.pop('noop', None) clientdoc = t.clientdoc log.info("loading done %s", len(clientdoc._models.values())) # patch id is not passed... modeldata['id'] = id modeldata = {'type': typename, 'attributes': modeldata} clientdoc.load(modeldata, events='existing', dirty=True) t.save() ws_update(clientdoc, t.write_docid, t.changed) # backbone expects us to send back attrs of this model, but it doesn't # make sense to do so because we modify other models, and we want this to # all go out over the websocket channel return make_json(protocol.serialize_json({'noop': True}))
def push_notebook(self): """ Update date for a plot in the IPthon notebook in place. This function can be be used to update data in plot data sources in the IPython notebook, without having to use the Bokeh server. Returns: None .. warning:: The current implementation leaks memory in the IPython notebook, due to accumulating JS code. This function typically works well with light UI interactions, but should not be used for continuously updating data. See :bokeh-issue:`1732` for more details and to track progress on potential fixes. """ from IPython.core import display from bokeh.protocol import serialize_json id = self.ref['id'] model = self.ref['type'] json = serialize_json(self.vm_serialize()) js = """ var ds = Bokeh.Collections('{model}').get('{id}'); var data = {json}; ds.set(data); """.format(model=model, id=id, json=json) display.display_javascript(js, raw=True)
def get_data(username, docid, datasourceid): bokehuser = bokeh_app.authentication.current_user() request_username = bokehuser.username # handle docid later... clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) init_bokeh(clientdoc) serverdatasource = clientdoc._models[datasourceid] parameters = json.loads(request.values.get('resample_parameters')) plot_state = json.loads(request.values.get('plot_state')) render_state = json.loads(request.values.get('render_state')) if 'render_state' in request.values else None # TODO: Desserializing directly to ranges....awk-ward. # There is probably a better way via the properties system that detects type...probably... # Possibly pass the whole plot_view object through instead of just the fragments we get with this mechanism plot_state=dict([(k, _make_range(r)) for k,r in iteritems(plot_state)]) result = bokeh_app.datamanager.get_data( request_username, serverdatasource, parameters, plot_state, render_state) result = make_json(protocol.serialize_json(result)) return result
def bokeh_data(): """ Export bokeh json from request.data Rows are tabs Pane's and rows versus column """ tab = [] plot_data = json.loads( request.data ) plt = [] for tab_name in plot_data: figure_data = plot_data[tab_name] plt.append( figure(**figure_data['figure'])) glyphs = figure_data['glyphs'] for glyph in glyphs: tmp = list(glyph.values())[0] tmp['source'] = source getattr( plt[-1], list(glyph.keys())[0] )(**tmp) tab.append( Panel(child=plt[-1],title=tab_name)) tabs = Tabs(tabs=tab) return json.dumps({ 'all_models': serialize_json( tabs.dump() ), 'ref': tabs.ref, })
def __call__(self, obj, fmt=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ # Example of the return format where the first value is the rendered data. plot, fmt = self._validate(obj, fmt) if fmt == 'html': html = self.figure_data(plot) html = '<center>%s</center>' % html return html, {'file-ext': fmt, 'mime_type': MIME_TYPES[fmt]} elif fmt == 'json': plotobjects = [ h for handles in plot.traverse(lambda x: x.current_handles) for h in handles ] data = OrderedDict() for plotobj in plotobjects: json = plotobj.vm_serialize(changed_only=True) data[plotobj.ref['id']] = { 'type': plotobj.ref['type'], 'data': json } return serialize_json(data), { 'file-ext': json, 'mime_type': MIME_TYPES[fmt] }
def __call__(self, obj, fmt=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ plot, fmt = self._validate(obj, fmt) widgets = list(self.widgets.keys())+['auto'] if fmt in widgets: return self.get_widget(plot, fmt)(), {'file-ext':' html', 'mime_type': MIME_TYPES['html']} elif fmt == 'html': html = self.figure_data(plot) html = '<center>%s</center>' % html return html, {'file-ext':fmt, 'mime_type':MIME_TYPES[fmt]} elif fmt == 'json': plotobjects = [h for handles in plot.traverse(lambda x: x.current_handles) for h in handles] data = OrderedDict() for plotobj in plotobjects: if old_bokeh: json = plotobj.vm_serialize(changed_only=True) else: json = plotobj.to_json(False) data[plotobj.ref['id']] = {'type': plotobj.ref['type'], 'data': json} return serialize_json(data), {'file-ext': 'json', 'mime_type':MIME_TYPES[fmt]}
def bokeh_to_data(): bk_plot = parse_bokeh_manifest_with_loader(request.data.decode('utf-8')) print(bk_plot) return jsonify( all_models=serialize_json(bk_plot.dump()), ref=bk_plot.ref, )
def delete(docid, typename, id): clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) model = clientdoc._models[id] clientdoc.del_obj(docid, model) ws_delete(clientdoc, [model]) return make_json(protocol.serialize_json(clientdoc.dump(model)[0]['attributes']))
def fields(varname): df = namespace[varname] fields = [ dict(name=column, dtype=dtype.name) for (column, dtype) in zip(df.columns, df.dtypes) ] return make_json(protocol.serialize_json(fields))
def update(request, docid, typename, id): """we need to distinguish between writing and patching models namely in writing, we shouldn't remove unspecified attrs (we currently don't handle this correctly) """ doc = docs.Doc.load(request.registry.servermodel_storage, docid) bokehuser = request.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction( request, bokehuser, doc, 'rw', temporary_docid=temporary_docid ) t.load() modeldata = protocol.deserialize_json(request.body.decode('utf-8')) ### horrible hack, we need to pop off the noop object if it exists modeldata.pop('noop', None) clientdoc = t.clientdoc log.info("loading done %s", len(clientdoc._models.values())) # patch id is not passed... modeldata['id'] = id modeldata = {'type' : typename, 'attributes' : modeldata} clientdoc.load(modeldata, events='existing', dirty=True) t.save() ws_update(request, clientdoc, t.write_docid, t.changed) # backbone expects us to send back attrs of this model, but it doesn't # make sense to do so because we modify other models, and we want this to # all go out over the websocket channel return make_json(protocol.serialize_json({'noop' : True}))
def get_data(username, docid, datasourceid): bokehuser = bokeh_app.authentication.current_user() request_username = bokehuser.username # handle docid later... clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) init_bokeh(clientdoc) serverdatasource = clientdoc._models[datasourceid] parameters = json.loads(request.values.get('resample_parameters')) plot_state = json.loads(request.values.get('plot_state')) render_state = json.loads(request.values.get( 'render_state')) if 'render_state' in request.values else None # TODO: Desserializing directly to ranges....awk-ward. # There is probably a better way via the properties system that detects type...probably... # Possibly pass the whole plot_view object through instead of just the fragments we get with this mechanism plot_state = dict([(k, _make_range(r)) for k, r in iteritems(plot_state)]) result = bokeh_app.datamanager.get_data(request_username, serverdatasource, parameters, plot_state, render_state) result = make_json(protocol.serialize_json(result)) return result
def pivot(self, transform): json = protocol.serialize_json(transform) response = requests.post(self._url("pivot"), data=json) self._is_ok(response) data = response.json() self._trigger_events() return data
def update(docid, typename, id): """we need to distinguish between writing and patching models namely in writing, we shouldn't remove unspecified attrs (we currently don't handle this correctly) """ clientdoc = bokeh_app.backbone_storage.get_document(docid) log.info("loading done %s", len(clientdoc._models.values())) prune(clientdoc) init_bokeh(clientdoc) log.info("updating") modeldata = protocol.deserialize_json(request.data.decode('utf-8')) # patch id is not passed... modeldata['id'] = id modeldata = {'type' : typename, 'attributes' : modeldata} clientdoc.load(modeldata, events='existing', dirty=True) log.info("done") log.info("saving") changed = bokeh_app.backbone_storage.store_document(clientdoc) log.debug("changed, %s", str(changed)) ws_update(clientdoc, changed) log.debug("update, %s, %s", docid, typename) # backbone expects us to send back attrs of this model, but it doesn't # make sense to do so because we modify other models, and we want this to # all go out over the websocket channel return make_json(protocol.serialize_json({'noop' : True}))
def ws_update(clientdoc, models): attrs = clientdoc.dump(*models) msg = protocol.serialize_json({'msgtype' : 'modelpush', 'modelspecs' : attrs }) bokeh_app.publisher.send("bokehplot:" + clientdoc.docid, msg) return msg
def pivot(varname): if request.data: options = json.loads(request.data) else: options = {} rows = options.get("rows", []) columns = options.get("columns", []) values = options.get("values", []) filters = options.get("filters", []) def fields(items): return [ item["field"] for item in items ] row_fields = fields(rows) column_fields = fields(columns) value_fields = fields(values) filter_fields = fields(filters) if len(values) > 0: aggfunc = values[0]["aggregate"] else: aggfunc = len _, (_attrs, _rows, _cols, _values) = _pivot_table(namespace[varname], row_fields, column_fields, value_fields, aggfunc) result = { "attrs": _attrs, "rows": _rows, "cols": _cols, "values": _values, } return make_json(protocol.serialize_json(result))
def __call__(self, obj, fmt=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ plot, fmt = self._validate(obj, fmt) info = {"file-ext": fmt, "mime_type": MIME_TYPES[fmt]} if isinstance(plot, tuple(self.widgets.values())): return plot(), info elif fmt == "html": html = self.figure_data(plot) html = "<center>%s</center>" % html return html, info elif fmt == "json": plotobjects = [h for handles in plot.traverse(lambda x: x.current_handles) for h in handles] data = dict(data=[]) ids = [] if not old_bokeh: data["root"] = plot.state._id json_data = [] for plotobj in plotobjects: if plotobj.ref["id"] in ids: continue else: ids.append(plotobj.ref["id"]) if old_bokeh: json = plotobj.vm_serialize(changed_only=True) else: json = plotobj.to_json(False) json_data.append({"id": plotobj.ref["id"], "type": plotobj.ref["type"], "data": json}) data["data"] = json_data return serialize_json(data), info
def create(request): ''' Update or insert new objects for a given :class:`Document <bokeh.document.Document>`. :param docid: id of the :class:`Document <bokeh.document.Document>` to update or insert into :status 200: when user is authorized :status 401: when user is not authorized ''' docid, typename = request.POST['docid'], request.POST['typename'] doc = docs.Doc.load(request.registry.servermodel_storage, docid) bokehuser = request.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction( request, bokehuser, doc, 'rw', temporary_docid=temporary_docid ) t.load() modeldata = protocol.deserialize_json(request.body.decode('utf-8')) modeldata = [{'type' : typename, 'attributes' : modeldata}] t.clientdoc.load(*modeldata, dirty=True) t.save() ws_update(request, t.clientdoc, t.write_docid, modeldata) return make_json(protocol.serialize_json(modeldata[0]['attributes']))
def ws_update(request, clientdoc, docid, models): log.debug("sending wsupdate to %s", docid) attrs = clientdoc.dump(*models) msg = protocol.serialize_json({'msgtype' : 'modelpush', 'modelspecs' : attrs }) request.registry.publisher.send("bokehplot:" + docid, msg) return msg
def set_computed_columns(self, computed_columns): json = protocol.serialize_json(computed_columns) response = requests.get(self._url("computed"), data=json) self._is_ok(response) data = response.json() self.computed_columns = computed_columns self.data += 1 return data
def __init__(self, plot_object, elementid=None, min_width=700): if not elementid: elementid = str(uuid.uuid4()) self.elementid = elementid self.min_width = min_width self.modelid = plot_object.ref["id"] self.modeltype = plot_object.ref["type"] self.all_models = serialize_json(plot_object.dump())
def _bulkget(request, docid, typename=None): doc = docs.Doc.load(request.registry.servermodel_storage, docid) bokehuser = request.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction( request, bokehuser, doc, 'r', temporary_docid=temporary_docid ) t.load() clientdoc = t.clientdoc all_models = clientdoc._models.values() if typename is not None: attrs = clientdoc.dump(*[x for x in all_models \ if x.__view_model__==typename]) attrs = [x['attributes'] for x in attrs] return make_json(protocol.serialize_json(attrs)) else: attrs = clientdoc.dump(*all_models) return make_json(protocol.serialize_json(attrs))
def serialize(self, objects): """ Serializes any Bokeh plot objects passed to it as a list. """ data = dict(data=models_to_json(objects)) if not bokeh_lt_011: plot = self.plots[0] data['root'] = plot.state._id return serialize_json(data)
def connect(sock, addr, topic, auth): sock.timeout = 2.0 sock.connect(addr) msgobj = dict(msgtype='subscribe', topic=topic, auth=auth) sock.send(protocol.serialize_json(msgobj)) msg = sock.recv() msg = msg.split(":", 2)[-1] msgobj = protocol.deserialize_json(msg) assert msgobj['status'][:2] == ['subscribesuccess', topic]
def ws_update(clientdoc, docid, models): log.debug("sending wsupdate to %s", docid) attrs = clientdoc.dump(*models) msg = protocol.serialize_json({ 'msgtype': 'modelpush', 'modelspecs': attrs }) bokeh_app.publisher.send("bokehplot:" + docid, msg) return msg
def ws_delete(clientdoc, models): attrs = clientdoc.dump(*models) msg = { 'msgtype' : 'modeldel', 'modelspecs' : attrs, } msg = protocol.serialize_json(msg) bokeh_app.wsmanager.send("bokehplot:" + clientdoc.docid, msg) return msg
def ws_delete(request, clientdoc, docid, models): attrs = clientdoc.dump(*models) msg = { 'msgtype' : 'modeldel', 'modelspecs' : attrs, } msg = protocol.serialize_json(msg) request.registry.wsmanager.send("bokehplot:" + docid, msg) return msg
def serialize(self, objects): """ Serializes any Bokeh plot objects passed to it as a list. """ data = dict(data=models_to_json(objects)) if not bokeh_lt_011: plot = self.plots[0] data["root"] = plot.state._id return serialize_json(data)
def create(docid, typename): clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) modeldata = protocol.deserialize_json(request.data.decode('utf-8')) modeldata = {'type' : typename, 'attributes' : modeldata} clientdoc.load(modeldata, dirty=True) bokeh_app.backbone_storage.store_document(clientdoc) ws_update(clientdoc, modeldata) return protocol.serialize_json(modeldata[0]['attributes'])
def get(varname): if request.data: transforms = json.loads(request.data) else: transforms = {} groupobj, data, maxlength, totallength = get_data(varname, transforms) data = jsonify(data) data['maxlength'] = maxlength data['totallength'] = totallength return make_json(protocol.serialize_json(data))
def _bulkget(docid, typename=None): doc = docs.Doc.load(bokeh_app.servermodel_storage, docid) bokehuser = bokeh_app.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction(bokehuser, doc, 'r', temporary_docid=temporary_docid) t.load() clientdoc = t.clientdoc all_models = clientdoc._models.values() if typename is not None: attrs = clientdoc.dump(*[x for x in all_models \ if x.__view_model__==typename]) attrs = [x['attributes'] for x in attrs] return make_json(protocol.serialize_json(attrs)) else: attrs = clientdoc.dump(*all_models) return make_json(protocol.serialize_json(attrs))
def callbacks(docid): #broken... clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) if request.method == 'POST': jsondata = protocol.deserialize_json(request.data.decode('utf-8')) bokeh_app.backbone_storage.push_callbacks(jsondata) else: jsondata = bokeh_app.backbone_storage.load_callbacks() return make_json(protocol.serialize_json(jsondata))
def getbyid(request, docid, typename, id): doc = docs.Doc.load(request.registry.servermodel_storage, docid) bokehuser = request.registry.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction( request, bokehuser, doc, 'r', temporary_docid=temporary_docid ) t.load() clientdoc = t.clientdoc attr = clientdoc.dump(clientdoc._models[id])[0]['attributes'] return make_json(protocol.serialize_json(attr))
def push(self, docid, *jsonobjs): keys = [modelkey(attr['type'], docid, attr['attributes']['id']) for attr in jsonobjs] for attr in jsonobjs: attr['attributes']['doc'] = docid attrs = [protocol.serialize_json(attr['attributes']) for attr in jsonobjs] dkey = dockey(docid) data = dict(zip(keys, attrs)) self.mset(data) self.sadd(dkey, *keys)
def connect(sock, addr, topic, auth): sock.timeout = 2.0 sock.connect(addr) msgobj = dict(msgtype='subscribe', topic=topic, auth=auth ) sock.send(protocol.serialize_json(msgobj)) msg = sock.recv() msg = msg.split(":", 2)[-1] msgobj = protocol.deserialize_json(msg) assert msgobj['status'][:2] == ['subscribesuccess', topic]
def getbyid(docid, typename, id): doc = docs.Doc.load(bokeh_app.servermodel_storage, docid) bokehuser = bokeh_app.current_user() temporary_docid = get_temporary_docid(request, docid) t = BokehServerTransaction(bokehuser, doc, 'r', temporary_docid=temporary_docid) t.load() clientdoc = t.clientdoc attr = clientdoc.dump(clientdoc._models[id])[0]['attributes'] return make_json(protocol.serialize_json(attr))
def push_notebook(self): from IPython.core import display from bokeh.protocol import serialize_json id = self.ref['id'] model = self.ref['type'] json = serialize_json(self.vm_serialize()) js = """ var ds = Bokeh.Collections('{model}').get('{id}'); var data = {json}; ds.set(data); """.format(model=model, id=id, json=json) display.display_javascript(js, raw=True)
def connect(sock, addr, topic, auth): # TODO (bev) increasing timeout due to failing TravisCI tests # investigate if this is the real solution or if there is a # deeper problem sock.timeout = 4.0 sock.connect(addr) msgobj = dict(msgtype='subscribe', topic=topic, auth=auth) sock.send(protocol.serialize_json(msgobj)) msg = sock.recv() msg = msg.split(":", 2)[-1] msgobj = protocol.deserialize_json(msg) assert msgobj['status'][:2] == ['subscribesuccess', topic]
def rpc(docid, typename, id, funcname): clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) model = clientdoc._models[id] data = protocol.deserialize_json(request.data.decode('utf-8')) args = data.get('args', []) kwargs = data.get('kwargs', {}) result = getattr(model, funcname)(*args, **kwargs) log.debug("rpc, %s, %s", docid, typename) changed = bokeh_app.backbone_storage.store_document(clientdoc) ws_update(clientdoc, changed) return make_json(protocol.serialize_json(result))
def push(self, docid, *jsonobjs): keys = [ modelkey(attr['type'], docid, attr['attributes']['id']) for attr in jsonobjs ] for attr in jsonobjs: attr['attributes']['doc'] = docid attrs = [ protocol.serialize_json(attr['attributes']) for attr in jsonobjs ] dkey = dockey(docid) data = dict(zip(keys, attrs)) self.mset(data) self.sadd(dkey, *keys)
def callbacks_get(docid): ''' Retrieve callbacks for a given :class:`Document <bokeh.document.Document>`. :param docid: id of the :class:`Document <bokeh.document.Document>` to get callbacks for :status 200: when user is authorized :status 401: when user is not authorized ''' # broken... clientdoc = bokeh_app.backbone_storage.get_document(docid) prune(clientdoc) jsondata = bokeh_app.backbone_storage.load_callbacks() return make_json(protocol.serialize_json(jsondata))
def test_merge(self): d1 = document.Document() d2 = document.Document() p1 = circle([1], [2]) p2 = circle([1], [2]) d1.add(p1) d2.add(p2) json_objs = d1.dump() json_objs = protocol.deserialize_json(protocol.serialize_json(json_objs)) d2.merge(json_objs) assert d2.context._id == d1.context._id assert len(d2.context.children) == 2 assert d2.context is d2._models[d2.context._id] pcs = [x for x in d2._models.values() if x.__view_model__ == "PlotContext"] assert len(pcs) == 1
def on_message(self, message): msgobj = protocol.deserialize_json(message) msgtype = msgobj.get('msgtype') if msgtype == 'subscribe': auth = msgobj['auth'] topic = msgobj['topic'] if self.manager.auth(auth, topic): self.manager.subscribe(self.clientid, topic) msg = protocol.serialize_json( protocol.status_obj(['subscribesuccess', topic, self.clientid]) ) self.write_message(topic + ":" + msg) else: msg = protocol.serialize_web(protocol.error_obj('unauthorized')) self.write_message(topic + ":" + msg)