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
相关产品推荐
相关产品推荐

