Home > Backend Development > Python Tutorial > Quickly get started with the Flask framework: Start with installation and quickly get started with the installation process of the Flask framework.

Quickly get started with the Flask framework: Start with installation and quickly get started with the installation process of the Flask framework.

王林
Release: 2024-02-20 08:35:35
Original
1322 people have browsed it

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

  1. Install Python
    Before you start using the Flask framework, you first need to install Python. Flask requires Python 2.7 or Python 3.3 and above. You can download the corresponding installation package from the Python official website (https://www.python.org/downloads/) and follow the prompts to install it.
  2. 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
    Copy after login
  3. Create a virtual environment
    In the command line, enter the project directory and execute the following command to create a virtual environment:

    virtualenv venv
    Copy after login
  4. Activate the virtual environment
    Execute the following command to activate the virtual environment:

    • Windows:

      venvScriptsctivate
      Copy after login
    • Linux/Mac OS:

      source venv/bin/activate
      Copy after login
  5. ##Install Flask

    In the activated virtual environment, execute the following command to install the Flask framework:

    pip install flask
    Copy after login

2. Create a Flask application

    Create an application directory
  1. In the project directory, create a folder named
    app as our Flask application Table of contents.
  2. Create application file

    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()
    Copy after login

  3. Start the application

    Execute the following command to start the Flask application:

    python app.py
    Copy after login

  4. Access the application
  5. Enter
    http:/ in the browser /localhost:5000, you will see the page displays "Hello, Flask!".
3. Using Flask routing

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.'
Copy after login

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.

4. Use template engine

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:

    Create template directory
  1. Create a folder named
    templates in the app directory, use To store HTML template files.
  2. Create template file

    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>
    Copy after login

  3. Modify the application file

    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!')
    Copy after login
  4. Visit the application
  5. Restart the Flask application, and then visit
    http://localhost:5000/, you will see the page showing "Hello, Flask!".
5. Summary

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template