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

相关推荐
scan7249 小时前
智能体多个工具调用
python
2401_867623989 小时前
CSS Flex布局中如何设置子元素间距_掌握gap属性的现代用法
jvm·数据库·python
即使再小的船也能远航9 小时前
【Python】安装
开发语言·python
weixin_421725269 小时前
Linux 编程语言全解析:C、C++、Python、Go、Rust 谁更强?
linux·python·go·c·编程语言
没有梦想的咸鱼185-1037-16639 小时前
AI-Python机器学习、深度学习核心技术与前沿应用及OpenClaw、Hermes自动化编程
人工智能·python·深度学习·机器学习·chatgpt·数据挖掘·数据分析
axinawang10 小时前
第3课:变量与输入
python
idingzhi10 小时前
A股量化策略日报()
python
zyk_computer10 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
weixin1997010801610 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
萌新小码农‍10 小时前
python装饰器
开发语言·前端·python