[bokeh] How to access the coordinates of the 'box select tool' rect?

Hi,

There is a dusty and not-often used facility called ToolEvents that you can use at the moment:

  from bokeh.plotting import figure
  from bokeh.io import curdoc

  p = figure(tools="box_select")
  p.circle([1,2,3], [4,5,6])

  def cb(attr, old, new):
      print(p.tool_events.geometries)

  p.tool_events.on_change('geometries', cb)

  curdoc().add_root(p)

Running this will generate output like

  [{'type': 'rect', 'vx0': 169.18383026123047, 'vx1': 405.18383026123047, 'vy0': 143, 'vy1': 365, 'x0': 1.4736090642078281, 'y0': 4.357899409085795, 'x1': 2.4312257718798547, 'y1': 5.213532885154276}]

Which is what you are after, if I understand your question.

HOWEVER, please be advised that we are currently in the process of revamping the events system to be more general and consistent, and the ToolEvents mode will probably be deprecated in the process. (i.e. just understand that the code above will have to change at some point in the future).

Thanks,

Bryan

···

On Feb 25, 2017, at 16:30, nicolas.fr <[email protected]> wrote:

I forgot to mention that the problem for us is to access the selection 'geometry' from python.
We used to inject the selection coordinates into the python world asking the ipython kernel to execute a cmd from the JS callback attached to the BoxSelectTool. Unfortunately, this approach doesn't work properly with the 'embed server'.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/66fd63e6-c3e0-4a44-b922-d056849908e9%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Is there an updated version of this example somewhere?
I could not figure out how to do this with latest bokeh.

Ideally I’d like to get back the geometries drawn as python variables in a notebook.

Thanks,

-F

···

On Monday, February 27, 2017 at 2:23:43 PM UTC-3, Bryan Van de ven wrote:

Hi,

There is a dusty and not-often used facility called ToolEvents that you can use at the moment:

    from bokeh.plotting import figure

    from [bokeh.io](http://bokeh.io) import curdoc



    p = figure(tools="box_select")

    p.circle([1,2,3], [4,5,6])



    def cb(attr, old, new):

        print(p.tool_events.geometries)



    p.tool_events.on_change('geometries', cb)



    curdoc().add_root(p)

Running this will generate output like

    [{'type': 'rect', 'vx0': 169.18383026123047, 'vx1': 405.18383026123047, 'vy0': 143, 'vy1': 365, 'x0': 1.4736090642078281, 'y0': 4.357899409085795, 'x1': 2.4312257718798547, 'y1': 5.213532885154276}]

Which is what you are after, if I understand your question.

HOWEVER, please be advised that we are currently in the process of revamping the events system to be more general and consistent, and the ToolEvents mode will probably be deprecated in the process. (i.e. just understand that the code above will have to change at some point in the future).

Thanks,

Bryan

On Feb 25, 2017, at 16:30, nicolas.fr [email protected] wrote:

I forgot to mention that the problem for us is to access the selection ‘geometry’ from python.
We used to inject the selection coordinates into the python world asking the ipython kernel to execute a cmd from the JS callback attached to the BoxSelectTool. Unfortunately, this approach doesn’t work properly with the ‘embed server’.


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/66fd63e6-c3e0-4a44-b922-d056849908e9%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi,

This example shows a bokeh app that uses the bokeh.events machinery to print selection geometries:

  https://github.com/bokeh/bokeh/blob/masieter/examples/howto/events_app.py

For a notebook, that means embedding a Bokeh server app, e.g. like

  https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb

Thanks,

Bryan

···

On Jun 24, 2018, at 08:29, [email protected] wrote:

Is there an updated version of this example somewhere?
I could not figure out how to do this with latest bokeh.

Ideally I'd like to get back the geometries drawn as python variables in a notebook.

Thanks,

-F

On Monday, February 27, 2017 at 2:23:43 PM UTC-3, Bryan Van de ven wrote:
Hi,

There is a dusty and not-often used facility called ToolEvents that you can use at the moment:

        from bokeh.plotting import figure
        from bokeh.io import curdoc

        p = figure(tools="box_select")
        p.circle([1,2,3], [4,5,6])

        def cb(attr, old, new):
            print(p.tool_events.geometries)

        p.tool_events.on_change('geometries', cb)

        curdoc().add_root(p)

Running this will generate output like

        [{'type': 'rect', 'vx0': 169.18383026123047, 'vx1': 405.18383026123047, 'vy0': 143, 'vy1': 365, 'x0': 1.4736090642078281, 'y0': 4.357899409085795, 'x1': 2.4312257718798547, 'y1': 5.213532885154276}]

Which is what you are after, if I understand your question.

HOWEVER, please be advised that we are currently in the process of revamping the events system to be more general and consistent, and the ToolEvents mode will probably be deprecated in the process. (i.e. just understand that the code above will have to change at some point in the future).

Thanks,

Bryan

> On Feb 25, 2017, at 16:30, nicolas.fr <[email protected]> wrote:
>
> I forgot to mention that the problem for us is to access the selection 'geometry' from python.
> We used to inject the selection coordinates into the python world asking the ipython kernel to execute a cmd from the JS callback attached to the BoxSelectTool. Unfortunately, this approach doesn't work properly with the 'embed server'.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/66fd63e6-c3e0-4a44-b922-d056849908e9%40continuum.io.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/c5491b10-292c-4353-aac1-5fd6add3037b%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi,

This example shows a bokeh app that uses the bokeh.events machinery to print selection geometries:

    [https://github.com/bokeh/bokeh/blob/masieter/examples/howto/events_app.py](https://github.com/bokeh/bokeh/blob/masieter/examples/howto/events_app.py)

For a notebook, that means embedding a Bokeh server app, e.g. like

    [https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb](https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/notebook_embed.ipynb)

Thanks! My goal is to be able to draw points, lines, areas in images and get the geometry back.

Here is what I got so far:

https://gist.github.com/ocefpaf/c7706893a525c6d40c5ca07e16f6e746

Things that could improve this but I have no idea on how to implement them:

  1. The polygon select does not show the points and lines when I click.

  2. It would be nice if the last thing drawn could persist on the image.

Ideally I could draw stuff, modify it, and then reset the figure to remove them.

  1. Add a button that activates the callback only when clicked,

so the user can draw multiple times and save only the “correct” ones.

For (2) I saw an example of creating a patch with the drawing and then adding it to the image.

Maybe that is the best way to implement that? Not sure.

(1) Looks like a bug and I maybe there is already an example for (3) somewhere but I could not find it.

Thanks again,

-Filipe

PS: the first link above is a 404, the correct one is:

https://github.com/bokeh/bokeh/blob/master/examples/howto/events_app.py