Building a Python Web Server with Flask

In my previous post you have seen how i have created a simple python program. In this post let’s make a simple python application.

So it all started when i used to check my weight everyday after having lunch in the canteen and thought what exactly should my BMI be to be a healthy human. I started to find the logic of getting the perfect BMI from google.

BMI logic

Without wasting much of time i started to code in my favorite anaconda navigator and Jupyter notebook. With some little tweaks i got some absolute results. Don’t worry if you are not having the navigator installed, go ahead and write your code in VS code.

Our idea here should be creating a simple BMI calculator and run it in our local server. Flask is a lightweight Python framework for web applications that provides the basics for URL routing and page rendering.

https://flask.palletsprojects.com/en/1.0.x/quickstart/#

Setup the VS code as i explained in the Part 1 post.

  1. Create a project environment for the Flask tutorial.In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter command:

The selected environment appears on the left side of the VS Code status bar, and notice the “(venv)” indicator that tells you that you’re using a virtual environment:

As a programmer from data science, you may require to interact Flask APIs with Python Ajax and JSON. This article contains steps by steps for building your APIs in Flask and interacting it with Ajax in with HTML form.

After the setup has been done in your VS code, create two files of extension .py and .html. In the index.html the user will enter the weight and height and the data will be sent to the flask route as Post request and it will display the value of the field in the HTML. The following is the HTML code.

After entering the input field you will call the Ajax Javascript part that will finally display the combination of the values that is weight and height. It will display just after the submit button

Pardon me for my poor FRONT end skills, i have just made a bare bone skeleton aesthetics to make things work.

Don’t worry, i will provide my git repo from where you can download the source code of the BMI calculator.

https://github.com/sanajitghosh/python-flask-app.git

index.html
ajax call to POST the output
  1. Once the index page is ready, start creating the Flask app by importing the following modules required for creating the Flask Function
  2. Next create an object of Flask with any name lets take the app for this example.
  3. Create a URL endpoint for rendering the above HTML file lets say the name of the file is index.html.
  4. Create an endpoint for the form of the HTML page.

5. Add the logic of the code and return the results.

Once the code is ready, hit the terminal and run python -m flask run. Now open the browser and you could see the application running in our local flask web server.

python -m flak run
Application is up and running

In the next post of Part 3,i will show you the easiest way to deploy your flask python app in azure web/app service for production ready consumption.

Cloud, Data & Analytics, DevOps