Chapter 15: Web Development with Python
Web development is one of the most popular applications of Python. Frameworks like Flask and Django make it easy to build robust web applications and APIs. This chapter provides an overview of these frameworks and introduces key concepts for web development.
Flask: A Lightweight Web Framework
Flask is a microframework that provides the essentials for building web applications without additional overhead.
Installing Flask
pip install flask
Creating a Basic Flask App
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
@app.route("/")
: Defines the URL path for the route.app.run(debug=True)
: Runs the development server with debugging enabled.
Handling Routes and Parameters
@app.route("/greet/<name>")
def greet(name):
return f"Hello, {name}!"
Django: A Full-Featured Web Framework
Django is a high-level framework that includes built-in support for databases, authentication, and more.
Installing Django
pip install django
Creating a Django Project
Start a project:
django-admin startproject myproject
Navigate to the project directory and run the server:
cd myproject python manage.py runserver
Creating a Django App
Create an app:
python manage.py startapp myapp
Register the app in
INSTALLED_APPS
(insettings.py
):INSTALLED_APPS = [ 'myapp', ]
Define a view in
myapp/views.py
:from django.http import HttpResponse def home(request): return HttpResponse("Hello, Django!")
Map the view to a URL in
myapp/urls.py
:from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]
Include the app's URL configuration in
myproject/urls.py
:from django.urls import include, path urlpatterns = [ path('', include('myapp.urls')), ]
Building REST APIs
REST (Representational State Transfer) APIs allow communication between client and server applications.
Flask API Example
from flask import Flask, jsonify, request
app = Flask(__name__)
data = ["item1", "item2", "item3"]
@app.route("/api/items", methods=["GET"])
def get_items():
return jsonify(data)
@app.route("/api/items", methods=["POST"])
def add_item():
new_item = request.json.get("item")
data.append(new_item)
return jsonify({"message": "Item added", "items": data})
if __name__ == "__main__":
app.run(debug=True)
Django API Example with Django REST Framework
Install Django REST Framework:
pip install djangorestframework
Add it to
INSTALLED_APPS
:INSTALLED_APPS = [ 'rest_framework', ]
Define a serializer in
myapp/serializers.py
:from rest_framework import serializers class ItemSerializer(serializers.Serializer): name = serializers.CharField(max_length=100)
Define a view in
myapp/views.py
:from rest_framework.response import Response from rest_framework.views import APIView class ItemView(APIView): def get(self, request): return Response(["item1", "item2", "item3"]) def post(self, request): return Response({"message": "Item added", "item": request.data})
Add a URL mapping in
myapp/urls.py
:from django.urls import path from .views import ItemView urlpatterns = [ path('api/items/', ItemView.as_view(), name='item-list'), ]
Templates in Flask and Django
Both frameworks support templates for rendering HTML dynamically.
Flask Template Example
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html", title="Welcome")
templates/home.html
:<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>Welcome to Flask!</h1> </body> </html>
Django Template Example
Create a template in
myapp/templates/myapp/home.html
:<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>Welcome to Django!</h1> </body> </html>
Update the view in
myapp/views.py
:from django.shortcuts import render def home(request): return render(request, 'myapp/home.html', {"title": "Welcome"})
Exercises
Exercise 1:
Create a Flask application with routes for displaying a list of items and adding a new item.
Solution:
from flask import Flask, jsonify, request
app = Flask(__name__)
data = []
@app.route("/items", methods=["GET"])
def get_items():
return jsonify(data)
@app.route("/items", methods=["POST"])
def add_item():
item = request.json.get("item")
data.append(item)
return jsonify({"message": "Item added", "items": data})
if __name__ == "__main__":
app.run(debug=True)
Exercise 2:
Create a Django project and app to display a "Hello, World!" message on the homepage.
Solution:
Create a Django project and app (
helloapp
).Define a view in
helloapp/views.py
:from django.http import HttpResponse def home(request): return HttpResponse("Hello, World!")
Map the view in
helloapp/urls.py
and include it in the project URL configuration.
Best Practices
Use virtual environments to isolate project dependencies.
Keep your code organized using the MVC (Model-View-Controller) pattern.
Secure your web applications with proper input validation and error handling.
Use environment variables for sensitive data like API keys and database credentials.
Follow REST principles when designing APIs.
In the next chapter, we will explore data science and machine learning with Python, including libraries like numpy
, pandas
, and scikit-learn
.
Last updated