'Can you embed a YouTube video in a Jupyter Notebook?

I am trying to embed a YouTube video into an existing Jupyter notebook, using the embed code and an iframe. I have seen a few other solutions where they use a code cell which is then executed - but this is not desirable, as outlined below.

I would like to embed the iframe directly into a markdown cell in the notebooks. This way we can re-use the same notebook markdown with a variety of kernels without having to change the code cells.

Do you know if this is possible or not? Does it require installing any additional tools (e.g. Node.js)?


For a minimal example, suppose I wanted to embed this code in a markdown cell:

<iframe width="560" height="315" src="https://www.youtube.com/embed/jZ952vChhuI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

If I include it in a markdown cell as follows:

# My Markdown Video

This is where the video should go:

<iframe width="560" height="315" src="https://www.youtube.com/embed/jZ952vChhuI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>```

But it's not here.

However, when this markdown cell is rendered (run) the iframe does not show up; it's just a blank. It will display if you try to print the notebook, but it's not interactive.



Solution 1:[1]

Not in markdown but you can use %%html at the top of a code block that doesn't mind what kernel type you're using to embed the video:

%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/jZ952vChhuI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mids