从独立的bokeh s加载模板

2024-09-20 00:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

查看bokeh dash examplebokeh serve在以下位置加载jinja2模板/索引.html“。你知道吗

当我使用import bokeh.server.server从main启动bokeh作为一个独立的程序时,我该怎么做呢?你知道吗


Tags: import程序模板jinja2servermainhtmlbokeh
1条回答
网友
1楼 · 发布于 2024-09-20 00:16:16

对于Bokeh v0.12.6,这将是:

import numpy as np
import webbrowser
from flask import Flask, render_template
from tornado.ioloop import IOLoop
from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
from bokeh.embed import autoload_server
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
from bokeh.server.server import Server

app = Flask(__name__)

def make_doc(doc):

    def get_plot():
        x = np.linspace(0, 10, 1000)
        y = np.log(x) * np.sin(x)
        source = ColumnDataSource(data = dict(x = np.linspace(0, 10, 1000), y = y))

        plot = figure()
        plot.line('x', 'y', source = source)
        return plot

    doc.add_root(get_plot())
    doc.title = "Time Plot"

bokeh_app = Application(FunctionHandler(make_doc))

@app.route('/', methods = ['GET'])
def bkapp_page():
    script = autoload_server(model = None, url = 'http://localhost:5006/appname')
    return render_template("index.html", script = script)

def bk_worker():
    server = Server({'/appname': bokeh_app}, io_loop = IOLoop(), allow_websocket_origin = ["localhost:{}".format(8080)])
    server.start()
    server.io_loop.start()

from threading import Thread
Thread(target = bk_worker).start()

if __name__ == '__main__':
    print('Opening single process Flask app with embedded Bokeh application on http://localhost:{}/'.format(8080))
    webbrowser.open_new("http://localhost:{}/".format(8080))
    app.run(port = 8080, debug = False)

对于最新的v1.0.4,请将autoload_server()函数替换为server_document()。 结果如下: enter image description here

相关问题 更多 >