Build the first Flask Python web app framework

When developing a web app in Python, chances are you’re leveraging a framework. There are a number of frameworks for Python, from which we will now learn Flask. This article will be your complete definitive guide for developing fully functional websites with the Flask web framework.

Chirag Samal
Analytics Vidhya
4 min readNov 6, 2019

--

Breakdown of this Article:

1) Install flask

2) Create a Machine Learning Model

3) Saving Machine Learning Model

4) Creating an API using Flask

5) Create a front-end application

6) Hosting on the personal server

Environment setup

REST API using Flask

Flask is a web framework which provides us with tools, libraries and technologies that allow us to build a web application. This web application can be any web pages or a blog or go as large as a commercial website.

In Anaconda Distribution install Flask:

pip install flask

Explanation of Data Flow using Flask

Data Flow using Flask

Steps to be taken:

1) Train the Machine Learning Model

2) Save the trained machine learning model object as a pickle file (serialization)

3) Build a flask environment that will have an API endpoint which would encapsulate our trained machine learning model and enable it to receive inputs (features) through GET requests over HTTP/HTTPS and then return the output after de-serializing the earlier serialized model

4) Upload the flask script along with the trained Machine Learning Model

5) Make requests to the hosted flask script through a website or any other application capable of sending HTTP/HTTPS requests

Machine Learning Model

Train our Machine Learning Model

Create a Python file model.py

Machine Learning Model

Here, after running the above code, a file called “model.pkl” is created using dump which is the trained machine learning model that can be transferred anywhere and used after de-serialization as given using load.

Flask setup

Let’s first set up the flask server on the local host and later deploy it on python.

The following script starts the flask server on localhost and default port (5000) making the URL: http://127.0.0.1:5000/ (http://127.0.0.1:5000/)

app.py

In-App Route app.route decorator is used for specifying the flask app route over the web.

  1. “/” simply means the home that is “http://127.0.0.1:5000/” (http://127.0.0.1:5000/”)
  2. 2) “/predict/” means http://127.0.0.1:5000/predict/ (http://127.0.0.1:5000/predict/)

Routing

After initializing the app.py file, we have to tell the Flask what we want to do after the web page loads. The python code @app.route(“/”, methods = [“GET”,” POST”]) tells the Flask what to do when we load the home page of the website. In methods: GET method is the type of request our web browser will send to the website when it accesses the URL of the web page. Don’t worry about the POST method as it doesn’t have much relevance for us in this deployment process.

Setting up the HTML file

index.html

Save model.py, app.py and index.html

Run app.py in anaconda prompt

Copy the URL http://127.0.0.1:5000/ (http://127.0.0.1:5000/) and paste it into your browser

After a few secs, this would be shown in the browser.

Machine Learning API

Input Data into different Categories

Then Press Predict button

You can see that we Predicted our profit and notice that the URL of the page changed to http://127.0.0.1:5000/predict (http://127.0.0.1:5000/predict).

This is my first article hope you like it and the Github link for all the files is in this link: https://github.com/chiragsamal/flask_tutorial

--

--

Chirag Samal
Analytics Vidhya

Computer Vision Engineer @ Zeiss | Former Intern @ Stanford University, IISc | Kaggle Master | IIIT-NR