This document explains how to install Flask, create and deploy a Python web application, and use Flask's features for CRUD operations and template rendering.
This document covers installing Flask, creating and deploying a Python web application, and using Flask's features for CRUD operations and template rendering. It includes step-by-step instructions and code examples for building and running Flask apps.
Flask is a micro-framework for building web applications quickly and easily with Python. It supports CRUD operations—Create, Read, Update, and Delete—using HTTP methods such as POST, GET, PUT, PATCH, and DELETE.
| HTTP Method | Operation | Typical Use |
|---|---|---|
| POST | Create | Add new data or objects |
| GET | Read | Retrieve data from server |
| PUT/PATCH | Update | Modify existing data |
| DELETE | Delete | Remove data or objects |
Most web applications use POST for creating, updating, and deleting, and GET for reading data.
To get started, install Flask using pip:
1pip install flask
Next, import Flask, instantiate the Flask class, define routes, and run the application. Below is a minimal example that returns “Hello World” for a GET request:
1from flask import Flask
2
3app = Flask("MyFirstWebApplication")
4
5@app.route('/')
6def hello():
7 return 'Hello World!'
8
9if __name__ == '__main__':
10 app.run(debug=True)
When the server starts, it displays the IP address and port (default: 127.0.0.1:5000). Access this endpoint in a browser to see the response.
Flask supports both static and dynamic HTML templates. By default, templates are stored in a templates directory, and static files (images, CSS, JS) are stored in a static directory. Dynamic templates can use arguments passed via the URL or request parameters.
1from flask import Flask, render_template, request
2
3app = Flask("MyFirstApplication", static_folder='static')
4
5@app.route('/sample')
6def get_sample_html():
7 return render_template('sample.html')
8
9@app.route('/user/<username>', methods=['GET'])
10def user_profile(username):
11 return render_template('user.html', username=username)
12
13@app.route('/user', methods=['GET'])
14def user_param():
15 username = request.args.get('username')
16 return render_template('user.html', username=username)
/sample renders a static HTML page./user/<username> renders a dynamic page using a URL parameter./user?username=... renders a dynamic page using a request parameter.Flask enables rapid development and deployment of Python web applications. It supports CRUD operations, easy installation, and flexible template rendering for both static and dynamic content.
(2) Flask is a micro-framework for creating web applications quickly and easily with Python.
pip install flask.| Method | Operation |
|---|---|
| A. POST | 1. Read |
| B. GET | 2. Create |
| C. PUT | 3. Update |
| D. DELETE | 4. Delete |
A-2, B-1, C-3, D-4.
(4) Templates can use arguments passed via the URL or request parameters.
Flask supports CRUD operations using HTTP methods such as POST, GET, PUT, PATCH, and DELETE.
True. Flask supports all standard CRUD operations using these HTTP methods.