Skip to main content

Reflex custom component webcam

Project description

reflex-webcam

Package for a webcam for the Reflex Framework

⚙️ Installation

Open a terminal and run (Requires Python 3.8+):

pip install reflex-webcam

Import package

import reflex_webcam as webcam

Simple Usage (Screenshot)

This example below allows a user to use the webcam and take a screenshot, which is rendered under the webcam live feed.

To run the app shown below just copy and paste the code in a Reflex app.

import time
from pathlib import Path
from urllib.request import urlopen
from PIL import Image

import reflex as rx
import reflex_webcam as webcam


# Identifies a particular webcam component in the DOM
WEBCAM_REF = "webcam"


class State(rx.State):
    last_screenshot: Image.Image | None = None
    last_screenshot_timestamp: str = ""
    loading: bool = False

    def handle_screenshot(self, img_data_uri: str):
        """Webcam screenshot upload handler.
        Args:
            img_data_uri: The data uri of the screenshot (from upload_screenshot).
        """
        if self.loading:
            return
        self.last_screenshot_timestamp = time.strftime("%H:%M:%S")
        with urlopen(img_data_uri) as img:
            self.last_screenshot = Image.open(img)
            self.last_screenshot.load()
            # convert to webp during serialization for smaller size
            self.last_screenshot.format = "WEBP"  # type: ignore


def last_screenshot_widget() -> rx.Component:
    """Widget for displaying the last screenshot and timestamp."""
    return rx.box(
        rx.cond(
            State.last_screenshot,
            rx.fragment(
                rx.image(src=State.last_screenshot),
                rx.text(State.last_screenshot_timestamp),
            ),
            rx.center(
                rx.text("Click image to capture.", size="4"),
                ),
        ),
        height="270px",
    )


def webcam_upload_component(ref: str) -> rx.Component:
    """Component for displaying webcam preview and uploading screenshots.
    Args:
        ref: The ref of the webcam component.
    Returns:
        A reflex component.
    """
    return rx.vstack(
        webcam.webcam(
            id=ref,
            on_click=webcam.upload_screenshot(
                ref=ref,
                handler=State.handle_screenshot,  # type: ignore
            ),
        ),
        last_screenshot_widget(),
        width="320px",
        align="center",
    )


def index() -> rx.Component:
    return rx.fragment(
        rx.center(
            webcam_upload_component(WEBCAM_REF),
            padding_top="3em",
        ),
    )


app = rx.App()
app.add_page(index)

Advanced Usage (Video recording)

To run the webcam with video recording capabilities follow the instructions below to run the webcam_demo in this Github repo.

cd webcam_demo
reflex init
reflex run

You should see your app running at http://localhost:3000.

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

reflex_webcam-0.1.0a1.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

reflex_webcam-0.1.0a1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file reflex_webcam-0.1.0a1.tar.gz.

File metadata

  • Download URL: reflex_webcam-0.1.0a1.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for reflex_webcam-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 0bed63d7a85bca6341a6de615400643ac0b1f4d9b4c231150a25cffc9080c60b
MD5 d853d1db34da1d82bb55f840ab369794
BLAKE2b-256 6619a5c28c259498f28aaaa73aad1e3bf27f6a4a77b07b3bf547127c508bbe5a

See more details on using hashes here.

File details

Details for the file reflex_webcam-0.1.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for reflex_webcam-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 54d6d275de28c0bb00baccb77ae4dfcf2fffe92b08bb5081b3521452eb45a102
MD5 26c18f52b41656d04568c133e9e1e3ae
BLAKE2b-256 32578ec92649a80a29a4dbbee950db12f619cbdcc77c4ed8553da30dbf8f254e

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