Skip to main content

Embed youtube video inside notebook

Project description

ytnb-embed

PyPI version Python Version License: MIT Downloads Code style: black CI Documentation

A lightweight, privacy-focused Python package for embedding YouTube videos in Jupyter Notebooks

InstallationQuick StartUsageDocumentationContributing


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.com for 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.10+
  • 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 URL
  • Exception: 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-origin prevents data leakage

Contributing

Contributions are welcome and greatly appreciated! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Make your changes
  4. Add tests for new functionality
  5. Commit your changes (git commit -m 'Add some AmazingFeature')
  6. Push to the branch (git push origin feature/AmazingFeature)
  7. 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

🙏 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:

📊 Project Stats

GitHub stars GitHub forks GitHub issues GitHub pull requests

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

ytnb_embed-1.0.4.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ytnb_embed-1.0.4-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file ytnb_embed-1.0.4.tar.gz.

File metadata

  • Download URL: ytnb_embed-1.0.4.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ytnb_embed-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d2d781b81b113d4e7670ebc429d2086c77338a70568defb7354dd3284bd204a1
MD5 8045547c9a5696566387cb7df4a14ff0
BLAKE2b-256 7f4c1848adf61c8abdcba20f2a1feef921adc83c0412c9c81c9339f2f6cee020

See more details on using hashes here.

File details

Details for the file ytnb_embed-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ytnb_embed-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ytnb_embed-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 caa855ba3663fd530a3ba8d59bdec4855fb533a11264502d19ae431cc5d860f4
MD5 8048dd42937ed41e583dc8427ae2fd2c
BLAKE2b-256 4e1c7f9ca63fa91c65c432a46815868e165fec7db0ff345da13f80277bf44acf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page