官术网_书友最值得收藏!

  • Flask Framework Cookbook
  • Shalabh Aggarwal
  • 275字
  • 2021-08-05 17:17:19

Introduction

"Flask is a microframework for Python based on Werkzeug, Jinja2 and good intentions."

--Flask official documentation

Why micro? Does it mean that Flask is lacking in functionality or that your complete web application has to mandatorily go inside one file? Not really! It simply refers to the fact that Flask aims at keeping the core of the framework small but highly extensible. This makes writing applications or extensions very easy and flexible and gives developers the power to choose the configurations they want for their application, without imposing any restrictions on the choice of database, templating engine, and so on. In this chapter, you will learn some ways to set up and configure Flask.

Getting started with Flask hardly takes 2 minutes. Setting up a simple Hello World application is as easy as baking a pie:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello to the World of Flask!'

if __name__ == '__main__':
    app.run()

Now, Flask needs to be installed; this can be done simply via pip:

$ pip install Flask

The preceding snippet is a complete Flask-based web application. Here, an instance of the imported Flask class is a Web Server Gateway Interface (WSGI) (module, we set the __name__ string as '__main__'. If we save this in a file with the name app.py, then the application can simply be run using the following command:

$ python app.py
 * Running on http://127.0.0.1:5000/

Now, if we just head over to our browser and type http://127.0.0.1:5000/, we can see our application running.

Tip

Never save your application file as flask.py; if you do so, it will conflict with Flask itself while importing.

主站蜘蛛池模板: 营口市| 宣恩县| 文昌市| 凤庆县| 合江县| 通化市| 威远县| 锦州市| 汉源县| 全椒县| 隆子县| 集贤县| 涪陵区| 霍城县| 定兴县| 清新县| 达尔| 建湖县| 芦山县| 台湾省| 如东县| 朝阳市| 满洲里市| 宾川县| 白玉县| 三都| 呼和浩特市| 深圳市| 旺苍县| 南康市| 濮阳县| 鹿泉市| 丽水市| 葫芦岛市| 连城县| 浑源县| 开鲁县| 泾川县| 泸定县| 洪洞县| 和田市|