Confused about server options

Hello, I’m trying to use Bokeh to serve up about a dozen plots/widgets, and I still haven’t managed to wrap my head around the best way to do so. The plots/widgets that I have will be used across 3 or 4 pages (or tabs).

Version 1:
I could serve each individual page separately:

bokeh serve my_app1.py
bokeh serve my_app2.py
bokeh serve my_app3.py
bokeh serve my_app4.py

And then use Flask to embed these via server_document():

@app.route('/page1')
def page1():
    app_url = "http://localhost:5006/my_app1"
    with pull_session(url=app_url) as session:
        script = server_document(app_url)
        return render_template('page1.html', script=script)

Etc…

Version 2:
The other option would be to avoid using Flask, stick all my plots in a single serve application like so:

curdoc().add_root(layout(plot1, sizing_mode='stretch_both', name="plot1"))
curdoc().add_root(layout(plot2, sizing_mode='stretch_both', name="plot2"))
...

Then I can access and layout each plot in my templates via {{ embed(roots.plot1) }}, etc…

What I like about Version 2 is that I can leave the layouts for the templating, which gives me more control. I also only need to run bokeh serve main.py a single time. However, I also like the flexibility that Flask gives me.

What I would really like to be able to do is have a sort of combination of the Versions 1 and 2; how can I use Flask to interact with my Bokeh apps but still leave the laying out of the plots/widgets to the templates? In my Version 1, the page1.html template will just get the big <script>...</script> and stick it somewhere, without getting access to the individual plot components. Is there a way to get access to the components in Version 1?

Am I missing any other options?

Thank you!

So I will start out and say that I think there is almost certainly a technical path to achieve what you are asking, but I am not sure that anyone has ever actually done it, and I don’t know how to do it offhand. The view endpoint that renders pages for Bokeh docs with templates uses the somewhat obscure function server_html_page_for_session. So my first suggestion might be to use that function in your Flask app, together with a session that you obtain with pull_session.

cc @mateusz might also have comments/ideas.