📜  flask minimul 应用程序 - Python (1)

📅  最后修改于: 2023-12-03 15:15:05.692000             🧑  作者: Mango

Flask Minimal Application - Python

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. Thus allowing the developer to have flexibility when it comes to building web applications. In this tutorial, we will create a Flask minimal application.


Setting up the environment

The first step is to set up the environment. This includes installing the necessary packages and libraries. We will use pipenv to create a virtual environment and install Flask. Run the following commands in your terminal:

$ pip install pipenv
$ pipenv shell
$ pipenv install flask

Creating the application

Now that the environment is set up, we can proceed to create the application. Open up a new file called app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, World!'

This code imports Flask and creates an instance of the Flask class. It then sets up a decorator that creates a route for the root URL and returns a "Hello, World!" message.


Running the application

To run the application, simply run the following command in your terminal:

$ FLASK_APP=app.py flask run

This will start a development server at http://127.0.0.1:5000/. You should see the "Hello, World!" message in your browser if you visit that URL.


Conclusion

In this tutorial, we have created a Flask minimal application. We set up the environment, created the application, and ran the server. Flask is a very powerful tool and can be used to create complex web applications. For more information, check out the Flask documentation.