Skip to content

Develop a web application in Python in less than 10 lines of code.

It is fairly easy to develop a web application in Python by using Flask. Flask is a python module that provides a web framework and utilizes Web Server Gateway Interface (WSGI) specification between web servers and web applications. Flask is also called microframework as it is designed to keep the core of the application simple and scalable using the extensions.

First install the Flask Module

$ pip install flask

If not working on GitHub Codespaces then use following code. (This post will be assuming to be run on Codespaces and won’t provide further details on running the code in local)

$ python3 -m pip install flask

Import Flask and then instantiate the Flash object in this case “app”. It is interesting piece of code snippet Flask with bracket and two underscore in the front and back of name. Python provides these methods to use as the operator overloading depending on the user. Python provides this convention to differentiate between the user-defined function with the module’s function.

from flask import Flask
app = Flask(__name__)

Next define a route, defining a route is as simple as defining a function in python. Then, turn the function into routes by adding Python decorators using @. Decorators are a very powerful and useful tool in Python. Decorators allow programmers to modify the behaviour of a function or class. The best explanation of Python Decorators can be found here The best explanation of Python decorators I’ve ever seen. (An archived answer from StackOverflow.) (github.com)

@app.route("/")
def hello():
  return "Hello World!!"
app.run(debug=True)

Now when you run the application the web application is launched in the browser.

$ python main.py
 * Serving Flask app 'main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 772-296-512
127.0.0.1 - - [22/Jan/2024 11:01:43] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [22/Jan/2024 11:01:43] "GET /favicon.ico HTTP/1.1" 404 -
python web application
Leave a Reply

Your email address will not be published. Required fields are marked *