Python Flask basic tutorial sample code, pythonflask
This article focuses on the basic tutorial of Python Flask, which is described as follows.
Installation:pip install flaskYou can.
A simple Flask
From flask import Flask # import Flaskapp = Flask (_ name _) # create a Flask instance # Set the route, that is, the url@app.route ('/') # def hello_world (): # return 'Hello World! '# This is not run when the module is imported. For example, if the file is aa. py, the code is executed when python aa. py is used. If it is in another file import, do not execute this file. (This is the basic knowledge of python) if _ name _ = '_ main _': app. run ()Enable debugging mode
Change app. run () to app. run (debug = True. In this way, you do not need to restart the server every time you modify the code.
Running result:
Advantages and disadvantages:
This is basically the case for a simple web build, Which is lightweight. Compared with Django, URLs, views, modles, and templatetags are all customized. Django is set.
However, the Flask learning cost is low. You can get started soon.
The two have their own characteristics. However, on the whole, I prefer Django. After all, it is more standardized.Although Flask is fast for small websites, and the file is relatively simple, there are not as many files as Django. However, it is difficult to add and expand the website later.
In general, there is no good or bad. Only by looking at the needs of the actual situation can we determine the framework to choose. The right is the best.
Summary
This article describes all the sample code in the basic Python Flask tutorial. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!