Flask _AppCtxGlobals&x27;对象没有属性';p1和x27;带烧瓶的SocketIO

Flask _AppCtxGlobals&x27;对象没有属性';p1和x27;带烧瓶的SocketIO,flask,websocket,bokeh,Flask,Websocket,Bokeh,我正在使用Flask从bokeh服务器渲染图形 from flask import Flask, render_template from flask.ext.socketio import SocketIO, emit import flask from bokeh.plotting import figure, push from bokeh.models import Range1d from bokeh.embed import components from bokeh.embed

我正在使用Flask从bokeh服务器渲染图形

from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit
import flask

from bokeh.plotting import figure, push
from bokeh.models import Range1d
from bokeh.embed import components
from bokeh.embed import autoload_server
from bokeh.session import Session
from bokeh.document import Document
from bokeh.models.renderers import GlyphRenderer
from flask.globals import current_app

app = Flask("Graph Realtime",static_url_path='/static')
app.config['SECRET_KEY'] = 'secret!'
app.debug = True
socketio = SocketIO(app)

@app.route('/')
def index():
    x1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y1 = [0, 8, 2, 4, 6, 9, 5, 6, 25, 28, 4, 7]
    # select the tools we want
    TOOLS="pan,wheel_zoom,box_zoom,reset,save"

    # the red and blue graphs will share this data range
    xr1 = Range1d(start=0, end=30)
    yr1 = Range1d(start=0, end=30)

    # build our figures
    p1 = figure(x_range=xr1, y_range=yr1, tools=TOOLS, plot_width=300, plot_height=300)
    p1.scatter(x1, y1, size=12, color="red", alpha=0.5)

    document = Document()
    session = Session(root_url='http://localhost:5006/', load_from_config=False)
    session.use_doc('graph')
    session.load_document(document)
    document.clear()  # Semi-optional - see below
    document.add(p1)    # Put it on the server
    session.store_document(document)

    app_con = current_app._get_current_object().app_context()
    app_con.g.p1 = p1.select(dict(type=GlyphRenderer))[0].data_source
    script = autoload_server(p1, session)
    return render_template('index.html', script=script)
我正在将数据源保存到g变量,以便更新它

@socketio.on('my event', namespace='/test')
def test_message(message):
    app = current_app._get_current_object()
    with app.app_context():
        print flask.g.p1
    emit('my response', {'data': 'data'})
如果发生“我的事件”,我将尝试更新p1。 但尝试访问g变量会引发错误AttributeError:“'U AppCtxGlobals'对象没有属性'p1'”错误。
我做错了什么?非常感谢你的帮助

您有没有理由不使用“正常”方式访问
索引()中的
g
?我的意思是:
来自flask import g
。在我的测试应用程序中,您访问
g
的方法不起作用,它只生成一个空的dict。我也尝试了正常的方法,它似乎也不起作用。你在正常情况下运气好吗?