问题
I have two websites (both are using Dash & Flask) on my IIS (Windows Server 2016).
The first one is a minimal working example consisting of app.py and web.config. For somehow I cannot get the second site working. Below both examples and the error message is attached.
1 Working Example
FastCGI-Settings:
- PYTHONPATH: C:\inetpub\wwwroot\mvp
- WSGI_HANDLER: app.server
app.py
import flask
import dash
import dash_core_components as dcc
import dash_html_components as html
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server)
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
Dash: A web application framework for Python.
'''),
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montreal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
)
])
if __name__ == '__main__':
app.run_server(debug=True)
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="FlaskHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\ProgramData\Anaconda3\envs\mvp\python.exe|C:\ProgramData\Anaconda3\envs\mvp\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
2 Failing Example
FastCGI-Settings:
- PYTHONPATH: C:\inetpub\wwwroot\testsite
- WSGI_HANDLER: dashapp.server
-flaskapp
- __init__.py
- auth.py
- database_models.py
- email_sending.py
- main.py
--templates
- base.html
- index.html
- login.html
- dashapp.py
- web.config
flaskapp.py
# __init__.py
"""
Set up for the FLASK app, Database, basic login and Integrating the Dash app
-> Main file to run the Flask app is in the dashapp.py file
"""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
import dash
from flask.helpers import get_root_path
from flask_login import login_required
# setup Dash ###########################################################################################################
def register_dashapps(app):
# get the Dash app files
from flaskapp.dashapp_searchtool.layout import layout
from flaskapp.dashapp_searchtool.callbacks import register_callbacks
# Meta tags for viewport responsiveness
meta_viewport = {"name": "viewport", "content": "width=device-width, initial-scale=1, shrink-to-fit=no"}
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# base url, needed for login_requiered this url needs to end with / in dash
base_url = '/searchtool/'
# initialize dash app and bind server=app to the flask server
dashapp_searchtool = dash.Dash(__name__,
server=app,
url_base_pathname=base_url,
assets_folder=get_root_path(__name__) + r'\dashapp_searchtool\assets',
meta_tags=[meta_viewport],
external_stylesheets=external_stylesheets)
# set up the dash app layout and content
with app.app_context():
dashapp_searchtool.title = 'Search Tool'
dashapp_searchtool.layout = layout
register_callbacks(dashapp_searchtool)
# protect the dash app url, to only be called when logged in
_protect_dashviews(dashapp_searchtool, base_url)
# lock the dash app, only works when user login is is given.
# WORKAROUND because the in the example given version with dashapp.url_base_pathname doesnt work
def _protect_dashviews(dashapp, base_url):
for view_func in dashapp.server.view_functions:
if view_func.startswith(base_url):
dashapp.server.view_functions[view_func] = login_required(dashapp.server.view_functions[view_func])
# Setup Flask and Databse connection ###################################################################################
app = Flask(__name__)
#secret key good for anything with authentifictations...
app.config['SECRET_KEY'] = 'xxx'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
# init SQLAlchemy so we can use it later in our models with . db
db = SQLAlchemy(app)
# Set up Dash connection ###############################################################################################
register_dashapps(app)
# handle user login pre setup from the flask_login package #############################################################
login_manager = LoginManager()
# If someone is not logged in redirect to login
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .database_models import User
# uses cookie to save logged in user with a user_id and can recheck if user is in database
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app for login, logout and sing in
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app for profile and main
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
dashapp.py
# Run this file to run Dash App with entire flask login project
from flaskapp import app
server = app
if __name__ == '__main__':
server.run(debug=True)
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="FlaskH2" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\ProgramData\Anaconda3\envs\dash_search_app\python.exe|C:\ProgramData\Anaconda3\envs\dash_search_app\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
Locally, If I do python dashapp.py everything works fine. But by accesing the binded URL I am getting following errors:
Error occurred while reading WSGI handler: Traceback (most recent call last): File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 791, in main env,
handler = read_wsgi_handler(response.physical_path) File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 633,
in read_wsgi_handler handler = get_wsgi_handler(os.getenv("WSGI_HANDLER")) File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 616,
in get_wsgi_handler raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb)) ValueError: "dashapp.server" could not be imported: Traceback
(most recent call last): File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\wfastcgi.py", line 600,
in get_wsgi_handler handler = __import__(module_name, fromlist=[name_list[0][0]]) File ".\dashapp.py", line 3, in from flaskapp import app File ".\flaskapp\__init__.py",
line 67, in register_dashapps(app) File ".\flaskapp\__init__.py", line 19, in register_dashapps from flaskapp.dashapp_searchtool.layout import layout File ".\flaskapp\dashapp_searchtool\layout.py", line 30,
in import pandas as pd File "C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\pandas\__init__.py", line 17, in "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies: numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy c-extensions failed. -
Try uninstalling and reinstalling numpy. - If you have already done that, then: 1. Check that you expected to use Python3.7 from "C:\ProgramData\Anaconda3\envs\dash_search_app\python.exe",
and that you have no directories in your PATH or PYTHONPATH that can interfere with the Python and numpy version "1.17.3" you're trying to use. 2. If (1) looks fine,
you can open a new issue at https://github.com/numpy/numpy/issues. Please include details on: - how you installed Python
- how you installed numpy - your operating system - whether or not you have multiple versions of Python installed - if you built from source, your compiler versions and
ideally a build log - If you're working with a numpy git repository, try `git clean -xdf` (removes all files not under version control) and rebuild numpy. Note:
this error has many possible causes, so please don't comment on an existing issue about this - open a new one instead. Original error was: DLL load failed:
The specified module could not be found. StdOut: StdErr: C:\ProgramData\Anaconda3\envs\dash_search_app\lib\site-packages\flask_sqlalchemy\__init__.py:794:
FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
回答1:
I just found out that Python 3.7 is not compatible with wfastcgi.
Down grading the environment tp Pyython 3.6 was the solution.
来源:https://stackoverflow.com/questions/58980691/cant-get-dash-app-run-on-iis-with-flask-server