从独立的bokeh服务器加载模板

时间:2019-02-05 12:49:42

标签: python bokeh

查看bokeh dash example bokeh serve将jinja2模板加载到以下位置“ ./ templates / index.html”

使用import bokeh.server.server以独立方式从主系统启动bokeh时,该怎么办?

1 个答案:

答案 0 :(得分:1)

对于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