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学习"加入我们的技术交流群,一起成长一起进步。

相关推荐
nothing&nowhere7 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田7 小时前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang7 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
飞猪~11 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_9563198812 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python
开飞机的舒克_13 小时前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi
霸道流氓气质13 小时前
Kiro 中反编译 JAR 包并分析字节码的流程指南
chrome·python·jar
人工智能时代 准备好了吗14 小时前
AI回答内容进入率监测:引用识别、文本匹配与语义判断
开发语言·人工智能·python
Metaphor69214 小时前
使用 Python 冻结 Excel 文件中的行、列和单元格
开发语言·python·excel