简体   繁体   English

无法使用DirectoryHandler运行Bokeh服务器

[英]Can't get Bokeh server running with DirectoryHandler

I'm trying to get a bokeh server running with a DirectoryHandler. 我正在尝试使用DirectoryHandler运行bokeh服务器。 The server seems to start but when I load the page in my browser it shows a blank page. 服务器似乎已启动,但是当我在浏览器中加载页面时,它显示为空白页面。

When I use a ScriptHandler and call the main.py directly everything works fine. 当我使用ScriptHandler并直接调用main.py时,一切正常。 The problem is that in this case the static directory is not recognized. 问题在于,在这种情况下,无法识别静态目录。

This does not work (DirectoryHandler): 不起作用 (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()

This does work(ScriptHandler) 确实有效(ScriptHandler)

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()

where main.py is: 其中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))

What am I missing here? 我在这里想念什么?

I need the static directory for images I want to load. 我需要要加载的图像的静态目录。

Good working example! 很好的例子! I am struggling to recreate though. 我正在努力重建。 I ran your code exactly on Windows, Python3.7 and bokeh 1.3.1 and bokeh server launches the app on http://localhost:8080/ . 我完全在Windows,Python3.7和bokeh 1.3.1上运行了您的代码,而bokeh服务器在http:// localhost:8080 /上启动了该应用程序。

Some things to try: 可以尝试的一些事情:

  1. Where and how are you executing the calling script (the scripts where the only difference is DirectoryHandler or ScriptHandler)? 您在哪里以及如何执行调用脚本(唯一区别在于DirectoryHandler或ScriptHandler的脚本)? I put mine in a separate run.py and just ran it with "python run.py". 我把我放在一个单独的run.py中,然后用“ python run.py”运行它。 run.py can but doesn't have to be in the top level app folder. run.py可以但不必位于顶层应用程序文件夹中。 It runs with main.py as the only file in my top level app folder. 它与main.py一起运行,这是我的顶级应用程序文件夹中的唯一文件。

  2. What are outputs of python console logging? python控制台日志记录的输出是什么?

  3. What are outputs of your browsers developer tab (usually F12)? 浏览器“开发人员”标签(通常为F12)的输出是什么? I do get an error that Tornado can't find the favicon.ico file (lil browser thumbnail image) 我确实收到错误消息,说龙卷风找不到favicon.ico文件(lil浏览器缩略图)

If you take a look at the Bokeh library code for the DirectoryHandler it doesn't do much beyond decorating the ScriptHandler! 如果您查看DirectoryHandler的Bokeh库代码,除了装饰ScriptHandler之外,它没有做其他事情!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM