Figure out Port Number

Hi,

I’m trying to connect panel apps to flask. There are some ideas about this already here: https://docs.bokeh.org/en/latest/docs/user_guide/server.html

from flask import Flask, render_template

from bokeh.client import pull_session
from bokeh.embed import server_session

app = Flask(__name__)

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

    with pull_session(url="http://localhost:5006/sliders") as session:

        # update or customize that session
        session.document.roots[0].children[1].title.text = "Special Sliders For A Specific User!"

        # generate a script to load the customized session
        script = server_session(session_id=session.id, url='http://localhost:5006/sliders')

        # use the script in the rendered page
        return render_template("embed.html", script=script, template="Flask")

if __name__ == '__main__':
    app.run(port=8080)

Effectively, if I understand correctly, I need to use the pull_session. In order to do that correctly, I need to figure out what port panel was running on. I could of course assign a port by hand and use that one, but having the program randomly select an open one is handy (and might even be required if my idea expands to multiple users)

My current thinking is to use pn.serve and then figure out the port number from the object that returns. However, if I run pn.serve with something I want to display, the program blocks:

a = pn.serve(gs) # gs is a GridSpec with some plots/buttons/whatever
print(a) # doesn't print until I hit Ctrl-C
print(dir(a))

Is there a way to do this? Is this even the correct approach, or am I on the wrong path here?

1 Like

It seems like a good idea that we print the current port and address the server is running on when you run .serve and .show. An issue about that would be appreciated. Without that I sadly don’t have any good ideas but if you’re having to sync pull_session it seems like the only way to automate that across those processes would be to use a fixed port anyway.

hmm, thanks. Maybe I need a different approach then. Ideally I’d like to have one separate panel “app” for each thing I’d like to show and then connect them all together with Flask, since then I can get some other things like login pages and so on. If you have any ideas that would help, I’d be grateful!

Here’s an issue regarding the port number in pn.serve: https://github.com/holoviz/panel/issues/1088