gradio-folderexplorer 0.0.1
A FolderExplorer for Gradio UI
Requires Python: >=3.10
Keywords: gradio-custom-component, gradio-template-FileExplorer

---
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`
<img alt="Static Badge" src="https://img.shields.io/badge/version%20-%200.0.1%20-%20orange">  

A FolderExplorer for Gradio UI

## Installation

```bash
pip install gradio_folderexplorer
```

## Usage

```python
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

<table>
<thead>
<tr>
<th align="left">name</th>
<th align="left" style="width: 25%;">type</th>
<th align="left">default</th>
<th align="left">description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>value</code></td>
<td align="left" style="width: 25%;">

```python
str | Path | Callable | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>root_dir</code></td>
<td align="left" style="width: 25%;">

```python
str | Path
```

</td>
<td align="left"><code>"."</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>label</code></td>
<td align="left" style="width: 25%;">

```python
str | I18nData | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>every</code></td>
<td align="left" style="width: 25%;">

```python
Timer | float | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>inputs</code></td>
<td align="left" style="width: 25%;">

```python
Component | Sequence[Component] | set[Component] | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>show_label</code></td>
<td align="left" style="width: 25%;">

```python
bool | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">If True, will display the label.</td>
</tr>

<tr>
<td align="left"><code>container</code></td>
<td align="left" style="width: 25%;">

```python
bool
```

</td>
<td align="left"><code>True</code></td>
<td align="left">If True, will place the component in a container, providing some extra padding around the border.</td>
</tr>

<tr>
<td align="left"><code>scale</code></td>
<td align="left" style="width: 25%;">

```python
int | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>min_width</code></td>
<td align="left" style="width: 25%;">

```python
int
```

</td>
<td align="left"><code>160</code></td>
<td align="left">Minimum pixel width, will wrap if not sufficient screen space to satisfy this value.</td>
</tr>

<tr>
<td align="left"><code>height</code></td>
<td align="left" style="width: 25%;">

```python
int | str | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">The height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed.</td>
</tr>

<tr>
<td align="left"><code>max_height</code></td>
<td align="left" style="width: 25%;">

```python
int | str | None
```

</td>
<td align="left"><code>500</code></td>
<td align="left">The maximum height of the component.</td>
</tr>

<tr>
<td align="left"><code>min_height</code></td>
<td align="left" style="width: 25%;">

```python
int | str | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">The minimum height of the component.</td>
</tr>

<tr>
<td align="left"><code>interactive</code></td>
<td align="left" style="width: 25%;">

```python
bool | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">If True, will allow users to select a folder; if False, will only display the folder structure.</td>
</tr>

<tr>
<td align="left"><code>visible</code></td>
<td align="left" style="width: 25%;">

```python
bool | Literal["hidden"]
```

</td>
<td align="left"><code>True</code></td>
<td align="left">If False, component will be hidden. If "hidden", component will be visually hidden but still exist in the DOM.</td>
</tr>

<tr>
<td align="left"><code>elem_id</code></td>
<td align="left" style="width: 25%;">

```python
str | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">An optional string that is assigned as the id of this component in the HTML DOM.</td>
</tr>

<tr>
<td align="left"><code>elem_classes</code></td>
<td align="left" style="width: 25%;">

```python
list[str] | str | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM.</td>
</tr>

<tr>
<td align="left"><code>render</code></td>
<td align="left" style="width: 25%;">

```python
bool
```

</td>
<td align="left"><code>True</code></td>
<td align="left">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.</td>
</tr>

<tr>
<td align="left"><code>key</code></td>
<td align="left" style="width: 25%;">

```python
int | str | tuple[int | str, ...] | None
```

</td>
<td align="left"><code>None</code></td>
<td align="left">In a gr.render, Components with the same key across re-renders are treated as the same component.</td>
</tr>

<tr>
<td align="left"><code>preserved_by_key</code></td>
<td align="left" style="width: 25%;">

```python
list[str] | str | None
```

</td>
<td align="left"><code>"value"</code></td>
<td align="left">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.</td>
</tr>
</tbody></table>


### 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.

 ```python
 def predict(
     value: str | None
 ) -> str | pathlib.Path | None:
     return value
 ```