Home > Article > Backend Development > Quickly get started with the Flask framework: Start with installation and quickly get started with the installation process of the Flask framework.
Quick Start with Flask Framework: Start with Installation
Introduction:
Flask is a simple and flexible Python web framework that is widely used in the field of web development. It provides many useful tools and libraries to make developing web applications efficient and fast. This article will introduce you to how to install the Flask framework and get started quickly.
1. Install the Flask framework
Install virtual environment
Using a virtual environment can effectively isolate the dependency packages required by the project and avoid conflicts with the system environment. Open the command line tool and execute the following command to install the virtual environment:
pip install virtualenv
Create a virtual environment
In the command line, enter the project directory and execute the following command to create a virtual environment:
virtualenv venv
Activate the virtual environment
Execute the following command to activate the virtual environment:
Windows:
venvScriptsctivate
Linux/Mac OS:
source venv/bin/activate
In the activated virtual environment, execute the following command to install the Flask framework:
pip install flask
as our Flask application Table of contents.
Create a Python file named
app.py in the
app directory and write the following code in it:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, Flask!' if __name__ == '__main__': app.run()
Execute the following command to start the Flask application:
python app.py
, you will see the page displays "Hello, Flask!".
The Flask framework allows us to handle different URL requests by defining routes. In the above example, we have defined a route
@app.route('/') which will handle the application root URL request. You can add more routes to handle other URL requests. The following is an example:
@app.route('/') def hello(): return 'Hello, Flask!' @app.route('/about') def about(): return 'This is the About page.' @app.route('/contact') def contact(): return 'This is the Contact page.'You can visit
http://localhost:5000/,
http://localhost:5000/about respectively in the browser and
http://localhost:5000/contact to view the output of different pages.
Flask uses a template engine to render dynamic content. The template engine can combine HTML templates with Python code to generate the final HTML page. The following is an example of using the template engine:
in the
app directory, use To store HTML template files.
Create an HTML template file named
index.html in the
templates directory, and write the following code:
<!DOCTYPE html> <html> <head> <title>Flask Template</title> </head> <body> <h1>{{ message }}</h1> </body> </html>
Modify
app.py, introduce Flask’s
render_template function, and modify the return value of the routing processing function:
from flask import Flask, render_template @app.route('/') def hello(): return render_template('index.html', message='Hello, Flask!')
, you will see the page showing "Hello, Flask!".
This article introduces how to install the Flask framework and get started quickly. You've learned to create a Flask application, use routing to handle URL requests, and use a template engine to render dynamic content. I hope that through the guidance of this article, you can quickly master the basic use of the Flask framework and be able to use it flexibly in future projects. I wish you good luck in learning and getting started with the Flask framework!
The above is the detailed content of Quickly get started with the Flask framework: Start with installation and quickly get started with the installation process of the Flask framework.. For more information, please follow other related articles on the PHP Chinese website!