According to ancient programming tradition, whenever you feel like you are ready to start building out new projects in programming, you start with a hello world application.
That's why in this tutorial we are going to be writing our very first hello world program in flask. The purpose of this program is to make you a little more familiar with flask and how it works.
Let's get started..
What is flask
Flask is a backend web framework written in python. It was developed by Armin Ronacher and it's used to develop web applications and web servers quickly and easily.
Why is Flask called a Microframework
Flask is also referred to as a lightweight python microframework because it doesn't come with a lot of features out of the box.
For example, It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. Flask is what is also referred to as an unopinionated framework.
What does it mean to be unopinionated?
Well it means that flask doesn't decide for you what your application should look like. It gives you (the developer) the freedom to choose whatever standard or convention you want when you want to build out your application.
Flask vs Django
Both flask and django are web frameworks written in python; and while there are similiarities between them, there are more differences. Here are a few areas where both frameworks differ from each other:
Flask | Django |
---|---|
Flask is a microframework and as such doesn't come with external libraries or dependencies. It is unopinionated and doesn't force you into building your application in a certain way | Django is a fullstack framework which is based on the MVC pattern and it is opinionated |
Flask doesn't come with an admin interface | Django comes with an in-built admin interface |
Django is more suitable for multi-page applications | Flask is most suitable for single-page applications |
Some Companies that use Flask
- MIT
- Netflix
- Airbnb
- Zillow
- Lyft
- MailGui
- Mozilla
Some Companies that use Django
- National Geographic
- Mozilla
- Coursera
- Zapier
- Spotify
- Udemy
Flask Setup
In order to start making use of flask, we need to set things up first. The first thing we need to do is setup a virtual environment
Setting up Virtual Environment
So before we set up our virtual environment, let's talk a little bit about why a virtual environment is useful.
The reason why a virtual environment is useful is because of what it does. A virtual environment gives us the ability to isolate the packages we use for our project so that in the future when we want to use a different version of those packages, we don't run into problems. For an in-depth explanation on virtual environments, you can check out this tutorial on real python
In order to create a virtual environment, open up your terminal and run the following command:
On Windows:
> mkdir myproject
> cd myproject
> py -3 -m venv venv
On MacOS/Linux:
$ mkdir myproject
$ cd myproject
$ python3 -m venv venv
By running the above command, what you have just done, is successfully create a project directory with a virtual environment in it.
Activating the Virtual Environment
Run the following command to activate the virtual environment you've just created:
> venv\Scripts\activate
Installing Flask
Now, we are done creating a virtual environment, so we can now install flask inside of this virtual environment by using the following command:
$ pip install Flask
Hello World Code in Flask
Importing Flask
So the first thing we do is import our Flask class from the flask library we just installed
from flask import Flask
Creating an Instance of our Flask App
Then the next thing we do is create an instance of our Flask app which will be our WSGI application
app = Flask(__name__)
We do this by passing the ""__name__"" variable as a parameter to the Flask constructor. The ""__name__"" variable here refers to our current python file.
Creating the Route Decorator and it's corresponding Function
@app.route("/")
def hello_world():
return "Hello, World!"
So here, the route decorator tells our app which route will trigger the hello_world function. In our case, it is the '/' route - the route of the home page of a website.
So what this means is that whenever a user visits the home page of our website, our web application will return a "Hello, World!" string.
Run App
if __name__ == '__main__':
app.run(debug=True)
Now we can add the final part of this code to enable us run our app. The run method from the code above can either have the debug value set to 'True' or 'False'. When it is set to 'True', it enables debug mode and the server will automatically reload whenever you make changes to your code and will also show an interactive debugger in the browser if an error occurs during a request.
Note: The debug mode should not be used in production.
if __name__ == '__main__':
if __name__ == '__main__':
This line of code checks to ensure that our python file is being run directly. Whenever we run our python file directly the ""__name__"" variable will have the value of ""__main__"". So our web server is only going to start when we run our python script directly and not through any other means.
Final Code
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
So now you can start your web server by running the python file.
To see your result, you can visit this address on your computer http://127.0.0.1:5000
Connect with me on:
Top comments (0)