Iām trying to figure out if itās possible to create a PlotLy Pie Chart in python with an on_click event in JupyterLab. Iāve been able to run the scatter plot example:
When I try to adapt this for a pie chart, though, the callback doesnāt look like itās ever being called. Iām not sure if this is possible and thereās something missing in my adaptation, or if itās a capability thatās only available for scatter and not for other kinds of plots.
Can anyone advise or provide feedback on this? Thanks!
Thanks! Yes, that works for me as well. And seeing a successful example allowed me to identify what what I was doing to adapt the scatterplot example that wasnāt working.
I noticed two things that donāt work following the scatterplot example (that is, it will create plots, but the callback wonāt fire on click):
iplot wonāt work for a callback-based interactive pie chart (or at least, it doesnāt when I change the example only to use iplot rather than VBox).
it wonāt work if I create the āpieā object with go.Pie instead of with fig.add_pie (as in: fig = go.FigureWidget([go.Pie(labels=labels, values=values)])).
I can move forward now with a working example, but Iām curious to understand these differences a bit better. Can someone help to explain the reason(s) for them? Iām new to using PlotLy, so it may just be that this isnāt clear to me because of my lack of familiarity with the programming model. Thanks!
iplot wonāt work for a callback-based interactive pie chart (or at least, it doesnāt when I change the example only to use iplot rather than VBox).
This is correct, when a FigureWidget is displayed using iplot, the resulting view is not an ipywidget and so the events canāt be synced back to the Python side. A FigureWidget should be allowed to display itself (by being the last expression in an input cell), or it can be displayed using the ipywidgets.display method (See Simple Widget Introduction ā Jupyter Widgets 8.1.1 documentation).
it wonāt work if I create the āpieā object with go.Pie instead of with fig.add_pie (as in: fig = go.FigureWidget([go.Pie(labels=labels, values=values)])).
Whatās happening here is that traces are copied when they are passed to figure constructors. So you do need to add a trace to the figure and then access it in order for the callbacks to be synced up. As an alternative to using the add_* methods, you can retrieve the trace from the figureās data property. E.g.
fig = go.FigureWidget([go.Pie(labels=labels, values=values)]))
pie = fig.data[0]
out = Output() @out.capture(clear_output=True)
def update_point(trace, points, selector):
c = list(scatter.marker.color)
s = list(scatter.marker.size)
for i in points.point_inds:
c[i] = ā#bae2beā
s[i] = 20
with f.batch_update():
scatter.marker.color = c
scatter.marker.size = s
webbrowser.open(āhttp://example.comā)
I canāt get the minimal example working, and I wonder if itās because something broke with plotply-ipywidget integration or if itās a configuration issue with my notebook setup (e.g. a missing extension). When I run this minimal example based on the pie chart example, it doesnāt render anything (neither the ipywidget nor the figure):
import ipywidgets as widgets
import plotly.graph_objects as go
fig = go.FigureWidget()
fig.add_pie(values=[1, 2, 3])
out = widgets.HTML()
out.value = 'hello'
widgets.VBox([out, fig])
I can render without including a plotly widget., e.g.:
Thanks for the reply, Nicolas! Unfortunately this is in a shared development environment where I may not be able to upgrade plotly in short order, but I may have more luck adding extensions.
Iām using a notebook platform based on Jupyter but not JupyterLab, and my understanding from the getting started guide is the plotlywidget extension is just for JupyterLab. Are there installation steps missing from the Jupyter Notebook Support section? https://plotly.com/python/getting-started/#jupyter-notebook-support
I notice in that section there isnāt a figure rendering below this code block, which is the same use case Iām having trouble with ā is it exhibiting the same problem?
import plotly.graph_objects as go
fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1]))
fig
So in Jupyter, the plotlywidget extension is loaded automatically, and in JupyterLab it must be installed manually. If youāre in a custom notebook platform, youāll need to get the extension loaded in one way or the other to get widgets to work
Can you provide more details on your notebook system? Is it Google Colab or SageMaker or something like that or from some smaller vendor?
Thanks so much, Nicholas. Itās a notebook platform built internally at my company, and I donāt know enough about its creation and development to be able to say more about how it differs. Theyāre now looking at getting plotlywidget to load properly.
It may take some time for them to get it working, and I need a temporary work-around to be able to do the demo I need (or switch to ggplot or matplotlib). Is there any way, even if very hacky, to capture click events on a figure rendered using show()? Thereās clearly some mouse-to-figure communication going on because hovertext works. I would be grateful for a hint of where to hook into the code base with the understanding that Iām on my own from there.