How To Display Rich Output Media ( Audio, Video, Image etc) In IPython Jupyter Notebook

IPython.display package provides a lot of classes for you to display rich media object in jupyter notebook directly and immediately.Using classes in the display package, you can display audio (local or remote ), video(youtube or local ), image, local file links, html data in jupyter notebook. This article will show you some examples.

1. Display Audio MP3 File In Jupyter Notebook.

IPython.display.Audio class is used to display audio file in jupyter notebook. Audio class constructor has below parameters to specify the audio file path.

IPython.display.Audio(data=None, filename=None, url=None, embed=None, rate=None, autoplay=False) : filename is the local audio file path and name. url is the remote audio file url path.

  1. Input below source code in jupyter notebook line cell. It will display an audio widget in jupyter notebook web page which will play a local mp3 file. But you should make sure the mp3 file is saved in the same directory with the jupyter notebook file ( .pynb file).
    # import IPython.display.Audio class.
    from IPython.display import Audio
    
    # create Audio objcet with local mp3 file.
    audio = Audio(filename='./test.mp3')
    
    # display audio object in jupyter notebook.
    display(audio)
    
    # you can also write audio variable directly to display it.
    audio
  2. Then click the run button to execute above line cell source code, you will get below picture.
    display audio mp3 file in jupyter notebook

2. Display Video Files In Jupyter Notebook.

2.1 Display Youtube Video.

IPython.display.YouTubeVideo class is used to display youtube video, you should provide the youtube video id to create the YouTubeVideo class object.

  1. Input below display youtube video python source code in line cell.
    # import IPython.display.YouTubeVideo class.
    from IPython.display import YouTubeVideo
    
    # create an instance of YouTubeVideo class with provided youtube video id.
    youtube_video = YouTubeVideo('nC3QfNiudQ4')
    
    # display youtube video
    display(youtube_video)
  2. Click run button to run above line cell source code, then you get below web page.
    display youtube video in jupyter notebook
READ :   How To Run Python 2 Code In Jupyter Notebook

2.2 Display Local Video File.

To display local video file in jupyter notebook, we should use html video tag ( <video... ), and set the base64 encoded video file content to the src attribute of html video tag ( <video ..... src='data:video/x-m4v;base64,...... ' ). So we should use IPython.display.HTML class in this example.

Input below source code in jupyter notebook line cell, please make sure the mp4 file is saved in the same directory with the jupyter notebook file.

# import IPython.display.HTML class, because this example will use html to embed a local video file to display, so need the HTML class.
from IPython.display import HTML
# import base64.b64encode, the video file need to be encoded with base64 encoding.
from base64 import b64encode

# open local mp4 video file with read permission, rb means read binary file. 
file = open("./play_video_test.mp4", "rb")

# read the file content to video variable.
video_data = file.read()

# encode the video content with base64 encoding format.
video_data_encoded = b64encode(video_data)

# create embedded video html tag text. use base64 encoded video data to replace the {0} placeholder.
video_html_tag_text = '<video controls alt="display local video in jupyter notebook" src="data:video/x-m4v;base64,{0}">'.format(video_data_encoded)

# create a IPython.display.HTML object to display above embedded video html tag. 
html = HTML(data=video_html_tag_text)

# display html object in jupyter notebook
html

When you run above source code, you will get below result. It wil display a video widget with control bar at the bottom of the video.

display local mp4 file in jupyter notebook

3. Display Image Files In Jupyter Notebook.

3.1 Display Image With Url.

IPython.display.Image class is used to display images in jupyter notebook. You can specify image url or image file path and name to tell Image class which image to display.

In below example, make sure test.png file is saved in the same directory as the jupyter notebook file.

# import IPython.display.Image class.
from IPython.display import Image

print('display image with url.')

# create an Image object with specified url.
remote_image = Image(url='http://python.org/images/python-logo.gif')

# display the remote image in jupyter notebook.
display(remote_image)

print('display a local image file.')

# create an Image object with file path and name.
local_image = Image(filename='./test.png')

# display the local image in jupyter notebook
local_image

Run above soruce code will get below result web page.

READ :   Python Parse Emails And Attachments From POP3 Server Example

display remote and local image in jupyter notebook

4. Display Html Content In Jupyter Notebook.

IPython.display.HTML class is used to display html content in jupyter notebook.

# import IPython.display.HTML class
from IPython.display import HTML

# create a string which contains html source code.
html_data = """<img src='http://python.org/images/python-logo.gif'/><br/>
       <button>Click Me</button>"""

# create a HTML object.
html = HTML(data=html_data)

# display the HTML object in jupyter notebook
html

Run above ipython souce code will display html elements in jupyter notebook web page like below.

display html content in jupyter notebook

5. Display Local File Link In Jupyter Notebook.

IPython.display.FileLink class is used to display a local file link in jupyter notebook. The path argument value is the local file path.

# import IPython.display.FileLink class
from IPython.display import FileLink

# create a IPython.display.FileLink object with provided file name and path.
file_link = FileLink(path='./hello_jupyter.ipynb')

# print the FileLink object.
file_link

Run above source code will display a url link which link to the local file in jupyter notebook web page.

display a local file link in jupyter notebook

6. Display Local File Link In Jupyter Notebook.

IPython.display.FileLinks class is used to display all the files link in provided directory.

# import IPython.display.FileLinks class.
from IPython.display import FileLinks

# create a FileLinks class object provide current directory as the argument.
file_links = FileLinks('.')

# display all the files link in current directory.
display(file_links)

Run above ipython code will display all files link in current directory which the jupyter notebook ( .ipynb ) file execute.

display all current directory local files link in jupyter notebook

References

  1. IPython Display Module
(Visited 51 times, 2 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.