How to Create a simple web application in python using flask

Tech Elucidator
3 min readOct 24, 2020

--

A web application framework is written in python

pic source here

In this blog we are not going to dive deep into the flask, we are just gonna see what is it then we will be setting up the environment and we are gonna have a simple hands-on experience on the flask.

Flask is a popular Python web framework. It is a third-party Python library used for developing web applications in python. It is quite simple and easier to start though you are a beginner.

Step — 1: Installing the components from the ubuntu repository

Let’s update the local package index and install the package that will allow us to build our python environment.

sudo apt update sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

Step- 2: Creating a python virtual environment:

Let’s set up a virtual environment in order to isolate our Flask application from the other Python files on the system.

sudo apt install python3-venv

Now, create and move into a directory where you will have the flask project

before executing below commands replace <directory_name>, <virtual_environment_name> and <filename> with the names that you wish.

Now, create a python virtual environment to store the project’s python requirement

python3 -m venv <virutal_environment_name>

We need to activate the virtual environment before installing the flask

source <virtual_environment_name>/bin/activate

Step — 3: Setting up a Flask application:

Let’s install ‘ wheel’ with the local instance of pip so that our package will be installed even if they are missing wheel archives.

pip install wheel

Now, let’s install Flask and Gunicorn

pip install gunicorn flask

Create a simple file with the extension ‘.py’, for example <filename>.py with the below code in it.

from flask import Flaskif __name__ == "__main__":
app = Flask(__name__) @app.route("/")
app.run(host='0.0.0.0',port=7070)
def hello():
return "<h1>Simple Flask Application</h1>"

We need to allow access through port 7070(which I have specified) through the UFW firewall

sudo ufw allow 7070

Now test the application by

you can see the below output

* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:7070/ (Press CTRL+C to quit)

visit your server by the URL http://localhost:7070 if you are accessing in the same machine else access it by http://<ipaddress>:7070

okay, guys, that’s it we have created a simple web application using flask😇😊

Thank you so much for spending your valuable time on my blog. Please support me by doing claps at the rate of 1–50 by the way you feel it is worth it. If you have any doubts regarding this blog or suggestions to improve it please do comment below and share it with those who want to know about a web application framework in python.

I will get you things as simple so my other blogs about

  1. PWA — deploy app at any environment is here
  2. Chrome extension is here

Do read, enjoy and provide support💪💪🙏

Have a good day :)

--

--