The synchronization between Python and JS only happens at function exits, so the first line.visible = False
has no effect since you change the value back to True before the function ends. You will need to split up your callback into two pieces using add_next_tick_callback
to do the second part of the work in a separate function invocation. This technique is demonstrated here:
https://docs.bokeh.org/en/latest/docs/user_guide/server/app.html#updating-from-threads