star rating form in flask

# Import necessary libraries
from flask import Flask, render_template, request

# Create a Flask web application
app = Flask(__name__)

# Define a route for the home page
@app.route('/')
def index():
    return render_template('index.html')

# Define a route for the star rating form
@app.route('/submit_rating', methods=['POST'])
def submit_rating():
    # Get the rating value from the form submission
    rating = request.form.get('rating')

    # Process the rating value (you can save it to a database or perform any other desired operation)
    # For demonstration purposes, we'll just print the rating to the console
    print(f'User submitted rating: {rating}')

    # Return a response (you can customize this based on your requirements)
    return 'Rating submitted successfully!'

# Run the Flask application
if __name__ == '__main__':
    app.run(debug=True)
<!-- Create a template file (index.html) for the home page with a simple star rating form -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Star Rating Form</title>
</head>
<body>
    <h1>Star Rating Form</h1>
    <form action="/submit_rating" method="post">
        <label for="rating">Rate us:</label>
        <!-- Create a star rating input using radio buttons -->
        <input type="radio" name="rating" value="5" id="star5"><label for="star5">5 stars</label>
        <input type="radio" name="rating" value="4" id="star4"><label for="star4">4 stars</label>
        <input type="radio" name="rating" value="3" id="star3"><label for="star3">3 stars</label>
        <input type="radio" name="rating" value="2" id="star2"><label for="star2">2 stars</label>
        <input type="radio" name="rating" value="1" id="star1"><label for="star1">1 star</label>
        <br>
        <input type="submit" value="Submit Rating">
    </form>
</body>
</html>