You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dash Bootstrap Components标签页中Iframe加载气象雷达URL异常问题

气象雷达URL在Iframe中加载异常的问题排查与解决建议

我通过Callback生成带最近5分钟取整时间戳的气象雷达URL,示例如下:
https://www.metoffice.gov.uk/weather/maps-and-charts/rainfall-radar-forecast-map#?bbox=[[50.546,-2.200],[52.140,2.944]]&model=ukmo-ukv&layer=rainfall-rate&timestep=1680109809000

该URL直接在浏览器打开正常,但嵌入Iframe时会报错,错误指向timestep参数,且这个问题时好时坏。

Iframe相关代码

dbc.Tab([
    dbc.Row([
        dbc.Col([
            html.H1(children='Data is usually updated every 2 hours', style=style_2),
        ], width=2, style={'height': '100vh'}),
        dbc.Col([
            html.Br(), html.H1(children='Weather Forecast.', style=style_1),
            html.H1(children='Current radar', style=style_2),
            html.Iframe(id='radar',src=rain_radar, style={"height": "600px", "width": "100%"}),
            dcc.Graph(id='forecast_rain_prob', figure=fig_forecast_rain_prob, config=config),
            html.Br(),
            dcc.Graph(id='forecast_daily_rain_prob', figure=fig_daily_rain_prob, config=config),
            html.Br(),
            dcc.Graph(id='forecast_temperature', figure=fig_tempreture, config=config),
            html.Br(),
            dcc.Graph(id='forecast_cloud', figure=fig_cloud, config=config),
            html.Br(),
            html.Br(),
            dcc.Graph(id='wind_speed', figure=fig_wind, config=config),
            dcc.Graph(id='wind_direction', figure=fig_wind_direction, config=config),
            dcc.Interval(id="forecast_trigger", interval=1000 * 60 * 60 * 2),  # milliseconds
        ])
    ])
], label="Weather forecast",tab_id='tab-7'),  # end tab

对应的Callback代码

@app.callback([Output('forecast_rain_prob', 'figure'), #forecast tab
            Output('forecast_temperature', 'figure'),
            Output('wind_speed', 'figure'),
            Output('wind_direction', 'figure'),
            Output('radar', 'src'),
            Output('forecast_cloud','figure'),
            Output('forecast_daily_rain_prob','figure')
           ],
          Input('forecast_trigger', 'n_intervals'))
def update_forecast(n):
    fig_forecast_rain_prob,fig_tempreture,fig_wind,fig_wind_direction,fig_river_forcast,  fig_cloud, rain_radar, fig_daily_rain, df_daily_forecast = forecast.get_open_meteo_forecast()
    update_time = datetime.now()

    return fig_forecast_rain_prob,fig_tempreture,fig_wind,fig_wind_direction, rain_radar, fig_cloud,fig_daily_rain

解决建议

  • 时间戳精度校验:检查get_open_meteo_forecast生成的timestep是否严格为5分钟整倍数的毫秒级时间戳。比如当前时间是10:07,取整到10:05,对应的毫秒戳必须准确,避免出现非整5分钟的时间点,导致Met Office接口在Iframe环境下校验不通过。
  • URL编码处理:Iframe加载时可能对URL中的特殊字符(如[])解析存在差异。生成rain_radar时,对URL参数做URL编码,尤其是bbox和timestep部分,确保参数传递准确。
  • 缓存强制刷新:问题时好时坏可能和缓存有关。在Callback返回rain_radar时,给URL添加随机参数(比如&cache_bust={随机数}),强制浏览器不缓存Iframe内容,确保每次加载都是最新的有效时间戳。
  • 跨域头检查:Met Office的页面可能动态设置X-Frame-Options,有时允许嵌入有时不允许。通过浏览器开发者工具查看Network请求的响应头,确认是否存在X-Frame-Options: DENY或SAMEORIGIN。如果是跨域问题,可以尝试用代理服务器转发请求,或检查Met Office是否提供官方嵌入API。
  • 时间戳节点适配:dcc.Interval触发时可能刚好处于Met Office数据更新间隙,生成的时间戳对应的雷达数据尚未上线。可以调整时间戳生成逻辑,向后取整到下一个5分钟节点,确保对应数据已存在。

内容的提问来源于stack exchange,提问作者A Rob4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 08:15:24