# How to listen events on modifying a shape

**URL:** <https://community.plotly.com/t/how-to-listen-events-on-modifying-a-shape/41977>\
**Category:** 📊 Plotly Python\
**Created:** [July 3, 2020, 8:13pm UTC](https://community.plotly.com/t/how-to-listen-events-on-modifying-a-shape/41977 "2020-07-03T20:13:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaladh](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@jaladh](https://community.plotly.com/u/jaladh)\
**Post date:** [July 3, 2020, 8:13pm UTC](https://community.plotly.com/t/how-to-listen-events-on-modifying-a-shape/41977/1 "2020-07-03T20:13:42Z")

</div>

[Drawing shapes on Cartesian Plot](https://plotly.com/python/shapes/#drawing-shapes-on-cartesian-plots) section of docs, state that:

> Drawing or modifying a shape triggers a `relayout` event, which [can be captured by a callback inside a Dash application](https://dash.plotly.com/interactive-graphing).

But I’m unable to figure out how to do the same in plotly python within jupyter notebook (because I believe dash is also using the callbacks just named differently). The code I used was:

```auto
fig = go.FigureWidget(go.Scatter(x=[1,2,3], y=[4,5,6]))
fig.add_shape(
    editable=True,
    x0=0, x1=2, y0=0, y1=3,
    xref='x1', yref='y1'
)
fig.update_layout(dragmode='drawrect')
shp = fig.layout.shapes[0]

fig.show(config={'modeBarButtonsToAdd':[
                                        'drawrect',
                                        'eraseshape'
                                       ]})

```

In another cell, I’m trying to capture the x-axis range to which rectangle span (i.e. its width):

```auto
out = ipywidgets.Output()

@out.capture()
def shape_handler(shp_obj, x0, x1):
    print(x0, x1)

shp.on_change(shape_handler, 'x0', 'x1', append=True)

```

But nothing is printed. Also I can observe manually that `fig.layout.shapes` never updates when I modify rectangle. It just contains the default value:

```auto
(layout.Shape({
     'editable': True, 'x0': 0, 'x1': 2, 'xref': 'x', 'y0': 0, 'y1': 3, 'yref': 'y'
 }),)

```

So I think I may be doing it wrong. But I couldn’t find any other attribute that stores information about shapes.

**Why I want this:**  
I’m trying to make some interface like this:

 ![line-info-qt-gui](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/5/529239c4815434659389fb2559396da4842a253e.gif)

I believe plotly is powerful enough to make this possible - capturing the x-axis range of selection (I can’t use rangeslider because I don’t want zooming it does). And, if it’s not possible in jupyter notebooks - then I have to resort for dash app, but still I’m not sure about whether I need to pass `fig` or `shp` in Input parameter of callback decorator - an example will be very helpful!

Thanks for the great work, I hope a reply soon.

---

<div class="post-metadata">

**Author:** ![sweetdream](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sweetdream](https://community.plotly.com/u/sweetdream)\
**Post date:** [July 7, 2020, 7:44am UTC](https://community.plotly.com/t/how-to-listen-events-on-modifying-a-shape/41977/2 "2020-07-07T07:44:40Z")

</div>

Hi jaladh,

I am facing exactly the same issue.  
I think the issue is coming from the fig.show() that seems to break the callback.  
If you just replace :

```auto
fig.show(config={'modeBarButtonsToAdd':[
                                        'drawrect',
                                        'eraseshape'
                                       ]})

```

by :

```auto
from IPython.display import display
display(fig)  

```

it works. But you are loosing the two buttons “drawrect” and “eraseshape”.

Maybe worth posting a bug report on github ?  
Please post here if you can find a solution or work-around 🙂

Hope this helps.

Patrick

---

<div class="post-metadata">

**Author:** ![sweetdream](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sweetdream](https://community.plotly.com/u/sweetdream)\
**Post date:** [July 7, 2020, 8:16am UTC](https://community.plotly.com/t/how-to-listen-events-on-modifying-a-shape/41977/3 "2020-07-07T08:16:24Z")

</div>

By the way I’ve just noticed from :

> **[Displaying](https://plotly.com/python/renderers/)**
>
> Detailed examples of Displaying Figures including changing color, size, log axes, and more in Python.

It says :  
`It is important to note that `FigureWidget`does not use the renderers framework discussed above, so you should not use the`show()`figure method or the`plotly.io.show`function on`FigureWidget` objects.`

So, you all functions, on\_\* (such as on\_change) which are FigureWidget specific will not work is you use the “show” function.

other useful ref:

> [@How to use 'scatter.on\_change' to grab event?](https://community.plotly.com/t/how-to-use-scatter-on-change-to-grab-event/18103):
>
> Hello. I’d like to find some examples for “on\_change” function for “Scatter” plot. Just need to send some event when plot is changed (eg. series are hide) to trigger some function which will update another plot (eg. resolve average). I tried to use it similarly to what I’ve found for ‘scatter.on\_click’ but dunno how to get it work. However, this example https://plot.ly/python/click-events/ as well doesn’t work for my case, because I use ‘iplot’. I hope ‘on\_change’ don’t worry about ‘iplot’. A…
