Build Your First Web App With Flask
Introduction
A web framework is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Now, there are various forms of web frameworks available. Among those web frameworks, Flask is mostly preferred by data science enthusiasts to deploy machine learning models, because it gives freedom to implement our own rules and does not require much setup. Flask is classified as a microframework because it does not require particular tools or libraries. Flask was created by Armin Ronacher of Pocoo, an international group of Python enthusiasts formed in 2004.
As Data Science Enthusiasts, Machine Learning Engineers, or data science practitioners, presenting our solution to the audience or to a client is very important. Building and Training a model using various algorithms on a dataset is one part of a data science application. But to use the model in order to predict the new data, we have to deploy it over the internet so that the outside world can use it. Flask is a great choice for this as it is extremely lightweight, needing only a little code to convert a Python function into a web endpoint. Also, Flask is best for beginners.
In this article, I will give a brief guide for you, to start your Flask journey with.
Hello, world! app with Flask
Installing Flask
In this step, I assume you are using windows.:) If you are using another OS it may be a little bit different than this.
First, you need to install Flask (you can do so with pip, the Python package manager). Then, open the terminal and type the command shown below:
pip install flask
This installs the Flask module on your computer. But, using a virtual environment for a python-based project is considered as a best programming practice. For that, use the following command after navigating inside the folder where you want to create your virtual environment.
python -m venv testenv
Once you’ve created a virtual environment, you may activate it.
testenv\Scripts\activate.bat
Now you can install the latest version of a package by specifying a package’s name. Let’s install Flask.
pip install flask
We are going to create a “Hello world” app for the web. If you load the website URL, it will show you the message “Hello world”. Now let’s build our app!
Flask Application
The first thing to do is to import the Flask module.
from flask import Flask
Next, we create an object of the flask class. We send the __name__ argument to the default constructor, this will help Flask look for templates and static files.
app = Flask(__name__)
Then, we can use the route decorator to define the route. The python function below to the decorator should be navigated into that route. We must define the function and the action to be performed.
@app.route('/')
def hello():
return 'Hello World from Python Flask!'@app.route('/Home')
def home():
return 'You are amazing!'
You can create any route you want. But make sure that both the route name and the function name are unique.
Now you can run your simple web app using the app.run(). It will initiate the server to run on the local development server.
if __name__ == '__main__':
app.run()
After running the code the output would be as follows.
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now, open your web browser and type http://127.0.0.1:5000/. This will show you the message ‘Hello World from Python Flask!’. And if you type http://127.0.0.1:5000/Home, it will show you the message ‘You are amazing!’.
Congratulations! You have created your first web application using Flask.
Thanks for reading. If you enjoyed this article, please hit the clap icon.:)