我当前正在构建一个Flask应用程序,并且我试图在其中加载一个Plotly Dash应用程序。
我能够从下面的stackoverflow留言板上得到这么远的信息,但是我认为我做的事情不正确(可能忽略了什么?)
Running a Dash app within a Flask app
这里的任何帮助将不胜感激
这是我的server.py代码:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_login import LoginManager
from flask_pagedown import PageDown
from flask_migrate import Migrate, MigrateCommand
from flask_sslify import SSLify
import os
import dash
import flask
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server)
basedir = os.path.abspath(os.path.dirname(__file__))
Here is my views.py:
from flask import Flask, request, render_template, flash, redirect,
session, url_for, g, jsonify, make_response
from services.email import send_invite_email, send_recover_email,
send_password_was_changed_email
from werkzeug.datastructures import CombinedMultiDict
from werkzeug.utils import secure_filename
from app import server, app
from app import db
from models import User, Post
from forms import RegisterForm, LoginForm, ForgotForm, ProfileForm,
ResetPassword, InviteForm, PostForm
from flask_mongoengine import MongoEngine
from flask_login import login_user, logout_user, current_user,
login_required, LoginManager
import os
from flask_babel import Babel, lazy_gettext
from flask_uploads import UploadSet, IMAGES, configure_uploads
from flask_avatar import Avatar
from momentjs import momentjs
from flask_mail import Mail
from dash import Dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, State, Output
import pandas as pd
import plotly.graph_objs as go
from analysis import *
df = pd.read_csv(
'/Users.../ds/total/main.csv')
@server.before_request
def before_request():
g.user = current_user
@server.route('/index', methods=['GET','POST'])
def index():
app.layout = html.Div([
dcc.Graph(
..... [[more dash code]]
])
app.layout = html.Div(children=[
[[more dash code]]
])
这是我的server.py: 从应用程序导入服务器,应用程序 server.run(debug = True)
全部运行时,我得到:
Traceback (most recent call last):
File "/Users/..lib/python3.6/site-packages/flask/app.py", line 2292, in
wsgi_app
response = self.full_dispatch_request()
File "/Users/lib/python3.6/site-packages/flask/app.py", line 1808, in
full_dispatch_request
self.try_trigger_before_first_request_functions()
File "/Users/lib/python3.6/site-packages/flask/app.py", line 1855, in
try_trigger_before_first_request_functions
func()
File "/Users/lib/python3.6/site-packages/dash/dash.py", line 561, in
_setup_server
self._generate_scripts_html()
File "/Users//lib/python3.6/site-packages/dash/dash.py", line 245, in
_generate_scripts_html
self.scripts.get_all_scripts() +
File "/Users/lib/python3.6/site-packages/dash/resources.py", line 128,
in get_all_scripts
return self._resources.get_all_resources()
File "/Users/lib/python3.6/site-packages/dash/resources.py", line 57,
in get_all_resources
self.get_inferred_resources() + self._resources
File "/Users//lib/python3.6/site-packages/dash/resources.py", line 86,
in
get_inferred_resources
for t in layout.traverse():
AttributeError: 'NoneType' object has no attribute 'traverse'
答案 0 :(得分:0)
尝试运行Dash应用程序时,我遇到了相同的错误消息,由于this thread,我对其进行了修复。据说您需要在Procfile中将app:server
更改为index:app.server
。
我不确定它是否适用于您的情况,甚至您是否拥有Procfile文件,但我还是将它放在这里的情况与以前一样。