Deploying a Plotly/Dash app to AWS using Serverless Framework(使用无服务器框架将Ploly/Dash应用部署到AWS)
本文介绍了使用无服务器框架将Ploly/Dash应用部署到AWS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用无服务器框架将Ploly Dash应用程序部署为AWS Lambda。该应用程序在本地运行正常,我可以使用serverless wsgi serve
命令启动它。serverless deploy
报告成功。但是,调用lambda时会失败,并显示以下错误:
Traceback (most recent call last):
File "/var/task/wsgi_handler.py", line 44, in import_app
wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])
File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/app.py", line 4, in <module>
import dash
File "/tmp/sls-py-req/dash/__init__.py", line 5, in <module>
from .dash import Dash, no_update # noqa: F401,E402
File "/tmp/sls-py-req/dash/dash.py", line 21, in <module>
from flask_compress import Compress
File "/tmp/sls-py-req/flask_compress.py", line 14, in <module>
import brotli
File "/tmp/sls-py-req/brotli.py", line 8, in <module>
import _brotli
ModuleNotFoundError: No module named '_brotli'
[ERROR] Exception: Unable to import app.server
Traceback (most recent call last):
File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/wsgi_handler.py", line 119, in <module>
wsgi_app = import_app(config)
File "/var/task/wsgi_handler.py", line 49, in import_app
raise Exception("Unable to import
{}
".format(config["app"]))
app.py
import dash
import dash_html_components as html
from flask import Flask
server = Flask(__name__)
@server.route("/")
def index():
return "Hello Flask app"
app = dash.Dash(
__name__,
server=server,
routes_pathname_prefix='/dash/'
)
app.layout = html.Div(html.H1("Hello Dash!"))
if __name__ == "__main__":
server.run(debug=True)
serverless.yml
---
service: dash-serverless
variablesResolutionMode: 20210219
useDotenv: true
provider:
name: aws
runtime: python3.8
stage: test
region: eu-central-1
apiGateway:
shouldStartNameWithService: true
lambdaHashingVersion: 20201221
functions:
app:
handler: wsgi_handler.handler
events:
- http: ANY /
- http: "ANY {proxy+}"
custom:
wsgi:
app: app.server
pythonBin: python3
packRequirements: false
pythonRequirements:
dockerPip: non-linux
plugins:
- serverless-wsgi
- serverless-python-requirements
package:
exclude:
- node_modules/**
# and other
Requirments.txt
-i https://pypi.org/simple
brotli==1.0.9
click==7.1.2; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
dash-core-components==1.15.0
dash-html-components==1.1.2
dash-renderer==1.9.0
dash-table==4.11.2
dash==1.19.0
flask-compress==1.9.0
flask==1.1.2
future==0.18.2; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
itsdangerous==1.1.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
jinja2==2.11.3; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
markupsafe==1.1.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
plotly==4.14.3
retrying==1.3.3
six==1.15.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
werkzeug==1.0.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
推荐答案
ModuleNotFoundError: No module named '_brotli'
的原因是依赖项打包不正确。通过使用docker和docker-lambda镜像打包应用程序,修复了该问题。slim: true
和strip: false
最小化包大小,同时保留在某些情况下需要的二进制文件(在本例中确实需要)。
pythonRequirements:
dockerizePip: true
slim: true
strip: false
解决了_brotli未找到的问题后,我遇到了下一个错误消息pkg_resources.DistributionNotFound: The 'flask-compress' distribution was not found and is required by the application
。我能够使用Pyinstaller executable cannot find 'flask-compress' distribution that is included中描述的解决方法解决此问题。
最后,应用程序在/<api-stage-name>/
(本例中为/test/
)下提供服务。这需要修改Dash应用程序配置,即
在Dash构造函数中提供requests_pathname_prefix="/test/dash/"
。
完整工作示例:
app.py
from collections import namedtuple
import pkg_resources
# backup true function
_true_get_distribution = pkg_resources.get_distribution
# create small placeholder for the dash call
# _flask_compress_version = parse_version(get_distribution("flask-compress").version)
_Dist = namedtuple('_Dist', ['version'])
def _get_distribution(dist):
if dist == 'flask-compress':
return _Dist('1.9.0'). # your flask-compress version
else:
return _true_get_distribution(dist)
# monkey patch the function so it can work once frozen and pkg_resources is of
# no help
pkg_resources.get_distribution = _get_distribution
import dash
import dash_html_components as html
from flask import Flask
server = Flask(__name__)
@server.route("/")
def index():
return "Hello Flask app"
app = dash.Dash(
__name__,
server=server,
routes_pathname_prefix="/dash/",
requests_pathname_prefix="/test/dash/"
)
app.layout = html.Div(html.H1("Hello Dash!"))
if __name__ == "__main__":
server.run(debug=True)
serverless.yml
---
service: dash-serverless
variablesResolutionMode: 20210219
useDotenv: true
provider:
name: aws
runtime: python3.8
stage: test
region: eu-central-1
apiGateway:
shouldStartNameWithService: true
lambdaHashingVersion: 20201221
functions:
app:
handler: wsgi_handler.handler
events:
- http: ANY /
- http: "ANY {proxy+}"
custom:
wsgi:
app: app.server
pythonBin: python3
packRequirements: false
pythonRequirements:
dockerizePip: true
slim: true
strip: false
plugins:
- serverless-wsgi
- serverless-python-requirements
package:
exclude:
- node_modules/**
# and other
这篇关于使用无服务器框架将Ploly/Dash应用部署到AWS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用无服务器框架将Ploly/Dash应用部署到AWS
基础教程推荐
猜你喜欢
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01