You need to enable JavaScript to run this app. VS Code中是否有方法在.ipynb文件单元格执行完成后通知用户?
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中是否有方法在.ipynb文件单元格执行完成后通知用户?

VS Code Jupyter 单元格执行完成通知实现方法

1. 官方Jupyter扩展内置设置(推荐)

微软官方的Jupyter扩展原生支持该功能,无需额外安装其他扩展,设置步骤如下:

  • 打开VS Code设置界面:快捷键为Ctrl+,(Windows/Linux)或Cmd+,(MacOS)
  • 在顶部搜索框输入Jupyter: Notify On Cell Complete,勾选对应选项即可开启全局通知,单元格无论执行成功还是失败都会触发提醒
  • 若需要避免短运行任务频繁触发通知,可搜索Jupyter: Cell Execution Time Threshold For Notification,填入自定义毫秒数,只有运行时长超过该阈值的单元格执行完成后才会发送通知

2. 代码层面自定义实现

如果需要定制通知内容(比如携带运行结果、耗时等信息),可以在单元格末尾添加对应系统的通知触发代码,示例如下:

import os
import platform
import time

# 你的业务代码示例
time.sleep(5)
run_result = "计算完成,结果为12345"

# 触发系统通知
sys_type = platform.system()
if sys_type == "Darwin":
    # MacOS 系统通知
    os.system(f'''osascript -e 'display notification "{run_result}" with title "Jupyter单元格运行结束"' ''')
elif sys_type == "Windows":
    # Windows 系统通知(首次使用需以管理员身份运行PowerShell执行 `Install-Module -Name BurntToast` 安装依赖)
    os.system(f'PowerShell -Command "New-BurntToastNotification -Text \"Jupyter单元格运行结束\", \"{run_result}\""')
elif sys_type == "Linux":
    # Linux 系统通知
    os.system(f'notify-send "Jupyter单元格运行结束" "{run_result}"')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 02:15:07