Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize python Flask Program

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize the python Flask program". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. First open pycharm, as shown in the figure.

two。 After clicking on create, we created the first Flask program.

From flask import Flask

# create the application object of Flask app = Flask (_ _ name__)

# decorator, the path to bind the view function @ app.route ('/') def hello_world (): # View function return 'HellogrammWorldCraft functions if _ _ name__ = =' _ _ main__': # run the local server to test the flask program app.run () We can first run the project to see the effect, click the run button of the green triangle or the shortcut key ctrl+shift+F10 under pycharm Our first project was up and running. See the following figure:

The prompt after running is shown in the following figure:

According to the prompt, we open a browser, visit the address http://127.0.0.1:5000/( and visit localhost:5000 here.) and guess what you can see? Yes, we can see the output of Hello World!

Our first flask web program is very simple, only one shows Hello World! And the main program app.py only has less than seven lines of code, let's analyze these seven lines of code, the specific code is shown in the figure below.

Hello world of Flask! Brief introduction

The first line of code is to import a Flask class into the flask package

The fourth line of code instantiates an Flask object, that is, creates an application named app, and passes in a parameter of _ _ name__, which is used to specify the name of the application module or package, where _ _ name__ is actually the name of the app.py module, so that flask knows where to find static files, templates and other resources.

The seventh line of code uses a decorator @ app.route, what? You don't know what a decorator is? Then I suggest you take a look at our python basic advanced story about decorators. This decorator tells flask how to handle url routing. The current code has only one'/ 'argument, which is the root route to trigger the hello_world () method. So we typed http://127.0.0.1:5000/ directly into the browser and displayed Hello World!

The next two lines of code define a method that the decorator route route triggers. This method is simple and returns a string that is the information we want the browser to display.

Finally, the run method of the Flask instance object app is used to get the application running. Of course, use if _ _ name__ = ='_ _ main__': before calling the method to ensure that the script runs only if it is executed directly by the interpreter, and will not run if imported as a module.

You can see that this is how our program runs!

Turn on debug mode

Although the run () method works on a local development server, it needs to be manually restarted every time the code is modified, which can be cumbersome and inelegant.

But if you start debug mode, the server will automatically reload after code changes without restarting. And provide a fairly useful debugger when an error occurs.

You can start debug mode in two ways:

From flask import Flask

# create the application object of Flask app = Flask (_ _ name__)

# decorator, the path to bind the view function @ app.route ('/') def hello_world (): # View function return 'HellogrammWorldview functions name__ = =' _ _ main__': # run the local server to test the flask program app.debug = True # debug mode method 1 app.run (debug = True) # debug mode method 2

The effects of these two methods are exactly the same!

You can see that debug mode has been turned on successfully!

This is the end of the content of "how to implement the python Flask Program". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report