RangeSlider unexpeted attribute Bokeh - bokeh

I am trying to use the RangeSlider within my Bokeh-application, initializing the object as so
from bokeh.models.widgets import RangeSlider
#RangeSlider
slider = RangeSlider(title="OAS slider", start=0, end=1000, value=(0,2000),
step=0.1)
When trying to compile the app I get the following error message:
AttributeError: unexpected attribute 'value' to RangeSlider, possible attributes are
callback, callback_policy, callback_throttle, css_classes, disabled, end,
height, js_callbacks, name, orientation, range, sizing_mode, start, step,
tags, title or width
I could change the code to use range instead as so
#RangeSlider altered inputs
slider = RangeSlider(title="OAS slider", start=0, end=1000, range=(0,2000),
step=0.1)
and it works.
However, as the example on the Bokeh-homepage found here
https://github.com/bokeh/bokeh/blob/master/examples/app/export_csv/main.py
used the value and works, I would be more eager to understand why I get the error message, rather than just changing to range... The example in the link also used the key word format, which obviously also generates the error above.
Could be a version issue. I am running Bokeh version 0.12.4
Thanks

This is a version issue. The RangeSlider was actually mostly broken for a long time, until we switched to a different underlying slider library to implement it. But some changes were necessary to make the switch. The correct property for 0.12.7 and newer is value and I would recommend to use that version or later if you want too use RangeSlider especially (0.12.4 is over a year old.)

Related

DataTables for R: "warning: `dim<-.vctrs_list_of()` not supported"

I'm new to DataTables but have been amazed at its power, and have managed to create a few different tables and use them in a Shiny dashboard. Currently I'm trying to implement this table, and have managed to do so after updating a bit of the code (see my own answer in the thread).
However, when inserting the final table into Shiny, like so...
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table")
),
server = function(input, output) {
output$table = DT::renderDataTable({final_table})
}
)
...I get the error DataTables warning: table id=DataTables_Table_0 - 'dim<-.vctrs_list_of()' not supported. Simply running final_table in RStudio produces the DataTable exactly as intended. Using the identically-named functions from shiny instead of DT returns a blank page.
I recognise the vctrs_list_of from one of the changes I had to make to the linked answer above (which was because tidyr's nest() these days returns a nested column of class vctrs_list_of rather than list), so I guess there's something happening there.
However, using 'inspect element' I only see an Uncaught TypeError: Cannot read property 'length' of undefined JS error, which links to some obscure line in the massive DataTables JS script. The warning is not included in DataTables' documentation.
Any leads/tips would be greatly appreciated!
I finally solved this by forcing the class of the column in question from vctrs_list_of to list, simply by running class(table$data) <- "list".
(Note: it required some changes to the JavaScript code of the linked example, since the column, now being list, had a slightly different structure.)
If anyone has a more precise explanation of what's happening here, would love to hear!

eval parse string with hyphen returning error object 'top:' not found

Seems like a lot of the similar questions asked on here do not have accepted answers and I haven't found anything that worked for me.
I'm creating a shiny app which has download buttons. on one page the download button needs to be moved down the page under all the plots.
this works for me but is hard coded:
output$saveGraphRob <- renderUI({
downloadButton("saveGraphRob1","Download", style = "margin-top:3500px")
})
what I want to do is allow for the style to be placed under all the graphs no matter how many there are. so if my plot_count() is 6 for example, instead of 3500px it should be plot_count()*350.
trying this is returning the error: object 'top:' not found
output$saveGraphRob <- renderUI({
downloadButton("saveGraphRob1","Download", style = eval(parse(text=(paste0("margin-top:",plot_count()*350))))))
})
it's trying to evaluate the hyphen in margin-top as a minus sign, but I'm not sure what the workaround is.
Also open to other solutions to change the position of my download button.
The style= parameter is just a character string. You should not be using eval(parse()) there (or almost ever with most R code). Just use style=paste0("margin-top:", plot_count()*350, "px")

How to modify the DataLabel fill (using a solid fill color)?

I'm trying to edit the data labels for a chart I'm writing on the slide. I can access the text of the datalabel using the methods available but as of yet the datalabel.fill is not existent. Any workarounds welcome if this is not planned on being added to the library in the future.
I've already gone through the source code in the github (https://github.com/scanny/python-pptx) but the datalabel class only has the font, has_text_frame, position, text_frame, _dLbl, _get_or_add_dLbl, _get_or_add_rich, _get_or_add_tx_rich, _get_or_add_txPr, and _remove_tx_rich methods. No fill or line fill methods is available.
The script I'm running does something similar for cells in a table:
cell.fill.solid()
cell.fill.fore_color.rgb = color_list[((col>0)*1)][i%2]
I'm looking at replicating the functionality on datalabels for chart series, with code that looks like this:
label.fill.solid()
label.fill.rgb = RGBColor(0x9B,0xBB,0x59)
label.fill.alpha = Alpha(.2)
label.line.fill.solid()
label.line.rgb = RGBColor(0xF0,0xF0,0x00)
The expected output xml should put the following for data labels:
<c:spPr>
<a:solidFill>
<a:srgbClr val="9BBB59">
<a:alpha val="80000"/>
</a:srgbClr>
</a:solidFill>
<a:ln>
<a:solidFill>
<a:schemeClr val="F0F000"/>
</a:solidFill>
</a:ln>
</c:spPr>
Actual output is non-existent as there is no method to do this directly.
This feature is not yet implemented, but if it was implemented it would be a .format property on the DataLabel object.
Typically users will work around an API gap like this by adding what we typically call a "workaround function" to the client code that manipulates the underlying XML directly, in this case, to add an <c:spPr> subtree in the right place.
python-pptx can generally get you close as far as a parent element is concerned. In this case, it can get you to the <c:dLbl> element like this:
data_label = chart.series[0].points[0].data_label
dLbl = data_label._dLbl
print(dLbl.xml)
The leading underscore in ._dLbl is your hint that you're getting into internals and if things don't go well it's something you're doing wrong, not an issue to be reported.
The dLbl object is an lxml.etree._Element object and can be manipulated with that API. If you have a search on "python-pptx workaround function" you'll find some examples for how to create new elements and put them where you want them.
The .xml property available on any python-pptx XML element object is handy for inspecting the results along the way. opc-diag can also be handy for inspecting PPTX files generated by PowerPoint or python-pptx for analysis or diagnostic purposes.

How to execute callback only when the Bokeh slider is released

I have a slider that affects a line in a plot:
vline = Span(location=0, dimension='height')
plot.renderers.extend([vline])
callback = CustomJS(args=dict(vline=vline), code="vline.location = slider.value;")
slider = Slider(start=-5, end=5, value=0, step=.1, callback=callback)
callback.args["slider"] = slider
I would like to, beyond changing the line, also execute an operation, call it commit_line(), via JS, that POSTs the value (and later updates another plot).
I could make the callback above call commit_line(), but that is unsuitable because it will make a couple hundred calls to the server just by sliding the slider.
In UX, this is typically addressed by executing only the expensive operation on release (of the slider). Can this be achieved in Bokeh sliders? If yes, how?
UPDATE for the current Bokeh v2.3.0 version: You should use:
JS callback:
from bokeh.models import CustomJS, Slider
from bokeh.plotting import show
slider = Slider(start=0, end=10, value=5)
slider.js_on_change('value_throttled', CustomJS(code='console.log(this.value)'))
show(slider)
Python callback:
from bokeh.models import Slider
from bokeh.plotting import curdoc
slider = Slider(start=0, end=10, value=5)
slider.on_change('value_throttled', lambda attr, old, new: print(new))
curdoc().add_root(slider)
Please note that the answer below was given for an older Bokeh version
and doesn't apply anymore for the current Bokeh v2.3.0
Pass callback_policy = "mouseup" parameter to your Slider constructor.
So:
slider = Slider(start = 1,
end = 10,
value = 1,
step = 1,
callback_policy = 'mouseup')
It comes handy when consulting Bokeh documentation to expand the JSON Prototype to find out which attributes a method actually supports, many methods are namely inherited from the base classes. Please note that JSON Prototype refers to the BokehJS model so it is not guaranteed you find all those properties in the DOM model when inspecting the code e.g. in Google Chrome Developers Tools.
In Bokeh 2.2.0, try using the "value_throttled" property:
self.date_range.on_change("value_throttled", callback)
This is working for me for a DateRangeSlider - would expect similar behaviour from other Sliders based on inheritance hierarchy.

Bokeh EditTools callback to server

I am trying to understand how to use callbacks for the new Bokeh EditTools (e.g. BoxEditTool or similar).
Specifically, I would like to see on the server side the coordinates of the newly added rectangles, but I am not sure how to do this.
I am running the following server app
def app( curdoc ):
TOOLS = "tap"
p = figure(title="Some Figure", tools=TOOLS)
source = ColumnDataSource( {'xs':[1], 'ys':[1], 'width':[.1],'height':[.1]})
r = p.rect('xs','ys','width','height', source=source)
p.add_tools(BoxEditTool( renderers = [r]))
def cb( attr, old, new ):
print(r.data_source.data)
r.data_source.on_change("selected", cb)
curdoc.add_root(column(p))
I do get printout from the cb when I select different rectangles, but the r.data_source.data does not change
Thanks for the help!
The behaviour you're describing is actually a bug in the current distribution of Bokeh (0.13.0). You can read more in the google groups discussion. To summarize, there was a problem with the synchronization of the data at the server, it has been resolved and merged.
Note that the on_change method for the Rect glyph ColumnDataSource should watch the 'data' attribute and not 'selected'.
Other than that your snippet looks good, but if you want a working example you can look here. This code is under development but at this stage it reads images and allows drawing ROIs, as well as a simple mechanism for serializing and loading them.
Hope this helps!

Resources