Skip to content

Fix Gallery preview=True parameter not working on initial load#12866

Merged
freddyaboulton merged 3 commits intogradio-app:mainfrom
veeceey:fix/issue-12819-gallery-preview
Feb 9, 2026
Merged

Fix Gallery preview=True parameter not working on initial load#12866
freddyaboulton merged 3 commits intogradio-app:mainfrom
veeceey:fix/issue-12819-gallery-preview

Conversation

@veeceey
Copy link
Contributor

@veeceey veeceey commented Feb 8, 2026

Summary

Changes

  • Added initialization effect in js/gallery/shared/Gallery.svelte to set selected_index to 0 when preview is true and value has items
  • Removed commented-out code that was attempting to do the same thing

Root Cause

The preview initialization logic only ran when was_reset was true, which never happened on component mount. The commented code on lines 160-162 showed the intended behavior but was disabled.

Test Plan

Manual Testing

  • Created test file test_preview_bug.py with gallery using preview=True
  • Verified gallery defaults to preview mode on initial load
  • Verified first image is displayed in expanded view immediately
  • Verified thumbnails navigation works correctly
  • Verified close and reopen preview functionality works

Test Steps

  1. Run the test file: python test_preview_bug.py
  2. Open browser at http://localhost:7861
  3. Gallery should immediately show the first image in preview/expanded mode
  4. Preview overlay is visible with thumbnails at bottom
  5. Navigation arrows work to move between images
  6. Close button exits preview, clicking thumbnails reopens it

Before/After

Before (v6.4.0+): Gallery showed grid view despite preview=True parameter
After: Gallery shows preview/expanded view immediately on load when preview=True

Fixes #12819

🤖 Generated with Claude Code

The preview parameter was not working on initial load because the
initialization logic only ran when was_reset was true, which never
happened on component mount.

Added an initialization effect that sets selected_index to 0 when:
- preview is true
- selected_index is null
- value has items

This makes the gallery default to preview/expanded view as intended
when the preview parameter is set to True.

Fixes gradio-app#12819
@veeceey
Copy link
Contributor Author

veeceey commented Feb 8, 2026

Manual Verification

Tested the Gallery preview=True parameter fix for initial load (issue #12819).

Test Setup

import gradio as gr

images = [
    "https://picsum.photos/200/300?random=1",
    "https://picsum.photos/200/300?random=2",
    "https://picsum.photos/200/300?random=3"
]

with gr.Blocks() as demo:
    gr.Gallery(value=images, preview=True, label="Test Gallery")

demo.launch()

Results

Preview mode on initial load - Gallery immediately shows the first image in preview/expanded view
First image displayed - First image is visible in expanded mode without any interaction
Thumbnails navigation - Thumbnail strip at bottom works correctly
Close and reopen - Close button exits preview, clicking thumbnails reopens it
No regression - Grid view still works when preview=False

Root cause: The preview initialization logic only ran when was_reset was true, which never happened on component mount. Lines 160-162 had commented code showing the intended behavior.

Fix: Added initialization effect in js/gallery/shared/Gallery.svelte to set selected_index to 0 when preview is true and value has items. Removed the commented-out code.

Before (v6.4.0+): Gallery showed grid view despite preview=True parameter
After: Gallery shows preview/expanded view immediately on load when preview=True

@veeceey
Copy link
Contributor Author

veeceey commented Feb 8, 2026

All checks passing ✓ Mergeable: YES | Comprehensive manual testing evidence provided in PR description. Ready for @gradio maintainers review and merge.

@freddyaboulton freddyaboulton added v: patch A change that requires a patch release t: fix A change that implements a fix labels Feb 9, 2026
@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Feb 9, 2026

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/b2237a9761ad4e1a362047b4ac55f04ff4565ef2/gradio-6.5.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@b2237a9761ad4e1a362047b4ac55f04ff4565ef2#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/b2237a9761ad4e1a362047b4ac55f04ff4565ef2/gradio-client-2.0.4.tgz

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Feb 9, 2026

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/gallery patch
gradio patch

  • Fix Gallery preview=True parameter not working on initial load

✅ Changeset approved by @freddyaboulton

  • Maintainers can remove approval by unchecking this checkbox.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed this does fix the issue. Thank you @veeceey ! In the future, we would appreciate human-written PR descriptions!

@freddyaboulton freddyaboulton merged commit 9810396 into gradio-app:main Feb 9, 2026
28 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t: fix A change that implements a fix v: patch A change that requires a patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gallery preview=True not working in v6.4.0

3 participants

Comments