使用散景服务器时导入错误

时间:2015-12-01 14:41:04

标签: python flask bokeh

我在网上找到了这个示例,其中显示了如何使用bokeh-server部署具有交互式绘图的应用程序。 问题是我收到了这个错误

Traceback (most recent call last):
  File "/Users/mtomas/Library/Enthought/Canopy_64bit/User/bin/bokeh-server", line 7, in <module>
    bokeh.server.run()
  File "/Users/mtomas/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/bokeh/server/__init__.py", line 186, in run
    start_server(args)
  File "/Users/mtomas/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/bokeh/server/__init__.py", line 190, in start_server
    start.start_simple_server(args)
  File "/Users/mtomas/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/bokeh/server/start.py", line 58, in start_simple_server
    configure_flask(config_argparse=args)
  File "/Users/mtomas/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/bokeh/server/configure.py", line 76, in configure_flask
    imp.load_source("_bokeh_app", script)
  File "slider.py", line 12, in <module>
    from bokeh.client import push_session
ImportError: No module named client

输入

bokeh-server --script slider.py

slider.py的内容如下

from __future__ import print_function

import numpy as np
import sympy as sy

from bokeh.browserlib import view
from bokeh.document import Document
from bokeh.models.glyphs import Line
from bokeh.models import Plot, DataRange1d, LinearAxis, ColumnDataSource, Grid, Legend
from bokeh.models.widgets import Slider, TextInput, HBox, VBox, Dialog

from bokeh.client import push_session

document = Document()
session = push_session(document)

xs = sy.Symbol('x')
expr = sy.exp(-xs)*sy.sin(xs)
order = 1

def taylor(fx, xs, order, x_range=(0, 1), n=200):
    x0, x1 = x_range
    x = np.linspace(float(x0), float(x1), n)

    fy = sy.lambdify(xs, fx, modules=['numpy'])(x)
    tx = fx.series(xs, n=order).removeO()

    if tx.is_Number:
        ty = np.zeros_like(x)
        ty.fill(float(tx))
    else:
        ty = sy.lambdify(xs, tx, modules=['numpy'])(x)

    return x, fy, ty

def update_data():
    x, fy, ty = taylor(expr, xs, order, (-2*sy.pi, 2*sy.pi), 200)

    plot.title = "%s vs. taylor(%s, n=%d)" % (expr, expr, order)
    legend.legends = [
        ("%s"         % expr, [line_f_glyph]),
        ("taylor(%s)" % expr, [line_t_glyph]),
    ]
    source.data = dict(x=x, fy=fy, ty=ty)
    slider.value = order

source = ColumnDataSource(data=dict(x=[], fy=[], ty=[]))

xdr = DataRange1d()
ydr = DataRange1d()

plot = Plot(x_range=xdr, y_range=ydr, plot_width=800, plot_height=400)

line_f = Line(x="x", y="fy", line_color="blue", line_width=2)
line_f_glyph = plot.add_glyph(source, line_f)
plot.add_layout(line_f_glyph)

line_t = Line(x="x", y="ty", line_color="red", line_width=2)
line_t_glyph = plot.add_glyph(source, line_t)
plot.add_layout(line_t_glyph)

xaxis = LinearAxis()
plot.add_layout(xaxis, 'below')

yaxis = LinearAxis()
plot.add_layout(yaxis, 'left')

xgrid = Grid(dimension=0, ticker=xaxis.ticker)
ygrid = Grid(dimension=1, ticker=yaxis.ticker)

legend = Legend(orientation="bottom_left")
plot.add_layout(legend)

def on_slider_value_change(attr, old, new):
    global order
    order = int(new)
    update_data()

def on_text_value_change(attr, old, new):
    try:
        global expr
        expr = sy.sympify(new, dict(x=xs))
    except (sy.SympifyError, TypeError, ValueError) as exception:
        dialog.content = str(exception)
        dialog.visible = True
    else:
        update_data()

dialog = Dialog(title="Invalid expression")

slider = Slider(start=1, end=20, value=order, step=1, title="Order:")
slider.on_change('value', on_slider_value_change)

text = TextInput(value=str(expr), title="Expression:")
text.on_change('value', on_text_value_change)

inputs = HBox(children=[slider, text])
layout = VBox(children=[inputs, plot, dialog])
update_data()
document.add_root(layout)
session.show(layout)

if __name__ == "__main__":
    print("\npress ctrl-C to exit")
    session.loop_until_closed()

1 个答案:

答案 0 :(得分:1)

它可能来自您的bokeh版本。

import bokeh   
bokeh.__version__ 

bokeh.client似乎在v0.11中。您可以查看https://github.com/bokeh/bokeh/wiki/Porting-guide:-new-Bokeh-server