SoFunction
Updated on 2024-11-20

Python Flask basic tutorial sample code

This article examines the Python Flask Basics tutorial, which is described below.

Installation:pip install flaskcan immediately (do sth)

A simple Flask

from flask import Flask
# Import Flask
app = Flask(__name__)
#Creating an instance of Flask

#Set the route, i.e. url
@('/')
Functions corresponding to #url
def hello_world():
  # returned pages
  return 'Hello World!'

# This is not run when imported as a module, such as this file for, when python executes this code. If it is imported in another file, it is not executed. (This is basic python knowledge.)
if __name__ == '__main__':
  ()

Enable debug mode

Just change () to (debug=True). This way you don't need to restart the server every time after each code change.

Run results:

Pros and Cons:

Basically a simple web build is just that, this is more lightweight. Compared to Django, url, views, modles, templatetags all become customized. Whereas Django is all set.

But Flask is less expensive to learn. It's quick to get started.

Both have their own characteristics. But overall, I prefer Django a little, after all, more standardized.Although Flask for small site development speed is fast, and the file is relatively single, not as many files as Django. But the later site additions and extensions will seem to be a struggle.

Overall, there is no good or bad, only look at the actual situation needs to determine the choice of that frame, the right is the best.

summarize

Meaning line is the entire content of this article on Python Flask basic tutorial sample code, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!