Home >Backend Development >Python Tutorial >Quickly build simple web applications using the Flask framework and Python
Use the Flask framework and Python to quickly build simple web applications
With the rapid development of the Internet, web applications have become an indispensable part of our daily lives . Building a simple web application can be achieved by using the Flask framework and Python. This article will introduce the basic use of the Flask framework and how to write code in Python to build a simple web application.
1. Install the Flask framework
Before you start, you need to install the Flask framework. You can install it through the following command:
$ pip install flask
2. Create a simple Web application
First, we need to create a new Python file, such as app.py. Then, add the following code to the file:
from flask import Flask # 创建Flask应用 app = Flask(__name__) # 定义一个路由 @app.route('/') def hello(): return "Hello, Flask!" # 运行应用 if __name__ == '__main__': app.run()
The above code implements a simple Web application. Among them, @app.route('/')
defines a route. When the user accesses the root path, the hello function will be executed and "Hello, Flask!" will be returned.
3. Run the application
After saving the app.py file, you can run the application through the following command:
$ python app.py
After executing the above command, something similar to the following will be output:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
This shows that the application is already running locally. You can access the application by entering http://127.0.0.1:5000/ in the browser, and you will see the return result of "Hello, Flask!".
4. Processing GET requests and POST requests
In addition to simply returning text, we can also handle different request methods. For example, we can add the following code in the hello function:
from flask import request @app.route('/', methods=['GET', 'POST']) def hello(): if request.method == 'POST': name = request.form.get('name') return f"Hello, {name}!" else: return "Hello, Flask!"
In the above code, we use the request
object to obtain the data passed by the client. When the request method is POST, we get the parameter named 'name' from the requested form and return "Hello, {name}!"; when the request method is GET, we return "Hello, Flask!".
5. Static file processing
In addition to processing requests, we can also add static files, such as CSS, JavaScript, etc., to the Flask application. Just create a folder called 'static' in the root directory of your app and place your static files in that folder. For example, we can create a CSS file called 'style.css' and then add the following code to the hello function:
@app.route('/') def hello(): # ... return ''' <html> <head> <link rel="stylesheet" href="/static/style.css"> </head> <body> <h1>Hello, Flask!</h1> </body> </html> '''
In the above code, we used <link>
element to link static CSS files.
6. Template engine
In actual development, we usually use template engines to dynamically generate HTML pages. The Flask framework has a built-in Jinja2 template engine, which can be created by creating a folder named 'templates' in the root directory of the application and placing the template files in the folder.
For example, we can create a template file named 'hello.html' and then add the following code to the hello function:
from flask import render_template @app.route('/') def hello(): return render_template('hello.html', name='Flask')
In the above code, we used The render_template
function renders the template file named 'hello.html' and passes a parameter named 'name'.
7. Summary
This article briefly introduces how to quickly build a simple web application using the Flask framework and Python, and gives specific code examples. Through the Flask framework, we can easily build a web application and handle various types of requests, introduce static files, and use a template engine to generate dynamic pages. Using these features, we can develop various types of Web applications more efficiently.
The above is the detailed content of Quickly build simple web applications using the Flask framework and Python. For more information, please follow other related articles on the PHP Chinese website!