File size: 9,862 Bytes
667bab7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | # Phase 3: Web Application with Flask
"""
Phase 3: Simple Web Application with Flask
Learning Objectives:
- HTTP requests and responses
- Routing
- Templates
- Building a complete web app
"""
from flask import Flask, render_template_string, request, jsonify, redirect, url_for
app = Flask(__name__)
# ============================================================
# LESSON 1: Basic Routes and Responses
# ============================================================
@app.route('/')
def home():
"""Home page"""
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Lao Python Web App - Lesson 1</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
h1 { color: #2c3e50; }
.lesson { background: #f8f9fa; padding: 15px; margin: 10px 0; border-radius: 5px; }
</style>
</head>
<body>
<h1>π Lao Python Web Application</h1>
<div class="lesson">
<h2>Lesson 1: Basic Routes</h2>
<p>Routes map URLs to Python functions:</p>
<pre><code>@app.route('/')
def home():
return "Hello World!"</code></pre>
</div>
<h3>Navigation:</h3>
<a href="/about">About</a> |
<a href="/contact">Contact</a> |
<a href="/api/data">API Data</a>
</body>
</html>
'''
return html
@app.route('/about')
def about():
"""About page"""
return '<h1>About</h1><p>This is a Flask web application for learning Python.</p>'
@app.route('/contact')
def contact():
"""Contact page"""
return '''
<h1>Contact</h1>
<form method="POST" action="/submit">
<input type="text" name="name" placeholder="Your Name"><br>
<input type="email" name="email" placeholder="Your Email"><br>
<textarea name="message" placeholder="Your Message"></textarea><br>
<button type="submit">Send</button>
</form>
'''
@app.route('/submit', methods=['POST'])
def submit():
"""Handle form submission"""
name = request.form.get('name', 'Anonymous')
return f'<h1>Thanks {name}!</h1><p>Your message has been received.</p><a href="/">Back</a>'
# ============================================================
# LESSON 2: Templates
# ============================================================
TEMPLATE = '''
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.card { background: white; padding: 20px; margin: 10px 0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.btn { display: inline-block; padding: 10px 20px; background: #3498db; color: white; text-decoration: none; border-radius: 5px; }
</style>
</head>
<body>
<h1>{{ title }}</h1>
<div class="card">
<h2>{{ heading }}</h2>
<p>{{ content }}</p>
</div>
{% if items %}
<div class="card">
<h2>Items</h2>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<a href="/" class="btn">Home</a>
</body>
</html>
'''
@app.route('/template-demo')
def template_demo():
"""Demonstrate template rendering"""
items = ["Python", "Flask", "HTML", "CSS", "JavaScript"]
return render_template_string(TEMPLATE,
title="Template Demo",
heading="Passing Variables to Templates",
content="Flask templates make it easy to separate logic from presentation.",
items=items)
# ============================================================
# LESSON 3: API Endpoints
# ============================================================
# In-memory "database"
tasks = []
@app.route('/api/tasks', methods=['GET'])
def get_tasks():
"""Get all tasks"""
return jsonify(tasks)
@app.route('/api/tasks', methods=['POST'])
def create_task():
"""Create a new task"""
data = request.get_json()
task = {
"id": len(tasks) + 1,
"title": data.get("title", ""),
"completed": False
}
tasks.append(task)
return jsonify(task), 201
@app.route('/api/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
"""Update a task"""
for task in tasks:
if task["id"] == task_id:
data = request.get_json()
task["title"] = data.get("title", task["title"])
task["completed"] = data.get("completed", task["completed"])
return jsonify(task)
return jsonify({"error": "Task not found"}), 404
@app.route('/api/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
"""Delete a task"""
global tasks
tasks = [t for t in tasks if t["id"] != task_id]
return jsonify({"message": "Task deleted"})
@app.route('/api/data')
def api_data():
"""Simple API endpoint"""
return jsonify({
"message": "Hello from Flask API!",
"timestamp": "2026-10-25",
"version": "1.0"
})
# ============================================================
# LESSON 4: Full Web App - Task Manager
# ============================================================
TASK_MANAGER_TEMPLATE = '''
<!DOCTYPE html>
<html>
<head>
<title>Task Manager - Lesson 4</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.task { display: flex; justify-content: space-between; align-items: center;
padding: 15px; margin: 10px 0; background: #f8f9fa; border-radius: 5px; }
.task.completed { text-decoration: line-through; opacity: 0.6; }
.btn { padding: 8px 15px; margin: 0 5px; border: none; border-radius: 3px; cursor: pointer; }
.btn-primary { background: #3498db; color: white; }
.btn-danger { background: #e74c3c; color: white; }
.btn-success { background: #2ecc71; color: white; }
input[type="text"] { padding: 10px; width: 70%; border: 1px solid #ddd; border-radius: 3px; }
</style>
</head>
<body>
<h1>π Task Manager</h1>
<div class="add-task">
<form method="POST" action="/tasks">
<input type="text" name="title" placeholder="Add new task..." required>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<div class="tasks">
{% for task in tasks %}
<div class="task {% if task.completed %}completed{% endif %}">
<span>{{ task.title }}</span>
<div>
<a href="/tasks/{{ task.id }}/toggle" class="btn btn-success">β</a>
<a href="/tasks/{{ task.id }}/delete" class="btn btn-danger">β</a>
</div>
</div>
{% endfor %}
</div>
<p>Total: {{ tasks|length }} tasks | Completed: {{ tasks|selectattr("completed")|list|length }} tasks</p>
</body>
</html>
'''
@app.route('/tasks', methods=['GET'])
def list_tasks():
"""List all tasks"""
return render_template_string(TASK_MANAGER_TEMPLATE, tasks=tasks)
@app.route('/tasks', methods=['POST'])
def add_task():
"""Add a new task"""
title = request.form.get('title', '').strip()
if title:
tasks.append({
"id": len(tasks) + 1,
"title": title,
"completed": False
})
return redirect(url_for('list_tasks'))
@app.route('/tasks/<int:task_id>/toggle', methods=['GET'])
def toggle_task(task_id):
"""Toggle task completion"""
for task in tasks:
if task["id"] == task_id:
task["completed"] = not task["completed"]
break
return redirect(url_for('list_tasks'))
@app.route('/tasks/<int:task_id>/delete', methods=['GET'])
def delete_task_route(task_id):
"""Delete a task"""
global tasks
tasks = [t for t in tasks if t["id"] != task_id]
return redirect(url_for('list_tasks'))
# ============================================================
# LESSON 5: Integration
# ============================================================
@app.route('/dashboard')
def dashboard():
"""Dashboard showing all info"""
completed = len([t for t in tasks if t["completed"]])
return jsonify({
"total_tasks": len(tasks),
"completed_tasks": completed,
"pending_tasks": len(tasks) - completed,
"message": "Welcome to your Python Dashboard!"
})
@app.errorhandler(404)
def not_found(e):
"""Handle 404 errors"""
return jsonify({"error": "Page not found"}), 404
# ============================================================
# RUN THE APP
# ============================================================
def run_web_app():
"""Run the Flask web application"""
print("=" * 50)
print("π Flask Web Application")
print("=" * 50)
print("\nRoutes:")
print(" GET / - Home page")
print(" GET /about - About page")
print(" GET /contact - Contact form")
print(" POST /submit - Submit contact form")
print(" GET /template-demo - Template example")
print(" GET /api/data - API endpoint")
print(" GET /tasks - Task manager")
print(" POST /tasks - Add task")
print(" GET /dashboard - Dashboard")
print("\nAPI Endpoints:")
print(" GET /api/tasks - List tasks")
print(" POST /api/tasks - Create task")
print(" PUT /api/tasks/<id> - Update task")
print(" DELETE /api/tasks/<id> - Delete task")
print("\n" + "=" * 50)
print("Starting Flask server on http://localhost:5000")
print("=" * 50)
app.run(debug=True, host='0.0.0.0', port=5000)
if __name__ == '__main__':
run_web_app()
|