Flask 和 Flask-SocketIO 集成和导入错误
Flask and Flask-SocketIO integration and import errors
我正在尝试将 Flask-SocketIO 与我的 Flask 应用程序集成。
由于某种原因,我收到了由
from .. import socketio
我的 events.py 中有,我不确定为什么。
如有任何帮助,我将不胜感激。
app.py
from factory import create_app
app = create_app()
factory.py
from flask import Flask
from flask.ext.socketio import SocketIO
from flask.ext.login import LoginManager
import os
from celery import Celery
lm = LoginManager()
socketio = SocketIO()
lm.login_view = 'main.login'
lm.session_protection = 'strong'
def create_app():
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_CONFIG')) # export APP_CONFIG=settings.Development
lm.init_app(app)
from project.main import main as main_blueprint
app.register_blueprint(main_blueprint)
socketio.init_app(app)
return app
main/____init____.py
from flask import Blueprint
main = Blueprint('main', __name__)
import views, events
main/events.py
# from __future__ import absolute_import
from uuid import uuid4
from flask import current_app, session, request
from flask.ext.socketio import emit, disconnect
from .. import socketio
@socketio.on('status', namespace='/events')
def events_message(message):
print 'socketio.on: status'
emit('status', {'status': message['status']})
@socketio.on('disconnect request', namespace='/events')
def disconnect_request():
print 'socketio.on: disconnect request'
emit('status', {'status': 'Disconnected!'})
disconnect()
@socketio.on('connect', namespace='/events')
def events_connect():
print 'socketio.on: connect'
websocket_id = str(uuid4())
session['websocket_id'] = websocket_id
current_app.clients[websocket_id] = request.namespace
emit('websocket_id', {'websocket_id': websocket_id})
将 main/____init____.py
重命名为 main/__init__.py
,您可以:
from . import socketio
如果 socketio
像你说的那样定义在 __init__.py
中。但是,它看起来像是在 factory.py
中定义的,在这种情况下您会这样做:
from ..factory import socketio
我正在尝试将 Flask-SocketIO 与我的 Flask 应用程序集成。
由于某种原因,我收到了由
from .. import socketio
我的 events.py 中有,我不确定为什么。
如有任何帮助,我将不胜感激。
app.py
from factory import create_app
app = create_app()
factory.py
from flask import Flask
from flask.ext.socketio import SocketIO
from flask.ext.login import LoginManager
import os
from celery import Celery
lm = LoginManager()
socketio = SocketIO()
lm.login_view = 'main.login'
lm.session_protection = 'strong'
def create_app():
app = Flask(__name__)
app.config.from_object(os.environ.get('APP_CONFIG')) # export APP_CONFIG=settings.Development
lm.init_app(app)
from project.main import main as main_blueprint
app.register_blueprint(main_blueprint)
socketio.init_app(app)
return app
main/____init____.py
from flask import Blueprint
main = Blueprint('main', __name__)
import views, events
main/events.py
# from __future__ import absolute_import
from uuid import uuid4
from flask import current_app, session, request
from flask.ext.socketio import emit, disconnect
from .. import socketio
@socketio.on('status', namespace='/events')
def events_message(message):
print 'socketio.on: status'
emit('status', {'status': message['status']})
@socketio.on('disconnect request', namespace='/events')
def disconnect_request():
print 'socketio.on: disconnect request'
emit('status', {'status': 'Disconnected!'})
disconnect()
@socketio.on('connect', namespace='/events')
def events_connect():
print 'socketio.on: connect'
websocket_id = str(uuid4())
session['websocket_id'] = websocket_id
current_app.clients[websocket_id] = request.namespace
emit('websocket_id', {'websocket_id': websocket_id})
将 main/____init____.py
重命名为 main/__init__.py
,您可以:
from . import socketio
如果 socketio
像你说的那样定义在 __init__.py
中。但是,它看起来像是在 factory.py
中定义的,在这种情况下您会这样做:
from ..factory import socketio