Displays the current value at the bottom of the SelectionRangeSlider Jupyter Notebook - jupyter-notebook

I'm creating an interactive notebook by using the library ipywidgets. I'm interested in SelectionRangeSlider widget. In documentation, we have a simple example :
import datetime
dates = [datetime.date(2015, i, 1) for i in range(1, 13)]
options = [(i.strftime('%b'), i) for i in dates]
widgets.SelectionRangeSlider(
options=options,
index=(0, 11),
description='Months (2015)',
disabled=False
)
That creates folowing slider :
I would like to change the position of the current values selected, putting it at the bottom, like that:
I've read documentation but I couldn't find a way to do this. Anyone knows if it's even possible to change this position? Thanks a lot.

Others have done things like this by setting readout=False and then making a separate widget to display the updated values. See these examples:
workaround offered by marcusreaiche for SelectionRangeSlider
TheIdealis' answer to 'Custom formatting for ipywidgets IntSlider'

Related

How do I view an image in dotnet interactive?

I would like to view an image interactively in F# jupyter notebook similarly to how I can do this in python:
from PIL import Image
Image.open("image.png")
and that shows an image.
Is there a straightforward way to do this?
EmguCV, OpenCVSharp and ImageSharp all don't work together with the plotting libraries like Plotly.NET or XPlot to provide this functionality so I can't get something like matplotlib's pyplot.imshow.
In a notebook, you can display an image like this:
#!fsharp
// Load iamge
let data = File.ReadAllBytes("E:\\Downloads\\myImage.png");
// Convert so we can display it as HTML
let b64 = Convert.ToBase64String(data);
HTML($"<img src=\"data:image/png;base64,{b64}\"></img>") // last call without ; gets displayed
// Alt, this one has a semicolon:
// display(HTML($"<img src=\"data:image/png;base64,{b64}\"></img>"));

Prompt a question similar to AskYesNo, but different value

I want to display a similar looking prompt question to askYesNo() but with different values to select.
I want to be able to select with a click, not through the console, a unique value. I am trying the following:
next_step = askYesNo("Choose a country", default = TRUE,
prompts = getOption("askYesNo", gettext(c("Morocco", "Tunisia", "Cancel"))),)
But it still appears as
Is there any way to change those values to the ones that I want?
Any other function that might help with this issue, a list display can be also useful, where I can select only one value.
In Windows, R only offers a few variations on the dialog you show, and none of them allow you to put arbitrary strings on the buttons.
However, you can use menu() for more flexibility. For example,
menu(gettext(c("Morocco", "Tunisie", "Cancel")),
graphics = TRUE,
title = "Choose a country")
should display something useful for you. I'm not on Windows, so I can't show you what it will look like for you. On a Mac it looks like this:

How can I wrap Jupyter output in custom HTML?

I want to be able to take the normal output for an object and insert it into custom HTML components. Specifically, I want to allow things like putting several charts into an accordion UI element, or having hidden dataframes that are shown when a button is clicked. Is there a way to get the HTML that would normally be output, wrap it in my own HTML components, and then output that?
I have tried:
import IPython.display as dp
dp.display(dp.HTML('<div id="mycontainer">')) # Just a simple div,
# but ideally would be e.g. bootstrap component
dp.display(my_obj) # my_obj here could be a (potentially styled) dataframe
# or a plot from matplotlib/altair/etc.
dp.display(dp.HTML('</div>'))
However, the unclosed <div> just gets automatically closed, so my_obj doesn't get inserted into it. Some objects have _repr_html_(), but not all do (particularly charts). Still, Jupyter obviously has some way of extracting HTML from arbitrary objects.
It seems from trying to read the source that nbconvert is used to change the object to HTML, but I'm not sure if A) I'm understanding that right or B) how to get HTML from an arbitrary object that is not in a notebook node object (or how to construct such an object myself).
this seams to work for me :
from IPython.core.display import display, HTML
myString = 'Hello, world!'
prependHtmlTag = '<h1>'
appendHtmlTag = '</h1>'
display(HTML(prependHtmlTag + myString + appendHtmlTag )
just put any html formated string in the display(HTML([yourString]))

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!

Using Bokeh to have multiple live plots in a Jupyter notebook

I'm trying to use Bokeh to have several live plots in a Jupyter notebook. I understand that I cannot use push_notebook() since it will only update the last figure. Is there another way to do it as of today?
push_notebook updates the last cell by defatult, if you don't pass it an argument. But it also accepts a "notebook handle" that show returns when it renders a cell.
# in one cell
p = figure(**opts)
r = p2.circle([1,2,3], [4,5,6])
h = show(p)
# in a different cell
r.glyph.fill_color = "white"
push_notebook(handle=h)
See the Basic Usage example notebook in the GitHub repo.

Resources