无法使用烧瓶服务器在 IIS 上运行达世币应用程序



我的IIS(Windows Server 2016(上有两个网站(都在使用Dash&Flask(。

第一个是一个最小的工作示例,由app.py和web.config组成。因为不知何故,我无法让第二个站点工作。下面是两个示例,并附上错误消息。

1 工作示例

FastCGI设置:

  • 巨蟒路径: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:ProgramDataAnaconda3envsmvppython.exe|C:ProgramDataAnaconda3envsmvpLibsite-packageswfastcgi.py" resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>

2失败示例

FastCGI设置:

  • 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_searchtoolassets',
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:ProgramDataAnaconda3envsdash_search_apppython.exe|C:ProgramDataAnaconda3envsdash_search_appLibsite-packageswfastcgi.py" resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>

在本地,如果我做python dashab.py,一切都很好。但是通过访问绑定的URL,我得到了以下错误:

Error occurred while reading WSGI handler: Traceback (most recent call last): File "C:ProgramDataAnaconda3envsdash_search_applibsite-packageswfastcgi.py", line 791, in main env, 
handler = read_wsgi_handler(response.physical_path) File "C:ProgramDataAnaconda3envsdash_search_applibsite-packageswfastcgi.py", line 633,
in read_wsgi_handler handler = get_wsgi_handler(os.getenv("WSGI_HANDLER")) File "C:ProgramDataAnaconda3envsdash_search_applibsite-packageswfastcgi.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:ProgramDataAnaconda3envsdash_search_applibsite-packageswfastcgi.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 ".flaskappdashapp_searchtoollayout.py", line 30,
in import pandas as pd File "C:ProgramDataAnaconda3envsdash_search_applibsite-packagespandas__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:ProgramDataAnaconda3envsdash_search_apppython.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:ProgramDataAnaconda3envsdash_search_applibsite-packagesflask_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.

我刚刚发现Python 3.7与wfastcgi不兼容。

降低环境等级tp Pyython 3.6是解决方案。

我通过授予IIS站点访问numpy所依赖的dll的权限找到了一个解决方案。适用于python 3.8(可能也是3.7(。我的解决方案张贴在这里

使用Numpy、Pandas等在Python 3.7+中使用wfastcgi在IIS上部署Python Flask应用程序

相关内容

最新更新