Jupyter/Colab:在任何单元中播放有任何错误的声音+在完成长时间运行的单元后播放声音 [英] Jupyter / Colab : Play sound with any error in any cell + Play sound after completing long running cells

查看:112
本文介绍了Jupyter/Colab:在任何单元中播放有任何错误的声音+在完成长时间运行的单元后播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时会发生错误,但是我没有注意到,因为我可能一次运行多个单元. 我想播放声音时出错.

Sometimes an error happens but I don't notice because I may be running multiple cells at one. I'd like errors to play a sound.

许多人希望长时间运行的单元在完成后可以播放声音.

Many people would like long running cells to play a sound when completed.

推荐答案

我混合了在某些地方找到的解决方案 2 3 4

I mixed the solutions I found in some places 1 2 3 4

a)创建一个在错误时发出哔哔声的全局异常处理程序

a) Create a global exception handler that beeps on errors

b)创建一个简单的函数,将其放置在长时间运行的单元格的末尾(链接上的某些其他方法)

b) Create a simple function that you place at the end of the long-running cell (some other approaches on links)

您可以将声音更改为您喜欢的任何声音.

You can change the sounds to anything you like.

注意:异常处理程序和beep_completed()内的声音是非常不同的,并且是有原因的.第一个是短暂而又不烦人的,第二个是漫长而令人愉快的(如果您不在计算机旁,则可以清楚地听到任务已完成).无论如何,您都可以更换它们.

Note: The sounds inside the Exception Handler and the beep_completed() are very different and with reason. The first is short and non-annoying and the second is long and pleasant (in case you are away from computer so you clearly hear that the task is completed). In any case you can replace them.

注意:有一行仅适用于Colab.如果您可以为Jupyter提供一个,我会很乐意更新答案.

Note: There is a line that only applies to Colab. If you can provide the one for Jupyter I will gladly update the answer.

# This line is specific for Colab  (please provide alternative for Jupyter)
from google.colab import output

from IPython.core.ultratb import AutoFormattedTB

# Catch any Exception, play error sound and re-raise the Exception
#-------------------------------------------------
# initialize the formatter for making the tracebacks into strings
itb = AutoFormattedTB(mode = 'Plain', tb_offset = 1)

# this function will be called on exceptions in any cell
def custom_exc(shell, etype, evalue, tb, tb_offset=None):

    # still show the error within the notebook, don't just swallow it
    shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)


    # Play an audio beep. Any audio URL will do.  
    output.eval_js('new Audio("http://soundbible.com/grab.php?id=419&type=wav").play()')

    # # grab the traceback and make it into a list of strings
    # stb = itb.structured_traceback(etype, evalue, tb)
    # sstb = itb.stb2text(stb)

    # print (sstb) # <--- this is the variable with the traceback string
    # print ("sending mail")
    # send_mail_to_myself(sstb)

# this registers a custom exception handler for the whole current notebook
get_ipython().set_custom_exc((Exception,), custom_exc)
#------------------------------------------


# Function to play a sound (to put at the end of a long job)
def beep_completed():
  #url_sound="http://soundbible.com/grab.php?id=1795&type=mp3";
  output.eval_js('new Audio("http://soundbible.com/grab.php?id=1795&type=mp3").play()')

# Just play it with
beep_completed()

这篇关于Jupyter/Colab:在任何单元中播放有任何错误的声音+在完成长时间运行的单元后播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆