与带有cgi.py模块的Flask一起使用时出现Bokeh错误



我正试图使用中描述的示例通过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

我的其他烧瓶应用程序运行得非常好。我认为博凯图书馆有问题。其他人也面临类似的问题吗?

编辑:添加了下面的代码示例

以下是更多详细信息:

$ 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)
    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    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)

cgi是Python中的内置模块。您已经将自己的cgi模块直接放置在路径上,这就掩盖了真正的内置模块。将cgi.py文件重命名为其他文件。

最新更新