Technologies Used:
- Backend: Python with Flask
- Frontend: HTML, CSS, JavaScript (React or Angular could be used for more advanced features)
- Database: SQLite (for simplicity; for a production system, consider PostgreSQL or MySQL)
- Recommendation Engine: A simple rule-based system (You can extend this with machine learning later on).
Project Structure
luaCopy code/personalized-insurance-products
|-- app.py
|-- templates/
|-- index.html
|-- recommendations.html
|-- static/
|-- style.css
|-- insurance_products.db
Step 1: Set Up Environment
Ensure that Flask is installed. You can install Flask with the following command:
bashCopy codepip install flask
You can also use SQLite, which is built-in with Python, but you could replace it with a more scalable database in a real-world scenario.
Step 2: Build the Backend with Flask
app.py (Main Python Application)
This application will have the following functionalities:
- Collect customer information.
- Generate personalized insurance product recommendations based on the information.
- Store and display the recommended insurance products.
pythonCopy codefrom flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
# Initialize the database
def init_db():
conn = sqlite3.connect('insurance_products.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS insurance_products
(id INTEGER PRIMARY KEY,
product_name TEXT,
description TEXT,
age_range TEXT,
health_condition TEXT,
lifestyle TEXT)''')
# Add some sample products for the sake of the demo
c.execute("INSERT INTO insurance_products (product_name, description, age_range, health_condition, lifestyle) VALUES (?, ?, ?, ?, ?)",
('Health Insurance Basic', 'Basic health insurance plan for young, healthy individuals.', '18-35', 'Healthy', 'Active'))
c.execute("INSERT INTO insurance_products (product_name, description, age_range, health_condition, lifestyle) VALUES (?, ?, ?, ?, ?)",
('Premium Health Insurance', 'Comprehensive coverage for individuals with existing health conditions.', '36-60', 'Pre-existing Conditions', 'Sedentary'))
c.execute("INSERT INTO insurance_products (product_name, description, age_range, health_condition, lifestyle) VALUES (?, ?, ?, ?, ?)",
('Senior Health Insurance', 'Health coverage for seniors, including specialized care and hospital services.', '60+', 'Any', 'Any'))
conn.commit()
conn.close()
# Route to display the form for collecting customer information
@app.route('/')
def index():
return render_template('index.html')
# Route to handle the form submission and generate recommendations
@app.route('/generate_recommendations', methods=['POST'])
def generate_recommendations():
# Collect customer data from the form
age = int(request.form['age'])
health_condition = request.form['health_condition']
lifestyle = request.form['lifestyle']
# Query the database for personalized recommendations based on customer data
conn = sqlite3.connect('insurance_products.db')
c = conn.cursor()
# Generate recommendations based on simple rules (you can add more complex logic here)
age_range = ''
if age <= 35:
age_range = '18-35'
elif 36 <= age <= 60:
age_range = '36-60'
else:
age_range = '60+'
# Get insurance products that match the customer profile
c.execute('''SELECT product_name, description FROM insurance_products
WHERE age_range LIKE ? AND health_condition LIKE ? AND lifestyle LIKE ?''',
(f"%{age_range}%", f"%{health_condition}%", f"%{lifestyle}%"))
recommended_products = c.fetchall()
conn.close()
return render_template('recommendations.html', products=recommended_products)
if __name__ == '__main__':
init_db() # Initialize the database if it doesn't exist
app.run(debug=True)
Explanation of Code:
- Database Initialization (
init_db
):- Creates a SQLite database called
insurance_products.db
if it doesn’t already exist. - Adds sample insurance products for the sake of the demo, with basic attributes such as
product_name
,description
,age_range
,health_condition
, andlifestyle
.
- Creates a SQLite database called
- Home Route (
/
):- Displays a form for collecting customer information (age, health condition, and lifestyle).
- Generate Recommendations (
/generate_recommendations
):- Collects the customer data from the form.
- Based on the customer’s age, health condition, and lifestyle, the backend queries the database and selects relevant insurance products.
- The recommended products are then displayed on a new page.
Step 3: Create the Frontend (HTML)
index.html (Customer Information Form)
This is the form where customers will input their information (age, health condition, lifestyle).
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Personalized Insurance Products</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>Personalized Insurance Products</h1>
<form action="/generate_recommendations" method="POST">
<label for="age">Age:</label>
<input type="number" id="age" name="age" required>
<label for="health_condition">Health Condition:</label>
<select id="health_condition" name="health_condition" required>
<option value="Healthy">Healthy</option>
<option value="Pre-existing Conditions">Pre-existing Conditions</option>
<option value="Any">Any</option>
</select>
<label for="lifestyle">Lifestyle:</label>
<select id="lifestyle" name="lifestyle" required>
<option value="Active">Active</option>
<option value="Sedentary">Sedentary</option>
<option value="Any">Any</option>
</select>
<button type="submit">Get Recommendations</button>
</form>
</div>
</body>
</html>
recommendations.html (Display Recommended Products)
This page displays the insurance products recommended based on the customer data.
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recommended Insurance Products</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>Your Personalized Insurance Recommendations</h1>
{% if products %}
<ul>
{% for product in products %}
<li>
<h3>{{ product[0] }}</h3>
<p>{{ product[1] }}</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>No products match your profile. Please try again with different inputs.</p>
{% endif %}
<a href="/">Go Back</a>
</div>
</body>
</html>
Step 4: Add Styles (CSS)
This CSS file adds simple styles to the web pages.
style.css:
cssCopy codebody {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 60%;
margin: 50px auto;
background-color: white;
padding: 20px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
}
form input, form select, form button {
width: 100%;
padding: 10px;
margin: 10px 0;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
}
ul li {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
}
a {
display: block;
text-align: center;
margin-top: 20px;
color: #4CAF50;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
Leave a Reply