What is the equivalent of bokeh's push_notebook in holoviews? - bokeh

I can refresh a bokeh picture after updating ColumnDataSource, like this:
t = show(p, notebook_handle=True)
##### new notebook cell #####
src.data['foo'][0] = 'bar'
push_notebook(handle=t)
but, how can I refresh a holoviews picture using python?

Related

tqdm in Jupyter notebook does not show up

I would like to implement a progress bar in Jupyter notebook. I tried using tqdm as shown in the code below, but I just get this shown on the screen and the progress bar is...well...not progressing:
CODE
# Iterate through each sound file and extract the features
from tqdm import tqdm
items = len(metadata.index)
for i in tqdm(range(items), "Completed"):
for index, row in metadata.iterrows():
file_name = os.path.join(os.path.abspath(fulldatasetpath),'fold'+str(row["fold"])+'/',str(row["slice_file_name"]))
class_label = row["class_name"]
data = extract_features(file_name)
features.append([data, class_label])
Can you help me get the progress bar to progress? Thanks!
For Jupyter notebooks you should use a special version of tqdm:
from tqdm.notebook import tqdm

Holoviews charts not updating on widget selection

I copied the code below from a separate post on creating widgets that update plots based on the selection:
# import libraries
import numpy as np
import pandas as pd
import hvplot
import hvplot.pandas
import holoviews as hv
hv.extension('bokeh', logo=False)
import panel as pn
# create sample data
df = pd.DataFrame({
'col1': np.random.rand(30),
'col2': np.random.normal(size=30),
'category_col': np.random.choice(['category1', 'category2'], size=30)
})
# create widget to select category
category = pn.widgets.Select(options=['category1', 'category2'])
# function that returns a plot depending on the category selected
#pn.depends(category)
def get_plot(category):
df_selected = df[df['category_col'] == category]
plot = df_selected.hvplot.scatter(x='col1', y='col2')
return plot
# show dashboard with selection widget and dynamic plot
pn.Column(
pn.Row(category),
get_plot,
)
# get value of current selected category
category.value
However, for me the charts do not update when the selection is made. Does anyone know what is going on here? I'm trying to execute this in a Jupyter Lab notebook on my local machine, no server. I'm running the following versions of these libraries:
holoviews: 1.13.3
hvplot: 0.6.0
panel: 0.9.7
Any insight would be greatly appreciated!
Kind regards
That code works fine for me in classic notebook using the same versions (holoviews: 1.13.3 hvplot: 0.6.0 panel: 0.9.7). Have you installed the JupyterLab extension? https://panel.holoviz.org/#installation

AutocompleteInput does not complete word when option is clicked

I'm working in Jupyter notebook 5.7.4 bokeh 1.0.4, Firefox 63.0.3 python3.7, I want to get autocompletion using bokeh to get some interactive visualization.
from bokeh.models.widgets import AutocompleteInput
from IPython.display import HTML
input_widget = AutocompleteInput(completions=cList1, title='FTTH Keys', value='value')
l = layout([[input_widget],], sizing_mode='fixed')
curdoc().add_root(l)
curdoc().title = 'UI Test'
show(l)
when I write characters all is fine, but when I want to choose one element from the autocomplete list the word didn't complete in the input box.

how to make the plot, but not open a new tab in bokeh?

Here is what I do to make plots with bokeh
p1=make_plot(df)
output_file("out.html", title="out example")
show(p1)
This always open a new tab to refresh the plot, but I do not want that. It's enough to just refresh a tab with previous version of the plot.
How can I stop show() to open a new tab and just make the new plot?
I have tried the option broweser=None or broweser='' but that does not work.
Use save instead of show in the last step. This will save the plot instead of opening it in browser.
from bokeh.io import save
p1=make_plot(df)
output_file("out.html", title="out example")
save(p1)
You are looking for save(), not show()
from bokeh.io import output_file,show,save
from bokeh.plotting import figure
output_file('out.html',title='out example')
fig = figure()
show(fig) # will pop up in the browser
a = raw_input() # just press any key to continue
fig.line(range(10),range(10))
save(fig) # you can refresh your browser tab to see the change

How to change the extent and position of an existing image in bokeh?

I am displaying 2d data as images of varying shapes in a bokeh server, and therefore need to dynamically update not only the image's data source, but also its dw, dh, x, and y properties. In the dummy example below, these changes are made in a callback function which is connected to a Button widget.
I've figured out that I need to access the glyph attribute of the image's GlyphRenderer object, and I can do so through its update() method (see code). But the changes don't take effect until I click the toolbar's Reset button. I've noticed that the changes also mysteriously take effect the second time I activate the callback() function. What is the proper way to make these changes?
import bokeh.plotting
import bokeh.models
import bokeh.layouts
import numpy as np
# set up the interface
fig1 = bokeh.plotting.figure(x_range=(0, 10), y_range=(0, 10))
im1 = fig1.image([], dw=5, dh=5)
button = bokeh.models.Button(label='scramble')
# add everything to the document
bokeh.plotting.curdoc().add_root(bokeh.layouts.column(button, fig1))
# define a callback and connect it
def callback():
# this always works:
im1.data_source.data = {'image': [np.random.random((100,100))]}
# these changes only take effect after pressing the "Reset"
# button, or after triggering this callback function twice:
im1.glyph.update(x=1, y=1, dw=9, dh=9)
button.on_click(callback)
I don't immediately see why you code isn't work. I can suggest explicitly using a ColumnDataSource and linking all of the Image glyph properties to columns in that source. Then you should be able to update the source.data in a single line and have all of the updates apply.
Here's some incomplete sample code to suggest how to do that:
from bokeh.models import Image, ColumnDataSource
from bokeh.plotting import figure
# the plotting code
plot = figure()
source = ColumnDataSource(data=dict(image=[], x=[], y=[], dw=[], dh=[]))
image = Image(data='image', x='x', y='y', dw='dw', dh=dh)
plot.add_glyph(source, glyph=image)
# the callback
def callback():
source.data = {'image': [np.random.random((100,100))], 'x':[1], 'y':[1], 'dw':[9], 'dh':[9]}
button.on_click(callback)

Resources