Getting started with Web development using Flask

Flask is a web application framework written in Python. It was developed by Armin Ronacher.
Now, what is a web application framework? It basically is a collection of all the libraries and modules which help us create our application without having to bother about things such as protocols etc.

What all do you need to get started?

Firstly, you need to have Python installed in your system. It’d be better if you have a Python 3 version (Python 3.4 onwards) or Python 2.7 installed. Don’t forget to set your environment variable for Python (this helps you use python even from your command prompt).
{ Visit www.python.org to find the installation file which satisfies the specification of your system and your needs.}

Also, it’d be a little less messy and easier if you had some text editor installed such as Visual Studio Code.

Now all we need to do is install flask, for which you need to open your command prompt and type the following,

Alternatively, you could also create a virtual environment and then install flask there but this way you are installing it system-wide. Both ways work.

Let’s Code!

When getting started with anything new in the world of programming, the “Hello World” program is a must (it actually helps understand the very basics really well and to also check if the installation is working or not).

So type the following code in your editor and save it as Hello.py.

So now how does this code work? What does everything here mean?

The first line says “from flask import Flask”. Here, the flask (the one with the ‘f’) refers to the framework that we just installed and the Flask (the one with the ‘F’) refers to a Python class datatype (basically, it is used to create instances of web application(s)). So, we are importing Flask from the framework flask because that’s what we need to use.

Now, the next line is “app = Flask(__name__)” {yes, it’s two underscores on each side}. Here, we are creating an instance of the Flask class for our web app. We will be referring to our app as “app” throughout the program as we are working on that particular instance. “__name__” is a special variable which gets the value of the string “__main__” (line 8) when the script is executed.

Next, in the lines 4–5 we are defining a function which returns the string “Hello World” when the home URL (‘/’) is being accessed. This home ‘/’ URL represents that your web app is currently being hosted on the local host, which is actually “localhost:5000” (when you actually run this program you will get a unique URL). When user navigates to this URL, the “hello_world” function will run and it will print the output “Hello World” on the webpage. If the input to the route() function was something else like ‘/info/’, the output of the function hello_world will be displayed only when the user is on “localhost:5000/info/” URL. Also, we can define multiple functions and also have different outputs when we visit different URLs on the same host. Also, you need to keep in mind that when you write “@app.route(‘/’)”, it actually shows that the routing is happening for one particular instance (which we named “app”).

Now, lets get back to the lines 8 and 9. Python assigns the name “__main__” to the script which is being executed. So when and if the condition “__name__ == ‘__main__’” is true then the “app.run()” function will get executed otherwise it won’t be executed.

Also, we write “debug=True” inside the “app.run()” function. This means that the debugging mode is on and so if at all there are any errors, they will be printed on the web page instead of the required output. This makes debugging easy for us but there is a problem with this and so when you are in production environment or don’t want any security issues, you should change it to “False”.

That’s the whole basic flask program and now you need to run the hello.py file and copy the URL mentioned there to your browser and you are set!

This is the most basic flask program, which isn’t of much use to us but with the help of HTML (Javascript and CSS too) you can make an appealing website. For that, you could return HTML files instead of strings in the above code (in that case, we will have to use render_template). The rest can be covered in another post if requested by you.

--

--