1

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.

  • You've already seen https://stackoverflow.com/a/29862668/8508004 and https://stackoverflow.com/a/69363841/8508004 ? – Wayne Apr 26 '22 at 19:22

1 Answers1

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>
mids
  • 356
  • 1
  • 4
  • 15
  • This is OK, but it will only work with kernels that support the ``%%html`` magic (like the IPython kernel), which was the issue I had with the code blocks; thanks for the suggestion though. – Jonathan Graves Apr 26 '22 at 22:56
  • When I add this HTML to a markdown cell, this `iframe` works without the magic command. Thanks for the gist. – Dan Kowalczyk Dec 02 '22 at 18:32