This tutorial will explain the step by step procedure on building a basic Flask application. Flask can be defined as a lightweight WSGI web application framework. It's quick and easy to get started and is designed that way along with the ability to scale up to complex applications.
Let's start with building a simple basic Flask application first. Later we will work on dockerizing this application using the containerization concept using Docker and Docker Hub.
Let us start this application in a separate virtual environment first so that the dependencies are good only for this application and this won't disturb your other applications dependencies and versions.
Assuming that I will place this application on my desktop folder, I will move to the desktop directory and then create a folder for this application.
Now move this directory,
Install virtualenv package to create virtual environments,
pip install virtualenv
(This installs virtual environments package)
then run the below command to create a virtual environment for this application.
(here “flask_env” is the name of the virtual environment, so feel free to put your own desired name)
Type the below to confirm that the virtualenv is created
(you should see the flask_env folder there which confirms that the virtualenv is created)
Once you created the virtual environment, you can activate it by using,
Once it's activated you should see the environment name (flask_env) on your screen.
To deactivate the env, enter command “deactivate” in the command line and this should remove the (flask_env) from your screen which means that it is deactivated.
If you want to see what packages are installed on this virtual environment, then type
Time to install flask now. Install by typing
pip install flask
This should install flask for you and you can get the version of flask and python by typing
Flask -- version
Once Flask is installed, check the list of packages and versions in your virtual environment by typing
Now lets create a file called “flaskapp.py” in the myflaskapp directory and then type this basic code to test run the application
from flask import Flask # importing flask
flaskapp = Flask(__name__) # creating flaskapp instance
@flaskapp.route("/") # defining end point /
def hello_world(): # calling method hello_world
return "Hello World! Welcome to my Flask app. Let's dockerize!" # returns the content in " "
if __name__ == "__main__": # for running python flaskapp.py
flaskapp.run(host='0.0.0.0',port=5000) # run the flask app / you can also specify desired port number here
Make this file executable by tying the below from the myflaskapp directory
sudo chmod +x flaskapp.py
Now it’s time to run our flask application. Enter this in terminal/command line
You should see the app running on your localhost on port 5000. Port 5000 is the default port number of Flask applications.
Go to your browser and type to open your application
That's it! It's pretty simple and straight forward. The Flask application is built. You can add HTML , CSS and other web development technologies to make your Flask application better and there is no end to this. This is just a starting step to build application.
Here’s the myflaskapp repository on Github
Dockerzing the Flask application will be posted in the next article. If you are interested in dockerizing this application which we build follow the trailing post. You can follow DevOpsPal social media for latest updates.
Recent updates on DevOpsPal
Follow us of social for latest updates.
Links to get started.