Python网页应用开发神器Dash 2.18.1稳定版本来啦

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

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

大家好我是费老师,上周Dash发布了2.18.0新版本,并于今天发布了可稳定 使用的2.18.1版本(自古.1版本最稳✌),今天的文章中就将针对2.18.1稳定版本中已修复的问题及调整的内容做简要介绍。

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

bash 复制代码
pip install dash -U

1 修复了回调返回单个no_update进行批量控制不生效的问题

2.18.0之前的版本中,针对编排了多个Output角色的回调函数,若我们希望在某些条件 分支下,取消 本次回调触发对所有 Output角色的更新 ,常用的方式是直接return dash.no_update,这里的单个dash.no_update就可以直接快捷概括对所有Output的不更新。

举个简单的例子,我们通过按钮的点击来触发3个不同目标内容的更新,且当点击次数为偶数时取消更新 ,在之前的2.18.0版本中,这个快捷写法会触发下图所示错误:

2.18.1版本中则对此问题进行了修复,可以看到功能正常了,即只有点击次数为奇数时才更新内容:

本例子完整代码:

app1.py

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

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        fac.AntdSpace(
            [
                f"Dash版本: {dash.__version__}",
                fac.AntdButton("点我试试", id="demo-button", type="primary"),
                fac.AntdText(id="demo-output1"),
                fac.AntdText(id="demo-output2"),
                fac.AntdText(id="demo-output3"),
            ],
            direction="vertical",
            align="center",
        )
    ],
    style=style(padding=50),
)


@app.callback(
    [Output(f"demo-output{i}", "children") for i in range(1, 4)],
    Input("demo-button", "nClicks"),
    prevent_initial_call=True,
)
def demo_callback(nClicks):
    # 仅在nClicks为奇数时触发
    if nClicks % 2 == 1:
        return (
            f"nClicks: {nClicks}",
            f"nClicks x 2: {nClicks*2}",
            f"nClicks x 3: {nClicks*3}",
        )

    # 不更新任何内容
    return dash.no_update


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

2 修复了全局/局部回调函数错误处理机制+字典化角色编排时的异常

Dash 2.18版本新特性介绍一文中我们介绍了Dash2.18.0开始新增的全局/局部回调错误处理机制 ,但此特性在结合回调函数字典化角色编排时,会功能异常,譬如我们将上面例子中的回调函数改造为字典化编排的形式:

python 复制代码
# 这里on_error简单写个匿名函数示意
app = dash.Dash(__name__, on_error=lambda e: print(e))

...

@app.callback(
    output=dict(
        demo_output1=Output("demo-output1", "children"),
        demo_output2=Output("demo-output2", "children"),
        demo_output3=Output("demo-output3", "children"),
    ),
    inputs=dict(nClicks=Input("demo-button", "nClicks")),
    prevent_initial_call=True,
)
def demo_callback(nClicks):
    # 仅在nClicks为奇数时触发
    if nClicks % 2 == 1:
        return dict(
            demo_output1=f"nClicks: {nClicks}",
            demo_output2=f"nClicks x 2: {nClicks*2}",
            demo_output3=f"nClicks x 3: {nClicks*3}",
        )

    # 故意触发错误
    raise Exception("自定义错误")

在之前的2.18.0版本中,错误处理遇上字典化角色编排就会出现多余的错误:

而在2.18.1中,此问题得到了有效修复,可以看到,示例中正常捕获到了错误:

3 开始弃用旧的run_server()方法

Dash早期启动应用的方式为app.run_server(),后面新增了更推荐的app.run()方式。而从2.18.1版本开始,正式将app.run_server()标记为废弃方法 ,并将在未来的Dash3.0版本中正式移除此方法,大家统一换成app.run()即可。

除此之外,此次版本更新中还对dash.Dash()中的pluginslong_callback_manager参数标记为废弃,完整的更新内容说明请移步https://github.com/plotly/dash/releases/tag/v2.18.1


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

相关推荐
数据智能老司机1 天前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python
这里有鱼汤1 天前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook1 天前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室1 天前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三2 天前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试