Skip to main content

A FolderExplorer for Gradio UI

Project description


tags: [gradio-custom-component, FileExplorer] title: gradio_folderexplorer short_description: A FolderExplorer for Gradio UI colorFrom: blue colorTo: yellow sdk: gradio pinned: false app_file: space.py

gradio_folderexplorer

Static Badge

A FolderExplorer for Gradio UI

Installation

pip install gradio_folderexplorer

Usage

import gradio as gr
from gradio_folderexplorer import FolderExplorer
from gradio_folderexplorer.helpers import load_media_from_folder
from PIL import Image
import os

# --- Configuration Constants ---

# Define the root directory for the FolderExplorer to start in.
# All browsable folders will be relative to this path.
ROOT_DIR_PATH = "./examples"

# --- UI Layout and Logic ---

# Create the user interface using Gradio Blocks.
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
    # A single row is used to create a side-by-side layout.
    gr.Markdown("# FolderExplorer Component Demo")
    with gr.Row(equal_height=True):
        # The first column contains the custom folder explorer component.
        with gr.Column(scale=1, min_width=300):
            folder_explorer = FolderExplorer(
                label="Select a Folder",
                root_dir=ROOT_DIR_PATH,
                # Set the initial selected value to the root directory itself.
                # This is used by the demo.load() event.
                value=ROOT_DIR_PATH
            )
        
        # The second column contains the gallery to display the media.
        with gr.Column(scale=3):
            gallery = gr.Gallery(
                label="Selected Images",
                columns=6,
                height="auto",
            )

    # --- Event Handling ---

    # 1. Event for user interaction:
    # When the user selects a new folder in the FolderExplorer, the .change() event
    # is triggered. The `load_media_from_folder` helper is called with the new
    # folder path, and its output populates the gallery.
    folder_explorer.change(
        fn=load_media_from_folder,
        inputs=folder_explorer,
        outputs=gallery
    )
    
    # 2. Event for initial page load:
    # This event runs once when the app starts. It takes the initial `value` of the
    # folder_explorer ('ROOT_DIR_PATH'), passes it to the helper function,
    # and populates the gallery with the media from the root directory.
    demo.load(
        fn=load_media_from_folder,
        inputs=folder_explorer,
        outputs=gallery
    )

# --- Application Launch ---
if __name__ == "__main__":
    demo.launch()

FolderExplorer

Initialization

name type default description
value
str | Path | Callable | None
None The directory path to show as "selected" when the component is first loaded. If a callable is provided, it will be called when the app loads to set the initial value.
root_dir
str | Path
"." Path to the root directory from which to browse folders. If not provided, defaults to the current working directory. Raises ValueError if the directory does not exist or is not a directory.
label
str | I18nData | None
None The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
every
Timer | float | None
None Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
inputs
Component | Sequence[Component] | set[Component] | None
None Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
show_label
bool | None
None If True, will display the label.
container
bool
True If True, will place the component in a container, providing some extra padding around the border.
scale
int | None
None Relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer.
min_width
int
160 Minimum pixel width, will wrap if not sufficient screen space to satisfy this value.
height
int | str | None
None The height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed.
max_height
int | str | None
500 The maximum height of the component.
min_height
int | str | None
None The minimum height of the component.
interactive
bool | None
None If True, will allow users to select a folder; if False, will only display the folder structure.
visible
bool | Literal["hidden"]
True If False, component will be hidden. If "hidden", component will be visually hidden but still exist in the DOM.
elem_id
str | None
None An optional string that is assigned as the id of this component in the HTML DOM.
elem_classes
list[str] | str | None
None An optional list of strings that are assigned as the classes of this component in the HTML DOM.
render
bool
True If False, component will not be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key
int | str | tuple[int | str, ...] | None
None In a gr.render, Components with the same key across re-renders are treated as the same component.
preserved_by_key
list[str] | str | None
"value" A list of parameters from this component's constructor. If a component is re-rendered with the same key, these parameters will be preserved in the UI.

Events

name description
change

User function

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

  • When used as an Input, the component only impacts the input signature of the user function.
  • When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

  • As output: Is passed, the absolute path of the selected folder as a str. Returns the root directory if no folder is selected.
  • As input: Should return, expects a str or Path object representing the directory to be selected in the UI.
def predict(
    value: str | None
) -> str | pathlib.Path | None:
    return value

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradio_folderexplorer-0.0.1.tar.gz (8.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gradio_folderexplorer-0.0.1-py3-none-any.whl (96.1 kB view details)

Uploaded Python 3

File details

Details for the file gradio_folderexplorer-0.0.1.tar.gz.

File metadata

  • Download URL: gradio_folderexplorer-0.0.1.tar.gz
  • Upload date:
  • Size: 8.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for gradio_folderexplorer-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fe5583c870761ec5e0dce17679881492237b97fd1c2765e858fe9eccfbd7ee38
MD5 3c6f7cf19b5ea68ae6116295ec50c08d
BLAKE2b-256 a264c0e01d45a9bb1daffb06e61d9f34e096f0b48a6110c5ace955b48a75c686

See more details on using hashes here.

File details

Details for the file gradio_folderexplorer-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gradio_folderexplorer-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e041b5c6c081f25d22851ad167e4d4ebbd61a8facf8ffef6a61bfbc50768ef92
MD5 37e3c95181c9a498c92769fc1132a5ca
BLAKE2b-256 35c00d734f087d705b1a4a1a9bb316e18e445e56f0d83ce2ad1cd1b7bc48cee9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page