Step-by-step guide: Build a Flask ML app with Bootstrap UI. Get code examples, best practices & project insights.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// File structure layout:
// - app.py // Main Flask application that defines routes and integrates ML model.
// - model.pkl // Serialized machine learning model (using pickle or joblib).
// - templates/
// - index.html // HTML template using Bootstrap for styling.
// - static/
// - css/
// - bootstrap.min.css // Bootstrapped CSS files for UI styling.
// - js/
// - bootstrap.min.js // Bootstrapped JavaScript files for interactive components.
app.py, load the model once during startup so that it can be used in subsequent prediction requests, avoiding the overhead of loading the model multiple times.
// Import necessary libraries
from flask import Flask, request, render\_template
import pickle // For deserializing the machine learning model
// Initialize Flask app
app = Flask(name)
// Load the serialized ML model
with open('model.pkl', 'rb') as file:
model = pickle.load(file) // The model is now loaded and available for predictions
templates folder, create an index.html file that uses HTML and Bootstrap classes to provide a clean form for user input, display results, and handle errors gracefully.
ML Prediction
Enter Data for Prediction
{% if prediction is defined %}
Prediction: {{ prediction }}
{% endif %}
// Define the main route for GET and POST requests
@app.route('/', methods=['GET', 'POST'])
def index():
prediction = None // Initialize prediction variable
if request.method == 'POST':
// Retrieve data from the form
input\_data = request.form.get('inputData')
// Data preprocessing: convert input string to the appropriate format
// For this example, assume the model expects a float value wrapped in a list.
try:
processed_input = [float(input_data)]
except ValueError:
processed\_input = None
// Perform prediction if preprocessing is successful
if processed\_input is not None:
prediction = model.predict([processed\_input])[0] // Make a prediction using the ML model
// Render the template with the prediction result
return render\_template('index.html', prediction=prediction)
// Run the Flask application
if name == "main":
app.run(debug=True)
// Example JavaScript snippet for AJAX form submission
document.addEventListener('DOMContentLoaded', function() {
// Select the form element
const form = document.querySelector('form');
form.addEventListener('submit', function(e) {
e.preventDefault(); // Prevent full page reload
// Prepare form data
let formData = new FormData(form);
// Execute the fetch request to the server
fetch('/', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(html => {
// Update a specific part of the page with the response content (assuming specific div id is present)
document.querySelector('#result').innerHTML = html;
})
.catch(error => console.error('Error:', error));
});
});
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.Â