Empty page when Flask configured to localhost

Hi, I have a bokeh app which i am trying to render thru Flask. Bokeh and Flask are installed on Ubuntu VM (on AWS, just in case that matters). The below code works only if i replace localhost:5006/env with the public.ip:5006/env. If i change the url in the below code to locahost:5006/env i get a blank page (at my.public.ip:5000).

from bokeh.resources import CDN

from flask import Flask, render_template
from bokeh.embed import server_document,server_session
from bokeh.client import pull_session

app = Flask(name)

@app.route(β€œ/”)

def index():
myurl = β€œhttp://localhost:5006/env”
bokeh_script = server_document(myurl)
return render_template(β€œindex.html”,bokeh_script=bokeh_script)

#run the app
if name == β€œmain”:
app.run(host=β€˜0.0.0.0’) #app.run(debug=True)

Any idea why this is happening? What could be the fix for it? I am trying not to hardcode the public IP in my code.

In localhost You have to put your public.ip

Just to expand on @nghenzi comment: the value of myurl is sent the browser, and the browser attempts to embed the app from that URL. If the Bokeh server is not on the same machine as the browser, then localhost cannot ever work (the browser will try to connect to a Bokeh server on the same machine, and that is not where the Bokeh server is running).

So, TLDR; you will have to configure a either a public IP address, or a valid domain name that points to the machine where the Bokeh server is running.

1 Like

@nghenzi Thank you!
@Bryan That explanation is very helpful. thanks!