与带有cgi.py模块的Flask一起使用时的散景错误

问题描述 投票:0回答:1

我正在尝试使用https://github.com/bokeh/bokeh/tree/master/examples/embed/simple中描述的示例通过Flask渲染散景图。

我收到以下错误:

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

    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)

[我正在尝试使用以下示例中的示例通过Flask渲染bokeh散点图:我收到以下错误:[Fri Dec 18 12 :13:46 ...

python flask cgi bokeh
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.