无法使用DirectoryHandler运行Bokeh服务器

时间:2019-07-30 07:59:40

标签: python-3.x bokeh

我正在尝试使用DirectoryHandler运行bokeh服务器。 服务器似乎已启动,但是当我在浏览器中加载页面时,它显示为空白页面。

当我使用ScriptHandler并直接调用main.py时,一切正常。问题在于,在这种情况下,无法识别静态目录。

不起作用(DirectoryHandler):

from bokeh.application import Application
from bokeh.application.handlers import DirectoryHandler
from bokeh.server.server import Server

from tornado.ioloop import IOLoop

io_loop = IOLoop.current()


def create_bokeh_server(io_loop, file, port):
    """Start bokeh server with applications paths"""

    bokeh_app = Application(DirectoryHandler(filename=file))

    # kwargs lifted from bokeh serve call to Server, with created io_loop
    kwargs = {
        'io_loop': io_loop,
        'port': port,
    }
    server = Server(bokeh_app,**kwargs)

    return server


if __name__ == '__main__':
    # create server
    print('Create server')
    bokeh_server = create_bokeh_server(io_loop=io_loop, file='C:/bokeh_app_dir/', port=8080)

    # start bokeh server
    print('Start bokeh server')
    bokeh_server.start()

    # start web server
    print('Start Localhost')
    io_loop.start()

确实有效(脚本处理程序)

from bokeh.application import Application
from bokeh.application.handlers.script import ScriptHandler
from bokeh.server.server import Server

from tornado.ioloop import IOLoop

io_loop = IOLoop.current()


def create_bokeh_server(io_loop, file, port):
    """Start bokeh server with applications paths"""

    bokeh_app = Application(ScriptHandler(filename=file))

    # kwargs lifted from bokeh serve call to Server, with created io_loop
    kwargs = {
        'io_loop': io_loop,
        'port': port,
    }
    server = Server(bokeh_app,**kwargs)

    return server


if __name__ == '__main__':
    # create server
    print('Create server')
    bokeh_server = create_bokeh_server(io_loop=io_loop, file='C:/bokeh_app_dir/main.py', port=8080)

    # start bokeh server
    print('Start bokeh server')
    bokeh_server.start()

    # start web server
    print('Start Localhost')
    io_loop.start()

其中main.py是:

rom random import random

from bokeh.layouts import column
from bokeh.models import Button
from bokeh.models.widgets import CheckboxGroup, RadioGroup
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc



# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None

# add a text renderer to our plot (no data yet)
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
           text_baseline="middle", text_align="center")

i = 0

ds = r.data_source

# create a callback that will add a number in a random location
def callback():
    global i

    # BEST PRACTICE --- update .data in one step with a new dict
    new_data = dict()
    new_data['x'] = ds.data['x'] + [random()*70 + 15]
    new_data['y'] = ds.data['y'] + [random()*70 + 15]
    new_data['text_color'] = ds.data['text_color'] + [RdYlBu3[i%3]]
    new_data['text'] = ds.data['text'] + [str(i)]
    ds.data = new_data

    i = i + 1

def update():
    """Example of updating plot on radio button change"""
    if radio_group.active == 0:
        p.border_fill_color = 'black'
        p.background_fill_color = 'black'
    elif radio_group.active == 1:
        p.border_fill_color = 'white'
        p.background_fill_color = 'white'
    else:
        p.border_fill_color = 'blue'
        p.background_fill_color = 'blue'


# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(callback)

# add group of radio butt
radio_group = RadioGroup(
    labels=['black', 'white', 'blue'],
    active=0
)
radio_group.on_change('active', lambda attr, old, new: update())

# put the button and plot in a layout and add to the document
curdoc().add_root(column(radio_group, button, p))

我在这里想念什么?

我需要用于加载图像的静态目录。

1 个答案:

答案 0 :(得分:0)

很好的例子!我正在努力重建。我完全在Windows,Python3.7和bokeh 1.3.1上运行了您的代码,而bokeh服务器在http://localhost:8080/上启动了该应用。

可以尝试的一些事情:

  1. 您在哪里以及如何执行调用脚本(只有DirectoryHandler或ScriptHandler的脚本)?我把我放在一个单独的run.py中,然后用“ python run.py”运行它。 run.py可以但不必位于顶层应用程序文件夹中。它与main.py一起运行,这是我的顶级应用程序文件夹中的唯一文件。

  2. python控制台日志记录的输出是什么?

  3. 浏览器“开发人员”选项卡(通常为F12)的输出是什么?我确实收到一个错误消息,说龙卷风找不到favicon.ico文件(lil浏览器缩略图)

如果您查看DirectoryHandler的Bokeh库代码,除了装饰ScriptHandler并没有什么用!

https://docs.bokeh.org/en/latest/_modules/bokeh/application/handlers/directory.html#DirectoryHandler