holoviz/param/panel: updating Str or HTML panes via python callback - python

I would like to know if there is a way to update a panel pane content through a python callback.
If I define a Parameterized custom class the following way:
import panel as pn
pn.extension()
import param
class Myclass(param.Parameterized):
letter = param.ObjectSelector(
objects=['a', 'b', 'c', 'd'],
default='b',
)
#param.depends('letter')
def text(self):
return(pn.pane.Str(self.letter))
instance = Myclass()
If I output this instance in my notebook, I get the following:
pn.Row(instance.param.letter, instance.text)
yields:
However, when I select another entry in the dropdown list, the text on the right is not updated:
I know that the parameter has been updated, and that the text callback is fired (through debugging). Yet, no update is done in my notebook.
I feel that this example is very similar to the Sine wave example in the documentation (https://panel.holoviz.org/user_guide/Param.html), but I don't get what I am doing wrong...
Ideally, the answer should also work with a panel.pane.HTML as well as a panel.pane.Str.

I tried your code in jupyter notebook and it works: the text gets updated when the dropdown changes.
If I try it in my jupyter lab it doesn't work, but I have that more often. I think with me it's a jupyter version / installation thing.
So I think your code is correct. You could try updating your jupyter notebook or your panel or param packages.

Related

In an HTML embedding of a Jupyter widget that contains a Plotly FigureWidget, on_click callbacks don't work

Here's a minimal example. It works perfectly in a Jupyter notebook, but when the html file is opened in a browser, clicking the plot produces no output.
If embed_minimal_html is called after the widget has been used in the Jupyter notebook several times and output is present, the same output will appear in the opened html file (without the black border for some reason), but additional clicks will not produce additional output.
import plotly.graph_objects as go
import ipywidgets as widgets
from ipywidgets.embed import embed_minimal_html
fig = go.FigureWidget(data=[go.Scatter(x=[1, 2], y=[1, 2])])
scatter = fig.data[0]
output = widgets.Output(layout={'border': '1px solid black'})
#scatter.on_click
def output_point(_, points, __):
with output:
print("Point: ({}, {})".format(points.xs[0], points.ys[0]))
minimal_onclick_widget = widgets.VBox([fig, output])
embed_minimal_html('minimal_onclick_widget.html', views=[minimal_onclick_widget], title="Minimal on_click widget")
minimal_onclick_widget
Any ideas what's going on, or how to fix it?
I think it might have something to do with server-side versus client-side event handling. The link and dlink widgets go through the Python kernel (server-side) and don't work in static html embeddings like the one above, but they have client-side alternatives jslink and jsdlink that do: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#Linking-widgets-attributes-from-the-client-side. It's not stated explicitly in the docs, but maybe on_click is a server-side event with no client-side alternative.

How to hide ipywidget code in Jupyter Notebook

I am writting a jupyter notebook in which at the begining I am creating a complex tab ipywidget for the user to select some inptus (see picture). I am wondering if there is any way to hide to the user the code cell that contains the code to create that widget.
I saw on this forum some questiosn about hiding code when exporting the notebook but in my case the user will access the j_notebook online. Just would like to avoid complexity by hiding some code cells
in module.py
import ipywidgets as ipyw
from IPython.display import display
button = ipyw.Button('Try this:')
out = ipyw.Output()
def print_it(button):
with out:
print('You clicked it')
button.on_click(print_it)
display(ipyw.VBox(children=[button, out]))
In your notebook:
import module

Why is IPython.display.Image not showing in output?

I have a cell that looks like this:
from IPython.display import Image
i = Image(filename='test.png')
i
print("test")
The output is just:
test
I don't see the image in the output. I checked to see that the file exists (anyway, if it did not exist, you get an error).
Any clues?
creating the image with
i = Image(filename='test.png')
only creates the object to display. Objects are displayed by one of two actions:
a direct call to IPython.display.display(obj), e.g.
from IPython.display import display
display(i)
the displayhook, which automatically displays the result of the cell, which is to say putting i on the last line of the cell. The lone i in your example doesn't display because it is not the last thing in the cell. So while this doesn't display the image:
i = Image(filename='test.png')
i
print("test")
This would:
i = Image(filename='test.png')
print("test")
i
I had the same problem.
Matplot lib expects to show figs outside the command line, for example in the GTK or QT.
Use this: get_ipython().magic(u'matplotlib inline')
It will enable inline backend for usage with IPython notebook.
See more here and here.
After looking into the code, I can say, that under Windows, as of IPython 7.1.0, this is only supported with %matplotlib inline, which does not work under the interactive IPython shell.
There is extra code in Jupyter. The following example works with
jupyter qtconsole
jupyter notebook
jupyter console in principle, but only via external program, and then for the example the temporary file got deleted
def test_display():
import pyx
c = pyx.canvas.canvas()
circle = pyx.path.circle(0, 0, 2)
c.stroke(circle, [pyx.style.linewidth.Thick,pyx.color.rgb.red])
return c
display(test_display())

Showing plots if checkbox is checked, on python (with PyQt4)

I'm brand new to Python and I'm trying to make my first program with PyQt4. My problem is basically the following: I have two checkboxes (Plot1 and Plot2), and a "End" push button, inside my class. When I press End, I would like to see only the plots that the user checks, using matplotlib. I'm not being able to do that. My first idea was:
self.endButton.clicked.connect(self.PlotandEnd)
self.plot1Checkbox.clicked.connect(self.Plot1)
self.plot2Checkbox.clicked.conncet(self.Plot2)
def PlotandEnd(self)
plot1=self.Plot1()
pyplot.show(plot1)
plot2=self.Plot2()
pyplot.show(plot2)
def Plot1(self)
plot1=pyplot.pie([1,2,5,3,2])
return plot1
def Plot2(self)
plot2=pyplot.plot([5,3,5,8,2])
return plot2
This doesn't work, of course, because "PlotandEnd" will plot both figures, regardless of the respective checkbox. How can I do what I'm trying to?
Wrap the plot creation in an if statement that looks at the state of the check boxes. For example:
def PlotandEnd(self)
if self.plot1Checkbox.isChecked():
plot1=self.Plot1()
pyplot.show(plot1)
if self.plot2Checkbox.isChecked():
plot2=self.Plot2()
pyplot.show(plot2)
You also don't need the following lines:
self.plot1Checkbox.clicked.connect(self.Plot1)
self.plot2Checkbox.clicked.conncet(self.Plot2)
This does nothing useful at the moment! Qt never uses the return value of your PlotX() methods, and you only want things to happen when you click the End button, not when you click a checkbox. The PlotX() methods are only currently useful for your PlotandEnd() method.

Newly-assignmed variables not showing up in Spyder's variable explorer

I'm writing Python on Spyder. Please see my code below:
import pandas as pd
data = pd.io.excel.read_excel('Data.xls')
CMT_column = data['CMT']
"data" contains a column called "CMT." What I'm trying to do is create a variable called "CMT_column" that contains the values of the "CMT" column.
Here's the problem. After I run the code, only "data" appears in the variable explorer. "CMT_column" is not there. But if I call "CMT_column" in the IPython console, it shows the values of "CMT" as expected. So I guess the variable has been created after all, but why is it not visible in the variable explore?
Thanks in advance for any help.
Go to Variable explorer window.
Then you have options button right hand side.
Click on it,Untick the option Exclude all uppercase preferences as shown in image.
It seems that Spyder's variable explorer does not like variables with upper case: try rewriting CMT_ as cmt_.
I just added the below library then it works
from IPython.display import display
display()

Categories

Resources