Flask "Hello, World" What is happening?
When you run your first Flask application what exactly will happen in your codes?
Every time we want to learn a new programming language or framework we do this by running the Hello, world! Application. It is too easy for doing this in python language but when it’s time to do this on a framework like Flask, there are so many things that you need to know, of course, Flask is a minimal framework but in this situation, you need to have a basic knowledge about what is going on. This is a simple hello_world application in the Flask:
from flask import Flask app = Flask(__name__) @app.route('/index') def index(): Return ('Hello, World!') if __name__ == '__main__': app.run(debug=True)
First, if you want to run this script you need to do these things: Install Python3 on your machine Using pip, install flask Check the flask is installed correctly via flask --version in terminal Make a file called app.py and put the above code into it. Run the file using python3 command in your terminal Go to localhost:8000 or any other ports that the terminal shows you. Note: Normally you should use a virtual env but I skipped that part. After doing these things it is time to take a deep looking into our code.
- In line 1 we imported a Class called Flask from flask module.
- Using a new variable called app we create a new instantiate from Flask class. name argument is referring the name of the script file. If the script runs directly the value of this variable will be main otherwise the value will be the name of the script.
- Flask class included some methods and commands that are vital for us to make a route for our application, run it on the server and etc.
- In the fourth line, we use the ‘route’ decorator that gets an argument for declaring a route for the next line. If you are not familiar with decorator it is not a good time to learn it but in a very short journey, I can say that a decorator will add additional features to a function that will be created in the next line.
- In the next line we declare a function called index() without any argument and this is going to just return a simple ‘Hello, World’ text.
- At the end of the file we can see an if statement. We aimed to say to python that only if the script is running directly then starts to run in the browser and simply makes it work.
- app.run() is a method for run the script. If we use this method without any argument development server can not show us the errors that may happen during the running application. But when you assign True to debug mode, then with any error the browser shows you the problem in a very human way.
- Learning flask is very important for those who have just started to be a back-end developer. Because flask is too minimal and there is no fantasy or shorthand way for making applications. As result, you need to do more works by yourself and this will give you a better understanding of how a back-end script works.
- Before you begin to learn flask make sure you have good knowledge about python. This will be very useful.
- In the next post, I will guide you through what is decorators but at this moment there is already a lot of information about decorators on the Internet.