How to build a Web App for a Machine Learning model using Flask micro framework?

To realize the true benefit of a Machine Learning model it has to be deployed onto a production environment and should start predicting outcomes for a business problem. Most Data Scientists know how to extract data from multiple data sources, combine, clean, and consolidate data, perform feature engineering, extract features, train multiple models, ensemble, validate, and test the models. But what they lack is how to take a trained model onto production.

There are multiple ways to deploy a model in production. However, in this post, we will go through the step-by-step process of creating a basic model and deploy it as a Web App using Flask micro framework - a Python based toolkit. These steps are executed in Windows Operating System. But for Linux, Ubuntu, and other OS, this should work seamlessly by adopting relevant syntax.


  1. Let us build a simple Machine Learning Model using iris dataset that is bundled with sklearn package. This can be done using Jupyter Notebook, PyCharm, PyTorch or any other IDE that you are comfortable with.
    Now that your machine learning model is created and persisted in hard-disk as SVMModel.pckl

  2. In Windows Command prompt, execute the below command to install Flask framework and its associated dependencies/libraries:

    pip install flask gevent requests pillow

  3. Let us create a folder structure as below so that it can be extended to production-like interactive and real-time application later.
    • Root folder flask-blog contains server start-up class
    • Sub-folder models contains pre-trained machine learning models
    • Sub-folder static contains image, CSS, and JavaScript files
    • Sub-folder templates contains static and dynamic html files
  4. Under flask-blog folder, create a file called with below content:

  5. After the above file is created, go to flask-blog folder, open a Command prompt, and run the command python It will execute as below:

  6. After the server is started successfully, open a browser window, and enter URL
       FlaskBrowser1 If you get a message Hi, Welcome to Flask!! in your browser, congratulations, your Flask server is up and running successfully! If you get any error or could not get the server up and running, leave a note under the Comments section of this blog and I will get back to you as early as possible.

  7. Having successfully started the server, let us move on to extend to predict the new observation using previously trained and stored SVM Model.

    Before updating code, go to the Command prompt and stop Flask server using Ctrl-C. Update code as below, or you may simply copy & paste the contents to your code.

  8. Go to the Command prompt again and start the server using python

  9. Once the server is started, open a browser window and enter the URL:    FlaskBrowser1

Voila! The predicted class of Iris will appear on the screen as above. Play around by changing the values of features in the URL query string.

Now that you understand how a machine learning model can be created, persisted onto a disk, loaded from disk, can extract features from a browser request, and can use the model to predict the class using those features.

This application can be extended with fancy UI containing form element, dropdown boxes, submit button, etc..