Gradio supports the ability to create custom progress bars so that you have customizability and control over the progress update that you show to the user.
Basic usage
To enable progress bars, add an argument to your function that has a default value of a gr.Progress instance. Then you can update the progress levels by calling this instance directly with a float between 0 and 1.
import gradio as gr
import time
def slowly_reverse(word, progress=gr.Progress()):
progress(0, desc="Starting")
time.sleep(1)
progress(0.05)
new_string = ""
for letter in progress.tqdm(word, desc="Reversing"):
time.sleep(0.25)
new_string = letter + new_string
return new_string
demo = gr.Interface(slowly_reverse, gr.Text(), gr.Text())
demo.launch()
In this example, the progress bar will:
- Start at 0% with the description “Starting”
- Update to 5% after a brief pause
- Show incremental progress with the description “Reversing” as it processes each letter
Using the tqdm method
The Progress instance provides a tqdm() method that makes it easy to track progress over an iterable. This method works similarly to the popular tqdm library:
import gradio as gr
import time
def process_items(items, progress=gr.Progress()):
results = []
for item in progress.tqdm(items, desc="Processing items"):
time.sleep(0.1)
results.append(item * 2)
return results
demo = gr.Interface(
process_items,
gr.Textbox(lines=5, placeholder="Enter items, one per line"),
gr.Textbox()
)
demo.launch()
Automatic tqdm integration
If you use the tqdm library, you can even report progress updates automatically from any tqdm.tqdm that already exists within your function by setting the default argument as gr.Progress(track_tqdm=True):
import gradio as gr
import time
from tqdm import tqdm
def process_with_tqdm(n, progress=gr.Progress(track_tqdm=True)):
results = []
for i in tqdm(range(n)):
time.sleep(0.1)
results.append(i ** 2)
return sum(results)
demo = gr.Interface(process_with_tqdm, gr.Slider(1, 100, step=1), gr.Number())
demo.launch()
With track_tqdm=True, you don’t need to manually update the progress - Gradio automatically tracks any tqdm progress bars in your function.
Progress bar parameters
When calling the Progress instance, you can provide:
- Float value (0-1): The progress level as a decimal between 0 and 1
desc parameter: A description string to display with the progress bar
def my_function(data, progress=gr.Progress()):
progress(0.25, desc="Loading data")
# Load data...
progress(0.50, desc="Processing data")
# Process data...
progress(0.75, desc="Generating results")
# Generate results...
progress(1.0, desc="Complete")
return results
Multi-step progress tracking
For complex operations with multiple stages, you can combine direct progress updates with the tqdm() method:
import gradio as gr
import time
def multi_step_process(items, progress=gr.Progress()):
# Step 1: Validation
progress(0, desc="Validating input")
time.sleep(1)
progress(0.1)
# Step 2: Processing
results = []
for item in progress.tqdm(items[:len(items)//2], desc="Phase 1"):
time.sleep(0.1)
results.append(item)
# Step 3: Final processing
for item in progress.tqdm(items[len(items)//2:], desc="Phase 2"):
time.sleep(0.1)
results.append(item * 2)
progress(1.0, desc="Complete")
return results
demo = gr.Interface(multi_step_process, gr.Textbox(), gr.Textbox())
demo.launch()
Progress bars provide valuable feedback to users during long-running operations, improving the overall user experience of your Gradio app.