A newer version of the Gradio SDK is available: 6.26.0
Add Shift+Scroll Zoom to Map Georectification App
Context
The Gradio app displays map images via gr.Image components but has no zoom capability. For precise GCP placement and coordinate querying, users need to zoom into the image. The simplest approach is injecting custom JavaScript.
Approach
Inject a JS snippet via the js parameter on gr.Blocks() that:
- Listens for
wheelevents on image containers (.image-container img) - When
shiftKeyis held, prevents default scroll and adjusts a CSStransform: scale()+transform-originon the image - Supports zoom in/out with shift+scroll, clamped to a reasonable range (e.g. 1x–10x)
- Sets
overflow: hiddenon the image container so zoomed content is clipped properly
File to modify
/home/inverse/workspace/images/app.py— single change
Change detail
- Define a
ZOOM_JSstring constant containing the JavaScript - Pass
js=ZOOM_JStogr.Blocks(title="Map Georectification", js=ZOOM_JS)
The JS will:
- Use event delegation on
documentforwheelevents - Find the closest
.image-containerancestor - Track zoom level per element (stored as a data attribute)
- Apply
transform: scale(zoom)withtransform-originat the cursor position - Clamp zoom between 1x and 10x
- Reset to 1x on double-click (bonus)
Verification
uv run app.pyand open in browser- Hold Shift and scroll over either image — it should zoom in/out
- Release Shift — normal page scroll resumes
- Image click events (GCP placement, coordinate query) should still work at any zoom level