With the development of the Internet, more and more companies and individuals are beginning to use Python for Web development. In the field of Python's web development, Flask is a web development framework that has attracted much attention. This article will introduce the basic concepts and usage of Flask.
1. What is Flask?
Flask in Python is a lightweight web development framework developed by Armin Ronacher and named after the lightweight one-way cruet. Flask is developed based on two Python libraries, Werkzeug and Jinja2. Werkzeug provides an underlying web framework and a WSGI toolkit, while Jinja2 is a well-known template engine.
2. Features of Flask
Before introducing how to use Flask, let’s take a look at the features of Flask:
3. How to use Flask
After understanding the characteristics of Flask, let’s take a look at how to use Flask:
Before using Flask, you need to install Flask first. You can use the pip command to install it:
pip install flask
Flask uses an application instance to handle web requests. The code to create an application instance is as follows:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello World!" if __name__ == '__main__': app.run()
In the above code, we first imported Flask, then created an application instance, and then used the @app.route decorator to define a URL rule and specify the return value. Finally, start the application through app.run().
In the application's root directory, run the following command to start the application:
python app.py
Open the browser and enter http://127.0.0.1:5000/ in the address bar to see the "Hello World!" output.
In addition to outputting simple information such as "Hello World!", we can also respond to GET/POST and other requests. The following is an example of responding to a POST request:
from flask import Flask, request app = Flask(__name__) @app.route('/', methods=['POST']) def hello(): username = request.form['username'] return "Hello, {}!".format(username) if __name__ == '__main__': app.run()
In the above code, we specify the request method as POST in the @app.route decorator, and use request.form to obtain the parameters in the request. Then return "Hello, {}!", where {} will be replaced by the user name by the format() function.
Flask provides the Jinja2 template engine, which allows us to generate Web pages more easily. The following is an example of using the Jinja2 template engine:
from flask import Flask, render_template app = Flask(__name__) @app.route('/user/') def user(name): return render_template('user.html', name=name) if __name__ == '__main__': app.run()
In the above code, we define a function user() and use a dynamic parameter name in the URL rule. In the function, we use the render_template() function to generate the web page. The render_template() function will generate HTML code based on the template file user.html, where {{name}} will be replaced with the value of the name parameter.
4. Summary
This article introduces the basic concepts and usage of Flask, the web development framework in Python. Flask is a lightweight web framework that is easy to learn and use and has strong scalability. Through the introduction of this article, I believe readers can master the development of Flask more easily.
The above is the detailed content of Web development framework Flask in Python. For more information, please follow other related articles on the PHP Chinese website!