Boost ML inference UX with our step-by-step guide to add a progress bar for long tasks—fast and engaging solution.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Example: Python pseudo-code for ML inference with progress updates
import time
import random
# Simulated function to update progress into a shared store (e.g., Redis or in-memory dict)
def update_progress(task_id, percentage):
// Save the new percentage value for the given task\_id
progress_store[task_id] = percentage
def long_ml_inference(task\_id, data):
total\_steps = 10 // Define number of steps in the inference process
for step in range(total\_steps):
// Simulate ML processing time for each step
time.sleep(random.uniform(0.5, 1.5))
// Calculate the current progress percentage
percentage = int(((step + 1) / total\_steps) \* 100)
update_progress(task_id, percentage)
return "Inference Complete"
# Global dictionary to simulate shared store for progress status
progress\_store = {}
# Start inference with a unique task id (in an actual deployment, this might be managed by a task queue like Celery)
task_id = "unique_task\_123"
result = long_ml_inference(task\_id, data={})
// HTML structure for the progress bar
<div id="progress-container" style="width: 100%; background-color: #eee;">
<div id="progress-bar" style="width: 0%; background-color: #5cb85c; height: 30px;"></div>
</div>
// JavaScript code to poll the progress status via AJAX (using fetch API)
function pollProgress(taskId) {
fetch('/progress?task\_id=' + taskId)
.then(response => response.json())
.then(data => {
// Update the progress bar width
document.getElementById("progress-bar").style.width = data.percentage + "%";
// Check if the task is complete
if (data.percentage < 100) {
// Continue polling every second until complete
setTimeout(function() {
pollProgress(taskId);
}, 1000);
} else {
alert("ML Inference Completed!");
}
})
.catch(error => {
console.error("Error fetching progress data:", error);
});
}
// Start polling for a given task ID
const taskId = "unique_task_123";
pollProgress(taskId);
// Example: Node.js server using Express and Socket.IO
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
app.get('/', (req, res) => {
res.sendFile(\_\_dirname + '/index.html'); // Serve homepage with progress bar
});
// Simulate ML inference task with periodic progress updates
function simulateMLTask(taskId, socket) {
let current = 0;
const total = 10;
const interval = setInterval(() => {
current++;
const percentage = Math.round((current / total) \* 100);
socket.emit('progressUpdate', { taskId: taskId, percentage: percentage });
if (current >= total) {
clearInterval(interval);
socket.emit('progressUpdate', { taskId: taskId, percentage: 100, message: "Inference Complete" });
}
}, 1000);
}
io.on('connection', (socket) => {
console.log('Client connected');
// Listen for a start task event from the client
socket.on('startTask', (data) => {
simulateMLTask(data.taskId, socket);
});
});
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
// In the HTML file (index.html), include Socket.IO client script
<script src="/socket.io/socket.io.js"></script>
<div id="progress-container" style="width: 100%; background-color: #eee;">
<div id="progress-bar" style="width: 0%; background-color: #5cb85c; height: 30px;"></div>
</div>
<script>
var socket = io(); // Connect to the backend using Socket.IO
var taskId = "unique_task_123";
// Start the ML inference task and progress updates
socket.emit('startTask', { taskId: taskId });
// Listen for progress updates from the server
socket.on('progressUpdate', function(data) {
if (data.taskId === taskId) {
document.getElementById("progress-bar").style.width = data.percentage + "%";
if (data.percentage === 100 && data.message) {
alert(data.message);
}
}
});
</script>
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.