Azure Functions Python v2部署Dash应用仅显示默认页面问题
问题分析与解决方案
验证根路径请求是否到达函数
- 登录Azure Portal,进入你的Function App,左侧菜单选择函数,点击对应函数(默认名称为
function_app)。 - 切换到集成标签,查看HTTP触发器的路由配置:
- 如果路由不是
/,说明根路径请求未转发到Dash函数,这是默认页面出现的核心原因。
- 如果路由不是
- 查看监测标签下的日志,确认访问
myapp.azurewebsites.net时是否有请求记录到达函数:- 无日志则说明请求未被路由到函数;有日志但返回错误,可进一步排查Dash应用配置。
修改配置让Dash应用接管根路径
1. 调整function_app.py代码
更新Dash初始化和WsgiFunctionApp配置,确保路由与静态资源路径正确:
import dash from dash import dcc, html import azure.functions as func # 初始化Dash时指定请求路径前缀,禁用本地静态资源服务适配Azure环境 dashapp = dash.Dash( __name__, requests_pathname_prefix='/', serve_locally=False ) colors = { 'background': '#111111', 'text': '#7FDBFF' } dashapp.layout = html.Div( style={'backgroundColor': colors['background']}, children=[ html.H1( children='Hello Dash', style={'textAlign': 'center', 'color': colors['text']} ), html.Div( children='Dash: A web application framework for Python.', style={'textAlign': 'center', 'color': colors['text']} ), dcc.Graph( id='Graph1', 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': 'Montréal'}, ], 'layout': { 'plot_bgcolor': colors['background'], 'paper_bgcolor': colors['background'], 'font': {'color': colors['text']} } } ) ] ) # 显式指定路由为"/",让函数接管所有根路径请求 app = func.WsgiFunctionApp( app=dashapp.server.wsgi_app, route="/", http_auth_level=func.AuthLevel.ANONYMOUS )
2. 确认host.json配置
你的host.json已设置routePrefix为空,无需修改,保留现有配置即可:
{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[3.15.0, 4.0.0)" }, "functionTimeout": "00:10:00", "extensions": { "http": { "routePrefix": "" } } }
部署后测试
重新部署修改后的代码到Azure Functions,访问myapp.azurewebsites.net即可看到Dash应用。若仍有问题,检查函数日志中的错误信息,重点排查静态资源加载或路由匹配问题。
内容的提问来源于stack exchange,提问作者Dean MacGregor
相关产品推荐
相关产品推荐

