Dash 2.18.2版本更新:模式匹配回调性能大提升

本文示例代码已上传至我的Github仓库:https://github.com/CNFeffery/dash-master

Gitee同步仓库地址:https://gitee.com/cnfeffery/dash-master

大家好我是费老师,今天Dash发布了2.18.2版本更新,虽然只是一次小版本更新,但其中涉及到的一些内容还是非常重要的,今天的文章中我就来为大家做相关介绍。

终端执行下列命令将Dash升级到最新版本:

bash 复制代码
pip install dash -U

模式匹配回调函数性能大幅提升

在先前的版本中,基于ALL模式匹配构建的回调函数,当涉及的组件数量非常多时,在触发回调函数时会存在明显的卡顿。

举个简单的例子,我们在页面中渲染了1000 个开关组件,通过ALL模式匹配回调,实时统计多少 开关处于打开 状态,在未更新前的2.18.1版本下,可以看到每次操作开关时,都伴随着明显的卡顿:

而更新到2.18.2之后,那叫一个丝滑🥳:

示例对应源码如下:

app.py

python 复制代码
import dash
from dash import html
import feffery_antd_components as fac
from feffery_dash_utils.style_utils import style
from dash.dependencies import Input, Output, ALL

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        f"Dash版本:{dash.__version__}",
        html.Div(
            fac.AntdSpace(
                [
                    fac.AntdSwitch(
                        id={"type": "test-switch", "index": i}, checked=False
                    )
                    for i in range(1000)
                ],
                wrap=True,
            ),
            style=style(
                height=300, overflow="auto", padding=5, border="1px solid #bfbfbf"
            ),
        ),
        fac.AntdText("已打开开关数量:0", id="test-output"),
    ],
    style=style(padding=50),
)

app.clientside_callback(
    "(checked_list) => `已打开开关数量:${checked_list.filter(Boolean).length}`",
    Output("test-output", "children"),
    Input({"type": "test-switch", "index": ALL}, "checked"),
    prevent_initial_call=True,
)

if __name__ == "__main__":
    app.run(debug=True)

除此之外,此次版本更新中还为常规回调对应的dash.ctx上下文新增了cookiesheaderspathremoteorigin等属性,完整的更新内容说明请移步https://github.com/plotly/dash/releases/tag/v2.18.2


以上就是本文的全部内容,对Dash应用开发感兴趣的朋友,欢迎添加微信号CNFeffery,备注"dash学习"加入我们的技术交流群,一起成长一起进步。

相关推荐
zone77393 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77393 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒17 小时前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习20 小时前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽1 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly1 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术1 天前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习
敏编程1 天前
一天一个Python库:virtualenv - 隔离你的Python环境,保持项目整洁
python
喝茶与编码1 天前
Python异步并发控制:asyncio.gather 与 Semaphore 协同设计解析
后端·python