A lightweight, privacy-focused Python package for embedding YouTube videos in Jupyter Notebooks
ytnb-embed provides a simple and elegant way to embed YouTube videos directly in Jupyter Notebooks. Built with privacy and simplicity in mind, it uses YouTube's privacy-enhanced mode (youtube-nocookie.com) to protect user privacy while maintaining full video functionality.
Perfect for data scientists, educators, and researchers who want to enrich their notebooks with video content without compromising on privacy or simplicity.
- 🎥 One-line embedding - Embed any YouTube video with a single function call
- 🔒 Privacy-first - Uses
youtube-nocookie.comfor enhanced privacy protection - 📐 Fully customizable - Adjust video player dimensions to fit your needs
- 🛡️ Smart URL parsing - Handles all YouTube URL formats automatically
- 📝 Built-in logging - Comprehensive logging for debugging and monitoring
- ✅ Type hints - Full type annotation support for better IDE integration
- 🧪 Well tested - Includes unit and integration tests
- 🚀 Lightweight - Minimal dependencies, maximum performance
Install ytnb-embed using pip:
pip install ytnb-embed- Python 3.10+
- Jupyter Notebook or JupyterLab
from ytnb_embed import embed_yt
# Embed a YouTube video with default dimensions (780x440)
embed_yt("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
# Customize the video player size
embed_yt("https://www.youtube.com/watch?v=dQw4w9WgXcQ", width=640, height=360)That's it! The video will be embedded directly in your Jupyter Notebook cell output.
ytnb-embed intelligently parses all standard YouTube URL formats:
from ytnb_embed import embed_yt
# Standard watch URL
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID")
# Short URL
embed_yt("https://youtu.be/VIDEO_ID")
# Embed URL
embed_yt("https://www.youtube.com/embed/VIDEO_ID")
# Direct video URL
embed_yt("https://www.youtube.com/v/VIDEO_ID")
# URLs with timestamps and other parameters
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID&t=30s")Tailor the video player size to your notebook layout:
# Large player for presentations
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=1024, height=576)
# Standard 16:9 aspect ratio
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=854, height=480)
# Compact player for documentation
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=560, height=315)Here's a complete example demonstrating common use cases:
from ytnb_embed import embed_yt
# Educational content
print("📚 Python Tutorial:")
embed_yt("https://www.youtube.com/watch?v=_uQrJ0TkZlc", width=800, height=450)
# Conference talks
print("\n🎤 Tech Talk:")
embed_yt("https://youtu.be/cKPlPJyQrt4", width=900, height=506)
# Quick demos
print("\n⚡ Quick Demo:")
embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=640, height=360)Embeds a YouTube video in a Jupyter Notebook using an iframe.
| Parameter | Type | Default | Description |
|---|---|---|---|
url |
str |
required | YouTube video URL in any standard format |
width |
int |
780 |
Width of the video player in pixels |
height |
int |
440 |
Height of the video player in pixels |
str: Returns"success"when the video is successfully embedded
InvalidURLException: Raised when the provided URL is not a valid YouTube URLException: Raised for other unexpected errors during embedding
result = embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=800, height=450)
# result == "success"ytnb-embed uses YouTube's privacy-enhanced mode by default. Videos are embedded from youtube-nocookie.com, which provides:
- Reduced tracking - YouTube doesn't store information about visitors unless they play the video
- No third-party cookies - Cookies are only set when the user interacts with the video
- Same functionality - All standard YouTube features remain available
- URL validation - Regex-based validation prevents injection attacks
- Sandboxed iframe - Videos load in isolated iframe contexts
- Strict referrer policy -
strict-origin-when-cross-originprevents data leakage
Contributions are welcome and greatly appreciated! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Make your changes
- Add tests for new functionality
- Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Write clear, descriptive commit messages
- Add tests for any new features or bug fixes
- Update documentation as needed
- Follow the existing code style (PEP 8)
- Ensure all tests pass before submitting PR
This project is licensed under the MIT License - see the LICENSE file for full details.
- GitHub: @aatansen
- Email: aatansen@gmail.com
- PyPI: ytnb-embed
- Built for the Jupyter Notebook community
- Inspired by the need for simple, privacy-focused video embedding
- Thanks to all contributors who help improve this package
If you encounter any issues or have questions:
- 🐛 Bug reports: GitHub Issues
- 💡 Feature requests: GitHub Issues
- 📧 Email: aatansen@gmail.com
Made with ❤️ for the Jupyter community
If you find this package helpful, please consider giving it a ⭐ on GitHub!