Home  >  Article  >  Backend Development  >  How to install Flask for beginners: Complete Python installation guide

How to install Flask for beginners: Complete Python installation guide

王林
王林Original
2024-02-19 14:25:05613browse

How to install Flask for beginners: Complete Python installation guide

Starting from scratch: A complete guide to installing Flask in Python

Introduction
Flask is a lightweight Python web framework that is widely used to develop simple And flexible web applications. This article will provide you with a complete guide on how to install Flask from scratch and provide some commonly used code examples.

  1. Installing Python
    First, you need to install Python. You can download the latest version of Python from the Python official website (https://www.python.org) and follow the prompts to install it.
  2. Create a virtual environment
    It is recommended that you create a virtual environment before installing Flask to avoid interfering with other projects. Execute the following command in the command line:

    python3 -m venv myenv

    This will create a virtual environment named myenv in the current directory.

  3. Activate virtual environment
    In Windows operating system, you can activate the virtual environment by executing the following command in the command line:

    myenvScriptsctivate

    In macOS and Linux operating systems , use the following command to activate the virtual environment:

    source myenv/bin/activate
  4. Install Flask
    After the virtual environment is activated, execute the following command to install Flask:

    pip install flask

    This will automatically Install Flask and its dependencies.

  5. Create a simple Flask application
    Create a file called app.py and write the following code in it:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
     return 'Hello, Flask!'
    
    if __name__ == '__main__':
     app.run()
  6. Run the Flask application
    Execute the following command in the command line to run the Flask application:

    python app.py

    You will see the following output:

     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

    Now, you can open the browser and visit http://127.0.0.1:5000/, you will see "Hello, Flask!". This means your Flask application is running successfully.

  7. Add routing and view functions
    Flask uses routing to associate URLs with view functions. Edit the app.py file and add the following code to create a new route and view function:

    @app.route('/about')
    def about():
     return 'This is the About page.'

    Now you can visit http://127.0.0.1:5000/about in your browser and you will see Go to "This is the About page."

  8. Using templates
    Flask also supports using templates to render dynamic content. Create a directory called templates and create a file called index.html in it. In index.html, you can edit the following code:

    <!DOCTYPE html>
    <html>
    <head>
     <title>Flask App</title>
    </head>
    <body>
     <h1>Welcome to Flask App!</h1>
     <p>This is a {{ message }}.</p>
    </body>
    </html>

    Next, edit the app.py file and update the view function to use the template:

    from flask import render_template
    
    @app.route('/message')
    def message():
     return render_template('index.html', message='dynamic message')

    Now, in the browser, you can Visit http://127.0.0.1:5000/message and you will see "Welcome to Flask App! This is a dynamic message."

Conclusion
Congratulations on completing the complete guide to installing Flask from scratch. Now, you can continue learning Flask and build feature-rich web applications according to your needs. Hope this article helps you!

The above is the detailed content of How to install Flask for beginners: Complete Python installation guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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