我正尝试使用以下所述的示例通过Flask渲染散景图:https://github.com/bokeh/bokeh/tree/master/examples/embed/simple
我收到以下错误:
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] Traceback (most recent call last):
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/web/cgi-bin/flask_temp/cgi.py", line 3, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from app import app
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/web/cgi-bin/flask_temp/app.py", line 8, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from bokeh.plotting import figure
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/local/lib/python2.7/dist-packages/bokeh/plotting.py", line 15, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from .session import Session
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/local/lib/python2.7/dist-packages/bokeh/session.py", line 30, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from requests.exceptions import ConnectionError
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 58, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from . import utils
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/local/lib/python2.7/dist-packages/requests/utils.py", line 12, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] import cgi
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] File "/usr/web/cgi-bin/flask_temp/cgi.py", line 3, in <module>
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] from app import app
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] ImportError: cannot import name app
[Fri Dec 18 12:13:46 2015] [error] [client 172.17.106.178] Premature end of script headers: cgi.py
我的其他烧瓶应用程序运行正常。我相信Bokeh库存在问题。还有其他人也面临过类似的问题吗?
编辑:在下面添加了代码示例。
以下是更多详情:
$ uname -a
3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64 GNU/Linux
$ python --version
Python 2.7.3
$ python -c "import bokeh; print bokeh.__version__"
0.10.0
$ python -c "import flask; print flask.__version__"
0.10.1
这是我的cgi.py
$ cat cgi.py
#!/usr/bin/python
from app import app
import logging, sys
from wsgiref.handlers import CGIHandler
logging.basicConfig(stream=sys.stderr)
CGIHandler().run(app)
这是我的app.py
$ cat app.py
import logging
from logging import StreamHandler
from flask import Flask, render_template, request,redirect,url_for
import os
import sys
from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.resources import INLINE
from bokeh.util.string import encode_utf8
app = Flask(__name__)
logger = StreamHandler()
logger.setLevel(logging.DEBUG)
app.logger.addHandler(logger)
@app.route("/")
def home(methods=['GET']):
return "Flask temporary app"
colors = {
'Black': '#000000',
'Red': '#FF0000',
'Green': '#00FF00',
'Blue': '#0000FF',
}
def getitem(obj, item, default):
if item not in obj:
return default
else:
return obj[item]
@app.route("/profile")
def polynomial():
""" Very simple embedding of a polynomial chart"""
# Grab the inputs arguments from the URL
# This is automated by the button
args = request.args
# Get all the form arguments in the url with defaults
color = colors[getitem(args, 'color', 'Black')]
_from = int(getitem(args, '_from', 0))
to = int(getitem(args, 'to', 10))
# Create a polynomial line graph
x = list(range(_from, to + 1))
fig = figure(title="Polynomial")
fig.line(x, [i ** 2 for i in x], color=color, line_width=2)
# Configure resources to include BokehJS inline in the document.
# For more details see:
# http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh-embed
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
# For more details see:
# http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components
script, div = components(fig, INLINE)
html = render_template(
'profile.html',
plot_script=script,
plot_div=div,
js_resources=js_resources,
css_resources=css_resources,
color=color,
_from=_from,
to=to
)
return encode_utf8(html)
if __name__ == "__main__":
app.debug = True
app.run(debug=True)
答案 0 :(得分:2)
cgi
是Python中的内置模块。您已将自己的cgi
模块直接放置在路径上,并且隐藏了真正的内置模块。将您的cgi.py
文件重命名为其他内容。