Adding and removing curves from flask

Dear all,
I am trying to add and remove curves from flask.
I was able to access the ColumDataSource, add a plot or remove lines by calling a flask server after the plot has been created, with

    p = session.document.get_model_by_name("figure")
    cds = session.document.get_model_by_name("cds")
    cds.data["y4"] = [1.5,1.5,1.5,1.5,1.5]
    p.line(x='x', y="y4", source=cds)

    session.document.get_model_by_name("line0").visible = False

    # session.push()

however the changes are visible only when I call again the plot, i.e. when I refresh the page

@app.route('/bokeh_v2', methods=['GET'])
def bokeh_app_page_v2():

    return render_template("embed.html", script=script)

I tried to add a session.push() (see first code snippet) after the modifications of the plots, but this is generating a wrong behavior, indeed what happens is that the plot updates by deleting some curves (those associated to the ColumDataSource), while other remain (those produced with other plot in the bokeh script). If I refresh the page (see above) everything looks as expected.

How can I generate the update automatically, without the need to “refresh”?

Thanks,
Luigi

I am not sure if this can help, this is the Console output when I visualize my plot in the broswer (call to /bokeh_v2 above)

DevTools failed to load source map: Could not load content for chrome-extension://kdfieneakcjfaiglcfcgkidlkmlijjnh/content/js/bundle.tracing.min.js.map: System error: net::ERR_FILE_NOT_FOUND
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:43 Bokeh: BokehJS not loaded, scheduling load and callback at Mon May 22 2023 16:07:52 GMT+0300 (Eastern European Summer Time)
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:77 Bokeh: injecting script tag for BokehJS library:  http://192.168.1.101:5006/static/js/bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:77 Bokeh: injecting script tag for BokehJS library:  http://192.168.1.101:5006/static/js/bokeh-gl.min.js?v=9cc8ec1abc75f3359ae423cc92f1ecf9bfd2904423483737fe69e904335557ed44a5fa594abe1191df1b1d2d00ff5d6aad56d745a9f263ad64abd5e8c176ffd5
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:77 Bokeh: injecting script tag for BokehJS library:  http://192.168.1.101:5006/static/js/bokeh-widgets.min.js?v=2c2c4d41bc34e1cebbb6bcbfc6b44cbb5a542c5e354ec67226a8de57389f41d7067d1863bb0bc8f0bdd1b01fb0bc85c48145a15efb16dec77077e09f2e626449
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:77 Bokeh: injecting script tag for BokehJS library:  http://192.168.1.101:5006/static/js/bokeh-tables.min.js?v=d83e9590cfc01b6d99aece15dc11779953d8cfce1b6707f211ecf53c1c9f5e2219f56e0c8ba71495e189e34bc02e3734cb42dfecc7a37b24ad7e2bc297036702
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:77 Bokeh: injecting script tag for BokehJS library:  http://192.168.1.101:5006/static/js/bokeh-mathjax.min.js?v=4c131c35abb2fd0ab0f4546893ddf0bcf6df62d0c91ebf2c5a44260b57d877ddac4fc87512d9646ba2a8d81675524d99d73ee2fcd79c44096ac33f64b573739d
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:49 Bokeh: all BokehJS libraries/stylesheets loaded
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:143 Bokeh: BokehJS plotting callback run at Mon May 22 2023 16:07:52 GMT+0300 (Eastern European Summer Time)
bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:183 [bokeh] setting log level to: 'info'
fd0c96c4-416e-49c2-8c61-8cf2273d9e4b:27 Bokeh: all callbacks have finished
bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:226 [bokeh] Websocket connection 0 is now open
bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:465 [Violation] Added non-passive event listener to a scroll-blocking 'wheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952
w @ bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:465
initialize @ bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:464
a @ bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:224
t.build_view @ bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:224
lazy_initialize @ bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:582
bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:163 Bokeh items were rendered successfully
bokeh.min.js?v=c8d721b75531cff043feef626ae0d6e29a26031fae26bba93c1baa962ecfb1b43f211027df3a0cf7772f30afed8c38198d70677b8967e581ca7e605358c68465:165 [bokeh] document idle at 76 ms
DevTools failed to load source map: Could not load content for chrome-extension://kdfieneakcjfaiglcfcgkidlkmlijjnh/content/js/bundle.tracing.min.js.map: System error: net::ERR_FILE_NOT_FOUND
DevTools failed to load source map: Could not load content for chrome-extension://kdfieneakcjfaiglcfcgkidlkmlijjnh/writer/js/angular-ui-router.js.map: System error: net::ERR_FILE_NOT_FOUND
writer.vendor.js:2 Running Ginger Widget v2.1.199
writer.vendor.js:2 [Violation] 'setTimeout' handler took 57ms
inject.js:359     GET http://192.168.1.101:5000/favicon.ico 404 (NOT FOUND)
extractIconBase64 @ inject.js:359
setTimeout (async)
(anonymous) @ inject.js:325
load (async)
(anonymous) @ inject.js:324
(anonymous) @ inject.js:736

I was able to find a solution (maybe a hack?), by using a function in the boken script attached to a periodic callback, whose purpose is to reprint data when they change.

curdoc().add_periodic_callback(update, 1000)

I am not sure this is the right solution to the problem, since it looks to me that such function to replot should be called only then the data change using for instance

cds.on_change('data', update)

However the latter is not generating any event on the bokeh server.

Any suggestion about which is the best approach to follow?

Thanks,
Luigi

Could anybody be so kind to provide some feedback and tell me if there are better ways than using

curdoc().add_periodic_callback

best,
Luigi