Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Created May 5, 2020 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmyersturnbull/e613b25465293d3cbdbc7635e6450a24 to your computer and use it in GitHub Desktop.
Save dmyersturnbull/e613b25465293d3cbdbc7635e6450a24 to your computer and use it in GitHub Desktop.
Audio container in Jupyter notebook
from typing import Union
from pathlib import PurePath
def listen(path: Union[str, PurePath, bytes]):
"""
Returns an audio container that Jupyter notebook will display.
Must be run from a Jupyter notebook.
Will raise an ImportError if IPython cannot be imported.
:param path: The local path to the audio file
:return: A jupyter notebook ipd.Audio object
"""
# noinspection PyPackageRequirements
import IPython.display as ipd
if isinstance(path, (str, PurePath)):
path = str(Path(path))
return ipd.Audio(filename=path)
else:
return ipd.Audio(data=path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment