azure - Azure Web 应用服务上的 Flask + Bokeh 服务器

标签 azure flask bokeh azure-web-app-service

我想在 Azure Web 应用服务中托管我的 Bokeh 服务器应用。按照 flask_embed.py 中的示例我创建了一个最小的示例,其中的 Bokeh 服务器进程在 localhost:5006 上运行,并使用 server_document 为其提供服务。在 flask route 。在本地,在我的电脑上,运行正常,没有任何错误:

from threading import Thread

from bokeh.embed import server_document
from bokeh.server.server import Server
from bokeh.models.widgets import Select, Div
from bokeh.layouts import column

from flask import Flask
from flask import render_template
from tornado.ioloop import IOLoop

app = Flask(__name__)

# This is the bokeh page
def modify_doc(doc):
    dropdown = Select(title="Cities", options=["New York", "Berlin"])
    title_row = Div(text="Home Page")

    main_layout = column([
        title_row,
        dropdown
    ])

    doc.add_root(main_layout)
    doc.title = "My bokeh server app"

# This is the subprocess serving the bokeh page
def bk_worker():
    server = Server(
        {'/bkapp': modify_doc},
        io_loop=IOLoop(),
        allow_websocket_origin=["*"],
    )
    server.start()
    server.io_loop.start()


Thread(target=bk_worker).start()

# This is the flask route showing the bokeh page
@app.route("/", methods=["GET"])
def my_app():
    script = server_document("http://localhost:5006/bkapp")
    return render_template("embed.html", script=script, template="Flask")

但是,当我将其推送到 Azure Web 应用程序时,页面为空白,并且通过检查页面会显示错误消息:

GET https://<my-azure-site>.azurewebsites.net:5006/bkapp/autoload.js?bokeh-autoload-element=0bfb1475-9ddb-4af5-9afe-f0c4a681d7aa&bokeh-app-path=/bkapp&bokeh-absolute-url=https://<my-azure-site>.azurewebsites.net:5006/bkapp net::ERR_CONNECTION_TIMED_OUT

我似乎无权访问远程 Azure 服务器的本地主机。实际上,我还不清楚 Bokeh 服务器是否运行/是否允许运行。在 server_document我尝试过输入 server_document("<my-azure-site>:5006/bkapp") 的功能但问题还是一样。

感谢任何帮助。

这篇文章与另一个问题相关:Bokeh embedded in flask app in azure web app

最佳答案

我意识到这是不久前的事情,但过去几天我花了很多时间来解决这个问题,所以这是给 future 的人的:

<小时/>

问题是 server_document() 只是创建一个 <script>嵌入到 jinja2 模板中并在其中执行的标记。

在本地,这不是问题,因为您的 Bokeh 服务器正在您的计算机的 localhost:5006 上运行。为了进行演示,您可以看到可以直接导航到 localhost:5006/bkapp 以查看您的 Bokeh 文档。

一旦您将其托管在 Azure 上,server_document() 就会创建浏览器尝试执行的完全相同的脚本 - 也就是说,您的浏览器将尝试执行 <script>引用 localhost:5006 的标记,但 localhost:5006 上没有任何运行,因为您的 Bokeh 应用程序现在实际上在 Azure 的服务器上运行。

我不确定最好的方法是什么,但其本质是您需要 server_document() 指向远程运行的 Bokeh 服务器。为此,您需要确保 {your_remote_bokeh_server}:5006 可公开访问。

关于azure - Azure Web 应用服务上的 Flask + Bokeh 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52849325/

相关文章:

azure - 如何使用 Terraform 在现有 keystore 中创建 secret ?

python - 在flask的celery文档中,为什么celery任务需要名称?

python - Flask 中的代码 503 与嵌入式 Bokeh 服务器应用程序通过 requests.get() 获取 json 化数据

azure - 更改 Azure 存储帐户中的磁盘类型

azure - 将隐私政策添加到 azure b2c 应用程序上的自定义 ui

python - Flask request.form 不检索任何数据

python - Bokeh - 将颜色贴图应用于线条集

python - Pandas 中 Dataframe 的堆积条形图

azure - SQL Azure 故障转移

python - 使用 Flask-Security 进行 Flask 身份验证