Fix Gallery preview=True parameter not working on initial load#12866
Fix Gallery preview=True parameter not working on initial load#12866freddyaboulton merged 3 commits intogradio-app:mainfrom
Conversation
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
Manual VerificationTested the Gallery Test Setupimport 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 Root cause: The preview initialization logic only ran when Fix: Added initialization effect in Before (v6.4.0+): Gallery showed grid view despite |
|
All checks passing ✓ Mergeable: YES | Comprehensive manual testing evidence provided in PR description. Ready for @gradio maintainers review and merge. |
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-pypi-previews.s3.amazonaws.com/b2237a9761ad4e1a362047b4ac55f04ff4565ef2/gradio-6.5.1-py3-none-any.whlInstall 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 |
🦄 change detectedThis Pull Request includes changes to the following packages.
✅ Changeset approved by @freddyaboulton
|
freddyaboulton
left a comment
There was a problem hiding this comment.
Confirmed this does fix the issue. Thank you @veeceey ! In the future, we would appreciate human-written PR descriptions!
Summary
Changes
js/gallery/shared/Gallery.svelteto setselected_indexto 0 when preview is true and value has itemsRoot Cause
The preview initialization logic only ran when
was_resetwas 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
test_preview_bug.pywith gallery using preview=TrueTest Steps
python test_preview_bug.pyBefore/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