【问题标题】:Bokeh Error while using with Flask with cgi.py module使用带有 cgi.py 模块的 Flask 时出现散景错误
【发布时间】:2019-11-24 03:18:48
【问题描述】:

我正在尝试使用以下示例中描述的示例通过 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)

    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)

【问题讨论】:

  • 我认为这是一个循环导入...

标签: python flask cgi bokeh


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多