We need to test our python flask set up before we move further and learn a big thing. To try out the setup that we did in the last part of this tutorial, we need to create a small “Hello World” program and run it on the browser to check if everything is working well. So let’s begin.
Write the following code in your text editor, I prefer Sublime text.
Python Flask Code
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World’ if __name__ == '__main__': app.run()
What does the above code do? Well, the above code creates a Flask class object. Then it calls the constructor and passes the current module name (__name__). Once the constructor is called, a route() function is called which defines the calling of functions for each URL. In other words, route() tells what has to be done when some URL is requested by the client.
We will go through each line of statement one by one but before that, we need to test if the Flask is working as per our requirements or not! So, to run the above project, go to terminal/command prompt and type the below code.
NOTE: hello.py is the name of the file in which we will write the above code.
Once the above code is run, you will get a message on the console like the one below:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
To see the output on the browser, navigate to the URL shown in the console and see if everything seems to be good.
Now, once everything is working fine, let us go through some lines of codes in the above snippet.
The route function
The route function takes in two parameters to define the work flow of the requested URL.
- The rule – this represents defines the URL binding with the function.
- The options – this is a list of parameters to be forwarded to the underlying Rule object
In the example demonstrated above, The ‘/’ URL is bound with hello_world() function. So, when the home page is requested by the client, the output of this function will be rendered.
The app.run function
app.run(host, port, debug, options)
The app.run function takes in four parameters, all of which is optional to provide.
- host – Hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to ‘0.0.0.0’ to have the server available externally.
- port – Defaults to 5000.
- debug – Defaults to false. If set to true, provides debug information.
- options – To be forwarded to the underlying Werkzeug server.
In Flask application development, we need to start/run the application by calling run() method. While developing the application, you will need to restart the application multiple times manually as the changes will be reflected only if the script is restarted. To avoid this, while the project is in a development phase, you can pass debug = True in run() function to track the error, if any, and avoid restarting the project each time you make changes in the code.
app.debug = True app.run() app.run(debug = True)
Try making some changes in the first program you wrote, and try reading out the errors, if any, and see the changes in output.
Stay tuned, you will love things coming up in this tutorial series….