| #!/usr/bin/env python | |
| import pathlib | |
| import gradio as gr | |
| paths = [ | |
| path.as_posix() for path in sorted(pathlib.Path('images').rglob('*.jpg')) | |
| ] | |
| def fn(data: gr.SelectData) -> int: | |
| return data.index | |
| with gr.Blocks() as demo: | |
| gallery = gr.Gallery(value=paths, | |
| allow_preview=False, | |
| columns=2, | |
| rows=2, | |
| height='600px', | |
| object_fit='scale-down') | |
| selected_index = gr.Number(label='selected index', precision=0, value=-1) | |
| gallery.select(fn=fn, outputs=selected_index) | |
| demo.queue().launch() | |