☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Find the DnD coordinates when cropping with BoxSelectTool

Community Support
bhomass
bhomass

I want a cross between the BoxSelectTool and BoxEditTool. When I crop a region on my scatter graph, I want to know which dots are selected as well as leave behind a rectangular marker of the selected region. I am using embedded server and callback. Right now, source.selected.on_change(‘indices’,callback) will give me the indices of selected dots, but not the coordinates with which to draw my rectangle. Can someone point out a way to accomplish this?

⌁   Link to post
Bryan
Bryan

There is not any way to have a single callback that gets both (short of creating your own custom extension tool) but you can have a separate callback that responds just to the selection geometry event. See this example:

⌁   Link to post
bhomass
bhomass

thanks for the code sample. I got it working, but when I tried to use print_event, I get an error
p.js_on_event(events.SelectionGeometry, print_event(attributes=[‘geometry’, ‘final’]))

TypeError: Object of type ‘function’ is not JSON serializable

what is the proper way to call the print_event function in the code?

⌁   Link to post
Bryan
Bryan

@bhomass print_event is just something created for that demo, it can be modified to suit your needs, or does not need to be used at all. It’s not anything standard or official in the library. It exists to be very general in order to make an example that covers all possible events. I would suggest you might just want to make a plain function callback, rather than use a generic factory-function for manufacturing callbacks (which is what print_event is)

In any case, please provide the entire stack trace (not just the last line) as well as some of the code context around the line that the exception reports.

⌁   Link to post
bhomass
bhomass

I tried writing a simple callback, but I don’t know the right syntax
def event_callback(attributes):
print(‘attributes={}’.format(attributes))

plot.js_on_event(events.SelectionGeometry, event_callback(attributes=['geometry', 'final']))    

the output just says
attributes=['geometry', 'final']

When I use the print_event call, I get
ERROR:bokeh.server.protocol_handler:error handling message
message: Message ‘PULL-DOC-REQ’ (revision 1) content: {}
error: TypeError(“Object of type ‘function’ is not JSON serializable”,)
Traceback (most recent call last):
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/server/protocol_handler.py”, line 100, in handle
work = yield handler(message, connection)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/tornado/gen.py”, line 735, in run
value = future.result()
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/tornado/gen.py”, line 748, in run
yielded = self.gen.send(value)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/server/session.py”, line 70, in _needs_document_lock_wrapper
result = yield yield_for_all_futures(func(self, *args, **kwargs))
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/server/session.py”, line 228, in _handle_pull
return connection.protocol.create(‘PULL-DOC-REPLY’, message.header[‘msgid’], self.document)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/protocol/init.py”, line 80, in create
return self._messages[msgtype].create(*args, **kwargs)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/protocol/messages/pull_doc_reply.py”, line 83, in create
content = { ‘doc’ : document.to_json() }
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/document/document.py”, line 845, in to_json
doc_json = self.to_json_string()
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/document/document.py”, line 874, in to_json_string
return serialize_json(json, indent=indent)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/core/json_encoder.py”, line 160, in serialize_json
return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/json/init.py”, line 238, in dumps
**kw).encode(obj)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/json/encoder.py”, line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/json/encoder.py”, line 257, in iterencode
return _iterencode(o, 0)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/core/json_encoder.py”, line 251, in default
return self.transform_python_types(obj)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/site-packages/bokeh/core/json_encoder.py”, line 218, in transform_python_types
return super(BokehJSONEncoder, self).default(obj)
File “/home/bruce/anaconda3/envs/bokeh/lib/python3.6/json/encoder.py”, line 180, in default
o.class.name)
TypeError: Object of type ‘function’ is not JSON serializable

⌁   Link to post

End of archived discussion · Replies are disabled