Skip to main content
Want to squeeze into a photo booth with your friends without squeezing into an actual booth? Let’s build one in the browser. Take a photo, add a few hearts, and invite someone else to decorate the same picture with you. A Beautiful Camera is a Python application inspired by purikura, the Japanese photo booths known for playful photo decorations. Our version has a shared photo, colorful sticker packs, and a gallery for saving your creations. It also has a release problem to solve: how do you introduce a new sparkle sticker pack to selected visitors, then turn it off if something goes wrong while people are still decorating? In this tutorial, you’ll run the Flask photo booth and walk through the code that connects LaunchDarkly feature flags to a live interface. You’ll use targeting to give different artists different sticker packs, then use Socket.IO to update their browsers without reloading the page. The complete photo booth source includes the camera, styles, and gallery. Follow along with that implementation. The code samples below explain existing sections rather than forming a second application to paste into app.py. Here’s the booth before releasing the sparkle pack: A Beautiful Camera with Diane selected, classic heart and star stickers on a pastel backdrop, and the sparkle palette locked. The classic booth works while sparkle targeting is off. The control room reports OFF for every artist. All screenshots show the actual app captured with Playwright using local SDK test data. The green camera image is Chromium’s simulated camera, not a person’s photo. Local flag fixtures and the app’s notification callback reproduce the tutorial’s release states. These captures do not verify a live LaunchDarkly stream or show the LaunchDarkly UI.

Tutorial requirements

To follow along, you need:
  • Python 3.10 or newer and Git installed on your computer.
  • A code editor and a browser with camera support.
  • A LaunchDarkly account with permission to create flags and segments in a project and environment.
  • The server-side software development kit (SDK) key for that environment.
You can decorate the pastel backdrop without granting camera access. To take a photo, use localhost or a site served over HTTPS. Browsers require a secure context and your permission for getUserMedia().

Understand the two live connections

A feature flag separates deploying code from releasing a feature. We can deploy the sparkle pack with the rest of the application, then decide who can use it through sparkleStickerEngine. The Python server evaluates that flag for each artist. WebSockets solve a different part of the experience: keeping a browser and server in communication after the page loads. We use Socket.IO for named, bidirectional events. It can use WebSocket transport and fall back to HTTP long-polling. Because Socket.IO adds its own protocol, this app uses its JavaScript client rather than the browser’s native WebSocket constructor. There are two separate connections in this design:
The SDK’s default streaming connection receives configuration updates from LaunchDarkly. It is separate from the browser connection. Our application bridges them by evaluating the updated flag and sending the results through Socket.IO. A flag change does not update an already-rendered page on its own.

Set up the Python application

Clone the project and create a virtual environment:
On Windows PowerShell, activate with .venv\Scripts\Activate.ps1 and copy the configuration with Copy-Item .env.example .env. The dependencies include Flask for routes and templates, Flask-SocketIO and simple-websocket for browser events, launchdarkly-server-sdk for flag evaluation, and python-dotenv for local configuration. The flag-change listener requires Python SDK version 9.1 or later. Install a compatible version if your existing environment has 9.0:
Open .env and replace the placeholder SDK key with the server-side key from your chosen LaunchDarkly environment:
Keep this key on the server. The browser receives evaluated booleans, not SDK credentials. The files we’ll work through have these responsibilities:

Configure the sticker flags

Create the flags in the same project and environment as your SDK key. Flag keys are case-sensitive. Start by defining the reusable audiences:
  1. Open your environment’s Segments page.
  2. Create a segment with the key free-tier.
  3. Add a rule matching the artist context kind’s plan attribute to free.
  4. Save the segment.
  5. Create a segment with the key premium-tier.
  6. Add a rule matching the artist context kind’s plan attribute to pro.
  7. Save the segment.
Next, configure the primary flag:
  1. Create a boolean flag with the key sparkleStickerEngine.
  2. Set its off variation to false.
  3. Add an individual target for context kind artist, key diane-artist, serving true.
  4. Add a targeting rule matching the free-tier segment, serving false.
  5. Set the default rule to serve true.
  6. Save the configuration with targeting off in your chosen environment.
Finally, configure the second pack:
  1. Create a boolean flag with the key spookyStickerPack.
  2. Set its off variation to false.
  3. Add a targeting rule matching the premium-tier segment, serving true.
  4. Set the default rule to serve false.
  5. Save the configuration and turn targeting on.
Both flags reuse the same audience definitions. This lets you release sparkle stickers and a seasonal pack independently. With sparkle targeting on, expect these results: Diane receives sparkle even though she matches the free-tier segment because individual targeting takes precedence over custom rules, assuming prerequisites are satisfied. When targeting is off, the configured off variation applies, including to Diane.

Initialize the SDK and evaluate each artist

Open app.py. The main() function initializes one shared LaunchDarkly client before starting the server. This excerpt shows the default setup. The repository also supports optional endpoint overrides:
The app exits if initialization fails. After initialization, the Python SDK evaluates flags using its local flag data rather than making a network request for each sticker placement. LaunchDarkly needs a context describing who is requesting the feature. In build_context(), the app creates an artist context with a stable key and attributes that match our rules:
Here, p is the selected persona’s record from PERSONAS. The multi-kind context combines artist, which supplies targeting attributes, with user, which the sample reserves for experiment randomization. You can use an artist context alone when experimentation is outside your application’s scope. The evaluate() function retrieves the sparkle decision and its explanation:
The final False is the fallback if evaluation cannot produce a value. It is separate from the off variation you configured in LaunchDarkly. A temporary stream interruption does not automatically turn the flag off: the SDK can continue using its last known flag data. The function returns each artist’s decisions with detail.reason. The control room displays reasons such as OFF, TARGET_MATCH, RULE_MATCH, and FALLTHROUGH, connecting the visible sticker palette to the rule that selected it. Its segment labels are derived from the demo personas’ plans. They are not a segment-membership response from LaunchDarkly.

Send flag changes to browsers without a reload

The server creates its Socket.IO instance near the top of app.py:
The larger message limit accommodates a captured image. The permissive origins setting is part of this demo. Restrict allowed origins for a deployed application. Start the sample with python app.py so it uses socketio.run(), as shown above. The registered listener connects SDK changes to the browser event stream:
flag_change.key identifies a changed flag. It does not contain one universal boolean for every visitor. The callback re-evaluates the personas because the same flag can serve different values to Diane and Sam. It uses socketio.emit() to broadcast from outside a browser request handler. In static/booth.js, the client receives those decisions:
applyEvaluations() finds the current artist, enables or disables their palette buttons, and updates the control room. If a selected sparkle sticker becomes unavailable, it switches the selection back to a classic heart. There is no location.reload(), which means the current photo and placed stickers remain on screen. After enabling sparkle, compare the unlocked palette with each artist’s evaluation reason: Diane's sparkle palette unlocked with sparkle stickers on the captured image. The control room shows TARGET_MATCH for Diane, RULE_MATCH for Sam, and FALLTHROUGH for Mika and Rin. Diane receives sparkle through her individual target. Sam still receives classic stickers through the free-tier rule. The SDK listener documentation describes configuration notifications and context-specific value listeners. Here we use a configuration listener so one callback can refresh every demo persona. Updates travel asynchronously through both connections. This is a live update, not a guarantee of zero delay.

Share the photo and sticker placements

First, connect each browser to the booth. The client creates its connection with const socket = io() and joins on every connection, including reconnections:
join() sends the selected persona. The server’s handle_join() records the Socket.IO session ID and responds to that browser with a booth_state snapshot containing the photo, stickers, gallery, and current evaluations. This matters because a browser that was disconnected during a flag change must get current state when it returns. The camera stays local until someone takes a snapshot. In the camera button handler, request a front-facing video stream without audio:
Select camera to open the preview before capturing a frame: The camera preview displays Chromium's green test pattern, with snap and cancel controls available below the photo frame. The preview is local to this browser. Select snap! to share a still photo, or cancel to stop the camera. When the visitor selects snap!, the app draws a mirrored frame onto an 800-by-600 canvas, encodes a JPEG, emits set_photo, and stops the camera tracks. The server validates the data URL, replaces the shared photo, clears the previous stickers, and broadcasts photo_updated. Everyone in this demo shares one booth, which means a new photo replaces the backdrop for all connected visitors. Live camera video is not broadcast. Sticker placement uses the same event-driven pattern. The client converts a click to percentage coordinates so different screen sizes can show the sticker in the same relative position:
The server checks the persona and emoji, evaluates access to gated packs, clamps the coordinates, and appends the accepted sticker to shared state. It then emits sticker_added to everyone. Each browser calls addSticker() to display the server’s accepted result, including the sparkle or spooky effect. Hiding a palette is useful feedback, but the server must enforce the flag too. This excerpt from handle_place_sticker() performs that check before saving or broadcasting a sticker:
A stale browser can still send a placement event after its palette should have locked. Re-evaluating on the server checks that action against the SDK’s current flag data. The persona selector is for demonstration, however: a production app must derive identity and plan from an authenticated session rather than let visitors choose another person’s identity. The save button composites the photo and current stickers into a JPEG. The server writes that image to gallery/ and broadcasts gallery_added, letting the browsers show the new thumbnail. After saving, check that your decorated photo appears in the gallery: A decorated camera snapshot remains in the booth and a matching thumbnail appears in the gallery below the sticker palettes. Select a gallery thumbnail to open the saved image. The other connected browser receives the same gallery entry.

Test the release with two browsers

Start the application from your activated virtual environment:
Open the local photo booth in two browser windows or separate browser profiles. Then work through the release:
  1. Select Diane in one window and Sam in the other.
  2. Take a photo with camera and snap!, or keep the pastel backdrop.
  3. Place a classic sticker and confirm it appears in both windows.
  4. Turn on sparkleStickerEngine in your LaunchDarkly environment and save the change.
  5. Confirm Diane’s sparkle palette unlocks while Sam’s remains locked.
  6. Place a sparkle sticker as Diane and confirm both windows display it.
  7. Switch Sam’s window to Mika and confirm the sparkle and spooky packs are available.
  8. Turn off sparkleStickerEngine and save the change.
  9. Confirm both windows lock sparkle placements while keeping the photo and existing stickers.
  10. Place another classic sticker and save the decorated photo to the gallery.
  11. Refresh one window and confirm it receives the current photo, stickers, gallery, and flag decisions.
Sam can see a sparkle sticker placed by Diane even though Sam cannot place one. The flag controls the ability to add that pack’s stickers. It does not hide collaborative content contributed by someone else. Compare Sam’s window with Diane’s released state: Sam selected in the photo booth with his sparkle palette locked, while Diane's sparkle stickers remain visible on the shared photo and her control-room row still shows sparkle access. Sam sees the shared artwork, but RULE_MATCH keeps his own sparkle palette locked. Turning the flag off acts as a kill switch for new sparkle placements. It does not remove existing sparkle effects or stop all sticker rendering. A rollback designed to stop a faulty animation entirely would also need to disable effects on existing stickers. After rollback, look for locked sparkle controls and the preserved artwork: Diane's sparkle palette is locked after rollback, every artist's evaluation reason is OFF, and the previously placed sparkle stickers remain on the same photo. The flag has turned off for every artist. Existing stickers stay on the photo, and classic stickers remain available.

Add an optional emergency trigger

The toggle in the LaunchDarkly UI is enough to demonstrate rollback. The sample also includes an emergency de-sparkle button that posts to a configured LaunchDarkly trigger. If your account supports a trigger that turns flag targeting off, create one for sparkleStickerEngine in the same environment, then add its URL to .env:
You can create that trigger in the UI, or without it. In the UI, open the environment’s three-dot overflow menu, select Configuration in environment, then click + Add trigger in the “Triggers” section. Choose Generic trigger as the trigger type and an action that turns targeting off. To learn more, read Creating flag triggers. The Create a flag trigger API takes an API access token (api-...), not the server-side SDK key:
Copy triggerURL from the 201 response into LD_TRIGGER_URL. LaunchDarkly returns that URL only on create. Restart the Python server to load the new variable. The button calls /api/panic. That server route sends the POST request to LaunchDarkly. The normal SDK listener and Socket.IO path then updates the browsers. Keep the trigger URL on the server because anyone who possesses it may be able to invoke its action. Read the LaunchDarkly trigger documentation for current availability and setup. The sample panic route has no authentication. Protect this administrative action before exposing it beyond a controlled demo.

Troubleshoot the live updates

Use these checks to narrow down a setup issue:

Keep building your photo booth

You now have a working release exercise: deploy a sticker feature, expose it to selected artists, propagate updated decisions to open browsers, and disable new placements while visitors keep decorating. For your next iteration, add private booth rooms so separate groups can decorate their own photos. The current app keeps one shared booth in Python memory and saves gallery images on local disk. Running multiple server processes requires shared application state and coordinated Socket.IO broadcasts. The development server configuration in this sample is for the tutorial. You can also explore engagement metrics. The app already calls track("sticker-placed", build_context(persona)) for accepted placements. Configure a metric and experiment separately to investigate whether access to the sparkle pack changes behavior. Tracking an event alone does not establish an experiment or a causal result. Try a seasonal pack, a different camera frame, or an artist-only beta. The same pattern applies: evaluate the feature on the server, notify connected browsers when decisions change, and validate gated actions when they arrive.