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

如何在运行SQL查询的Dash回调中使用dcc.Loading组件

Dash长耗时SQL回调中dcc.Loading配置方案

dcc.Loading是Dash内置的加载状态组件,不需要额外编写回调控制显示/隐藏:只要它包裹的子组件对应的回调处于执行未完成状态,就会自动展示加载动画,回调返回结果后自动隐藏,完全覆盖SQL查询、数据处理、图表渲染全流程的等待阶段。


配置步骤

  1. 先修正原代码的基础笔误
    • 原回调Output("map". "figure")存在语法错误,目标组件id是布局里的map1,应改为Output("map1", "figure")
    • 原代码中propname、地图中心点x/y、zoom变量未定义,需要补充赋值
    • 原SQL用字符串直接拼接用户输入存在注入风险,建议改用pandas参数化查询写法
  2. 布局中用dcc.Loading包裹需要显示加载状态的输出组件
    只需要把长耗时回调对应的输出组件(也就是你的dcc.Graph(id="map1"))放到dcc.Loading的子节点里即可,不要把输入组件(搜索框)放进Loading内部,避免输入操作触发无意义的加载动画。
  3. 根据需求调整Loading参数
    常用配置项:
    • type:加载动画样式,可选值为graph/cube/circle/dot,长耗时任务推荐用circle或dot辨识度更高
    • color:加载动画的主题色,和页面UI配色匹配即可
    • fullscreen:布尔值,设为True时加载动画会覆盖整个页面,避免用户在查询期间重复操作输入框,适合秒级以上的长查询场景
    • delay_show:单位毫秒,设置延迟显示加载动画的时间,比如设为500就是查询耗时超过500ms才出加载框,避免极快响应时动画闪烁
    • delay_hide:单位毫秒,设置结果返回后延迟隐藏动画的时间,避免渲染跳变

修正后的完整示例代码

import dash
from dash.dependencies import Input, Output
from dash import dcc, html
import pandas as pd
import pymysql
from sqlalchemy import create_engine
import dash_bootstrap_components as dbc
from app import app

# Mapbox配置
MAPBOX_KEY="pk.xxxxxxx"

# 数据库连接(全局初始化连接即可,查询逻辑必须放在回调内)
user = 'user1'
pwd = 'xxxx'
host =  'xasdas'
port = 3306
database = 'main'
engine = create_engine("mysql+pymysql://{}:{}@{}/{}".format(user,pwd,host,database))

# 布局部分
layout = html.Div([ 
    dcc.Input(id="selection", type="search", list="list1"),
    # 用Loading包裹目标图表组件
    dcc.Loading(
        id="loading-map",
        type="circle",
        color="#1f77b4",
        fullscreen=False, # 长查询可以改成True开全屏遮罩
        delay_show=300,
        children=[
            dcc.Graph(id="map1")
        ]
    )
])

# 回调部分,修正原Output的语法错误
@app.callback(
    Output("map1", "figure"),
    Input("selection", "value")
)
def update_map(selection):
    data = []
    # 初始化默认地图布局
    fig_layout = {
        "autosize": True,
        "hovermode": "closest",
        "mapbox": {
            "accesstoken": MAPBOX_KEY,
            "bearing": 0,
            "center": {"lat": 39.9, "lon": 116.4}, # 替换成你的默认中心点
            "pitch": 0,
            "zoom": 10, # 替换成你的默认缩放等级
            "style": "streets",
        },
        "margin": {"r": 0, "t": 0, "l": 0, "b": 0, "pad": 0}
    }

    if selection:
        # 改用参数化查询避免SQL注入
        query = "select x, y, z, propname from table1 where val = %s"
        df = pd.read_sql(query, con=engine, params=(selection,))

        if not df.empty:
            # 自动计算地图中心点,不用硬写
            center_lat = df['x'].mean()
            center_lon = df['y'].mean()
            fig_layout["mapbox"]["center"] = {"lat": center_lat, "lon": center_lon}

            data.append({
                "type": "scattermapbox",
                "lat": df['x'],
                "lon": df['y'],
                "name": "Location",
                "hovertext": df['propname'], # 对应SQL查出来的字段
                "mode": "markers",
                "clickmode": "event+select",
                "marker": {
                    "autocolorscale": False,
                    "showscale":True,
                    "symbol": "circle",
                    "size": 9,
                    "opacity": 0.8,
                    "color": df['z'],
                }
            })

    return {"data": data, "layout": fig_layout}

额外优化建议

  • 如果查询耗时普遍超过3秒,建议配合文字提示"数据查询中,请稍候...",降低用户等待焦虑
  • 数据库连接不要在每次回调里重复创建,全局初始化连接池即可,减少额外耗时
  • 可以给SQL查询的过滤字段加索引,从根源缩短等待时间

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 12:18:14