code a flight listing software
- User
code a flight listing software
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code a flight listing software
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
# app.py
from flask import Flask, render_template, request
from datetime import datetime, timedelta
import random
app = Flask(__name__)
# In-memory flight database
flights = []
# Sample data
airlines = ["Delta", "United", "American", "Southwest", "JetBlue", "Alaska"]
airports = {
"ATL": "Atlanta", "LAX": "Los Angeles", "ORD": "Chicago",
"DFW": "Dallas/Fort Worth", "DEN": "Denver", "JFK": "New York",
"SFO": "San Francisco", "SEA": "Seattle", "MIA": "Miami", "BOS": "Boston"
}
# Generate random flights
def generate_flights():
flight_ids = set()
today = datetime.now()
for _ in range(50):
# Generate unique flight ID
airline = random.choice(airlines)
flight_number = random.randint(100, 9999)
flight_id = f"{airline[:2].upper()}{flight_number}"
if flight_id in flight_ids:
continue
flight_ids.add(flight_id)
# Random origin and destination
airport_codes = list(airports.keys())
origin = random.choice(airport_codes)
destination = random.choice([code for code in airport_codes if code != origin])
# Random times and prices
departure_date = today + timedelta(days=random.randint(0, 7))
departure_time = departure_date.replace(
hour=random.randint(0, 23),
minute=random.choice([0, 15, 30, 45])
)
duration_minutes = random.randint(60, 360)
arrival_time = departure_time + timedelta(minutes=duration_minutes)
price = round(random.uniform(100, 1000), 2)
flights.append({
"flight_id": flight_id,
"airline": airline,
"origin": origin,
"origin_city": airports[origin],
"destination": destination,
"destination_city": airports[destination],
"departure_time": departure_time,
"arrival_time": arrival_time,
"duration_minutes": duration_minutes,
"price": price,
"seats_available": random.randint(0, 50)
})
generate_flights()
@app.route('/')
def index():
return render_template('index.html', airports=airports)
@app.route('/flights')
def list_flights():
origin = request.args.get('origin', '')
destination = request.args.get('destination', '')
date_str = request.args.get('date', '')
filtered_flights = flights
if origin:
filtered_flights = [f for f in filtered_flights if f['origin'] == origin]
if destination:
filtered_flights = [f for f in filtered_flights if f['destination'] == destination]
if date_str:
try:
search_date = datetime.strptime(date_str, '%Y-%m-%d').date()
filtered_flights = [f for f in filtered_flights if f['departure_time'].date() == search_date]
except ValueError:
pass
filtered_flights = sorted(filtered_flights, key=lambda x: x['departure_time'])
return render_template('flights.html',
flights=filtered_flights,
airports=airports,
origin=origin,
destination=destination,
date=date_str)
@app.route('/flight/<flight_id>')
def flight_details(flight_id):
flight = next((f for f in flights if f['flight_id'] == flight_id), None)
if flight:
return render_template('flight_detail.html', flight=flight)
else:
return "Flight not found", 404
if __name__ == '__main__':
app.run(debug=True)
Now let's create the HTML templates:
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flight Finder</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>Flight Finder</h1>
<div class="search-box">
<form action="{{ url_for('list_flights') }}" method="get">
<div class="form-group">
<label for="origin">Origin:</label>
<select name="origin" id="origin">
<option value="">Any</option>
{% for code, city in airports.items() %}
<option value="{{ code }}">{{ code }} - {{ city }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="destination">Destination:</label>
<select name="destination" id="destination">
<option value="">Any</option>
{% for code, city in airports.items() %}
<option value="{{ code }}">{{ code }} - {{ city }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="date">Date:</label>
<input type="date" id="date" name="date">
</div>
<button type="submit" class="search-btn">Search Flights</button>
</form>
</div>
</div>
</body>
</html>
<!-- templates/flight_detail.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flight Details - {{ flight.flight_id }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>Flight Details</h1>
<div class="flight-detail-card">
<div class="flight-header">
<h2>{{ flight.flight_id }} - {{ flight.airline }}</h2>
</div>
<div class="flight-route-detail">
<div class="origin-detail">
<h3>Departure</h3>
<div class="airport">{{ flight.origin }} - {{ flight.origin_city }}</div>
<div class="datetime">
<div class="time">{{ flight.departure_time.strftime('%H:%M') }}</div>
<div class="date">{{ flight.departure_time.strftime('%B %d, %Y') }}</div>
</div>
</div>
<div class="flight-duration-detail">
<div class="duration-label">Flight Duration</div>
<div class="duration">{{ flight.duration_minutes // 60 }} hours {{ flight.duration_minutes % 60 }} minutes</div>
<div class="line">———————→</div>
</div>
<div class="destination-detail">
<h3>Arrival</h3>
<div class="airport">{{ flight.destination }} - {{ flight.destination_city }}</div>
<div class="datetime">
<div class="time">{{ flight.arrival_time.strftime('%H:%M') }}</div>
<div class="date">{{ flight.arrival_time.strftime('%B %d, %Y') }}</div>
</div>
</div>
</div>
<div class="flight-additional-info">
<div class="info-item">
<span class="label">Price:</span>
<span class="value">${{ "%.2f"|format(flight.price) }}</span>
</div>
<div class="info-item">
<span class="label">Available Seats:</span>
<span class="value">{{ flight.seats_available }}</span>
</div>
</div>
<div class="booking-section">
<button class="book-btn">Book This Flight</button>
</div>
</div>
<div class="back-links">
<a href="{{ url_for('list_flights') }}">Back to Results</a> |
<a href="{{ url_for('index') }}">New Search</a>
</div>
</div>
</body>
</html>
Finally, let's create the CSS:
/* static/style.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
background-color: #f5f5f5;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #2c3e50;
margin-bottom: 30px;
text-align: center;
}
.search-box {
background-color: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 30px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
select, input[type="date"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.search-btn {
background-color: #3498db;
color: white;
border: none;
padding: 12px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
}
.flight-list {
display: flex;
flex-direction: column;
gap: 20px;
}
.flight-card {
background-color: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.flight-header {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
}
.flight-route {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.airport-code {
font-size: 24px;
font-weight: bold;
}
.flight-info {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid #eee;
}
.price {
font-size: 24px;
font-weight: bold;
color: #27ae60;
}
.detail-btn {
background-color: #3498db;
color: white;
text-decoration: none;
padding: 8px 15px;
border-radius: 4px;
}
.book-btn {
background-color: #27ae60;
color: white;
border: none;
padding: 12px 25px;
border-radius: 4px;
cursor: pointer;
font-size: 18px;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.flight-route {
flex-direction: column;
gap: 20px;
}
.flight-info {
flex-direction: column;
gap: 15px;
text-align: center;
}
}
Would you like me to explain or break down the code?
<!-- templates/flights.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flight Results</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>Flight Results</h1>
<div class="search-box">
<form action="{{ url_for('list_flights') }}" method="get">
<div class="form-group">
<label for="origin">Origin:</label>
<select name="origin" id="origin">
<option value="">Any</option>
{% for code, city in airports.items() %}
<option value="{{ code }}" {% if code == origin %}selected{% endif %}>{{ code }} - {{ city }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="destination">Destination:</label>
<select name="destination" id="destination">
<option value="">Any</option>
{% for code, city in airports.items() %}
<option value="{{ code }}" {% if code == destination %}selected{% endif %}>{{ code }} - {{ city }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="date">Date:</label>
<input type="date" id="date" name="date" value="{{ date }}">
</div>
<button type="submit" class="search-btn">Search Flights</button>
</form>
</div>
<div class="results">
<h2>Found {{ flights|length }} flights</h2>
{% if flights %}
<div class="flight-list">
{% for flight in flights %}
<div class="flight-card">
<div class="flight-header">
<span class="flight-id">{{ flight.flight_id }}</span>
<span class="airline">{{ flight.airline }}</span>
</div>
<div class="flight-route">
<div class="origin">
<div class="airport-code">{{ flight.origin }}</div>
<div class="airport-city">{{ flight.origin_city }}</div>
<div class="time">{{ flight.departure_time.strftime('%H:%M') }}</div>
<div class="date">{{ flight.departure_time.strftime('%b %d, %Y') }}</div>
</div>
<div class="flight-duration">
<div class="duration">{{ flight.duration_minutes // 60 }}h {{ flight.duration_minutes % 60 }}m</div>
<div class="line">———————→</div>
</div>
<div class="destination">
<div class="airport-code">{{ flight.destination }}</div>
<div class="airport-city">{{ flight.destination_city }}</div>
<div class="time">{{ flight.arrival_time.strftime('%H:%M') }}</div>
<div class="date">{{ flight.arrival_time.strftime('%b %d, %Y') }}</div>
</div>
</div>
<div class="flight-info">
<div class="price">${{ "%.2f"|format(flight.price) }}</div>
<div class="seats">{{ flight.seats_available }} seats left</div>
<a href="{{ url_for('flight_details', flight_id=flight.flight_id) }}" class="detail-btn">View Details</a>
</div>
</div>
{% endfor %}
</div>
{% else %}
<div class="no-results">No flights found matching your criteria.</div>
{% endif %}
</div>
<div class="back-link">
<a href="{{ url_for('index') }}">Back to Search</a>
</div>
</div>
</body>
</html>