Collection is no longer used that way in 0.11 as you discovered … I’m not sure what coverage the docs or examples have here, others will know better.
A use of Bokeh.index to get models I know of is
https://github.com/bokeh/bokeh/blob/master/examples/embed/spectrogram/spectrogram.coffee
In 0.11, ideally you would start by finding the bokeh Document instance you care about. It’s reachable from any model in the document as model.document - if you’re in a JS callback, pass any model to the callback then get model.document. If you’re in an arbitrary tag, I don’t remember a clean, supported way to do this (or even to get the Bokeh object, which may load later than your JS in some situations). I’ll suggest some hacks later though.
Once you have the document, the best approach is on the Python side set mydatasource.name=“something”, then in JS do doc.get_model_by_name(“something”). If you instead have the model id there is a get_model_by_id(). Or if you know the document structure you could dig through the document starting from doc.roots().
Hacks to get a document -
There isn’t a global var containing all documents or models anymore, but there is still a Bokeh.index which contains views for all document roots I think, plus I think models that were explicitly passed to show() or file_html()… The views have a model field and models have a document field so as a hack you can get the document that way. Pick any key from Bokeh.index, get its value, get its model field, get the model’s document field.
Another possible hack is to trigger a client-side callback that doesn’t do anything except get passed a model, and then in the callback set a global like window.bokeh_doc to model.document. Then other code can get the document from there. I’m not sure of the easiest way to do a fake self-triggered callback though. Maybe an invisible button. Very bad hack.
Either way I’d suggest use the hack to get Document and then use the Document API to get all the specific models.
The BEST hack of course is to make a PR for a future release adding things like Bokeh.get_document_by_session_id() or Bokeh.get_document_by_title() or Bokeh.get_rendered_documents().
Tricky bits might include:
-
if a document is no longer rendered or used in an open session, be sure to forget it to avoid a leak
-
multiple document instances can have the same session id if multiple websockets are open
-
document.title can change at any time
There may be other better PR ideas others have. A very simple one could be to be able to add a “Document created” callback, perhaps, and you could set global vars from that callback.
Anyway apologies for the rough edge.
Havoc
···
On Jan 20, 2016, at 3:48 PM, Greg Nordin [email protected] wrote:
I am trying to do something similar where I would like to change the data in a ColumnDataSource using javascript, but I’m having a hard time locating the ColumnDataSource. I generated the attached html file from the attached .py file. When I follow the steps below looking in the html file, I find Bokeh.ColumnDataSource.Collection.models, but it has nothing in it, i.e.
console.log(Bokeh.ColumnDataSource.Collection.models);
returns an empty list:
[]
Has something changed since August? If so, where can I find documentation or examples?
Thanks,
Greg
On Sunday, August 23, 2015 at 4:16:35 AM UTC-6, Pythonic wrote:
Solved:
In summary
The columndatasource is in window.Bokeh.ColumnDataSource.Collection.models[k].attributes.data, with [k] being the key to the datasource you want
The code
This shows how to access it, presuming you provided a name to the ColumnDataSource when you created it in Python (e.g. source = models.ColumnDataSource(data_dict, name=‘mycolumndatasource’) )
console.log(Bokeh);
var my_source_name = 'mycolumndatasource';
var sources = Bokeh.ColumnDataSource.Collection.models;
var source_keys = Bokeh._.keys(sources)
console.log(source_keys)
for (i = 0; i < source_keys.length; i++) {
var k = source_keys[i];
var source_name = sources[k].[attributes.name](http://attributes.name)
if (source_name == my_source_name) {
var my_data = sources[k].attributes.data
}
}
console.log(my_data);
Comment
- Being able to access the data sources from external js is extremely useful to do hybrid Python / Javascript development, as one can rely on Bokeh’s interconnection while using external functionalities
- Exploring window.Bokeh, I thought all the objects with capital first letter like ColumnDataSource were constructors, did not expect them to contain active collections and models
On Monday, 17 August 2015 16:06:26 UTC+1, Pythonic wrote:
Here is where I am:
_ = Bokeh._
index = window.Bokeh.index
keys = _.keys(index)
plot = Bokeh.index[keys[0]] // index only has 1 key
renderers = plot.model.get(‘renderers’)
console.log(renderers) // this results in “undefined”
Manually exploring the plot object in the log, I find renderers in plot.views.attributes.renderers**,** but none respond to get(‘data_source’) or show any data_source attribute in the console log
I am sure I must be doing some silly error along the line, any suggestion? Many thanks
On Monday, 17 August 2015 15:27:53 UTC+1, Pythonic wrote:
Priceless, many thanks
On Monday, 17 August 2015 15:23:30 UTC+1, Bryan Van de ven wrote:
Hi Giuseppe,
Yes, I forgot the index only contains Plot objects. But each of these has a “renderers” backbone attribute, so you can use
plot.get('renderers')
to get the list of renderers. One (or more) of these will be a GlyphRenderer, and these are the objects that have a “data_source” backbone attribute:
renderer.get('data_source')
You can update and trigger this object just as is done in the User Guide examples.
Adding some JS object graph search functions (like the python side has) is on our long TODO list.
Bryan
On Aug 17, 2015, at 1:31 AM, Pythonic [email protected] wrote:
Broadening this question, for mixed Python+JS development it might be useful to understand where to access the Backbone model of the various objects in a page
Two example of reasons to do this:
- re-rendering objects: for example, if I change the value of a Slider through the callback of another bokeh widget, the slider does not render to the new value and (I presume) I need to access its renderer and give it a nudge
- modifying data (columndatasources) from JS scripts that take place outside of a Bokeh Callback
By starting at bokeh.index, where should one look for the data sources and the models? [el, model, model.attributes, model.collections,…?]
–
You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/2ac5d5dc-a2f8-4f88-b842-2958e310448c%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
–
You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/75bba408-73bf-45a5-bac6-4bba998b8f99%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
<sinusoidal_tline_voltages_test.html>
<sinusoidal_tline_voltages.py>