Embed youtube video inside notebook
Project description
ytnb-embed
A lightweight, privacy-focused Python package for embedding YouTube videos in Jupyter Notebooks
Overview
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.
✨ Features
- 🎥 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
Installation
Install ytnb-embed using pip:
pip install ytnb-embed
Requirements
- Python 3.13+
- IPython
- Jupyter Notebook or JupyterLab
Quick Start
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.
Usage
Supported URL Formats
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")
Custom Player Dimensions
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)
Working Example
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)
🔧 API Reference
embed_yt(url, width=780, height=440)
Embeds a YouTube video in a Jupyter Notebook using an iframe.
Parameters
| 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 |
Returns
str: Returns"success"when the video is successfully embedded
Raises
InvalidURLException: Raised when the provided URL is not a valid YouTube URLException: Raised for other unexpected errors during embedding
Example
result = embed_yt("https://www.youtube.com/watch?v=VIDEO_ID", width=800, height=450)
# result == "success"
🔒 Privacy & Security
Privacy-Enhanced Mode
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
Security Features
- 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
Contributing
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
Contribution Guidelines
- 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
📝 License
This project is licensed under the MIT License - see the LICENSE file for full details.
👤 Author
- GitHub: @aatansen
- Email: aatansen@gmail.com
- PyPI: ytnb-embed
🙏 Acknowledgments
- 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
📮 Support
If you encounter any issues or have questions:
- 🐛 Bug reports: GitHub Issues
- 💡 Feature requests: GitHub Issues
- 📧 Email: aatansen@gmail.com
📊 Project Stats
Made with ❤️ for the Jupyter community
If you find this package helpful, please consider giving it a ⭐ on GitHub!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ytnb_embed-1.0.0.tar.gz.
File metadata
- Download URL: ytnb_embed-1.0.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3f03c4848032a67dc2122545aaa144e2020ac64c8ec45ff4b891dd31dafa98e
|
|
| MD5 |
e4e34ff63940ee271a4044f44dd48e14
|
|
| BLAKE2b-256 |
72c8cba5183b680df448ab36c9e3b19396a9acf24bbf5a537b899baf92329d6b
|
File details
Details for the file ytnb_embed-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ytnb_embed-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04d55ab69c2e366c2be6f9297da73484bf2d0342792c006d70bdc0298a9de781
|
|
| MD5 |
59c92538a7c1717a45a3b1468f458603
|
|
| BLAKE2b-256 |
dd8cdb398261ddedc7492664a8cfe4bdf2db239ce927dc6d32769ed482f23051
|