In this series, we will learn flask from scratch and we will create a basic flask application. In this article, we will learn how to create a basic flask application.
Let’s get started with the basics of flask.
What is Flask?
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
How to install Flask?
You can install flask using the following command:
pip install flask
Setting up the environment
To set up the environment, you have to create a virtual environment. You can create a virtual environment using the following command. In this tutorial, we will use Python=3.9.13
. If you don’t have python installed on your system, then you can download it from here.
python -m venv venv
source venv/bin/activate
How to create a basic flask application?
To create a basic flask application, you have to create a file named app.py
and add the following code in it:
from flask import Flask, jsonify
# Create a Flask app
app = Flask( __name__ )
# Create a route for home page
@app.route('/')
def home():
return jsonify({'message': 'codeperfectplus.com'})
# Run the app
if __name__ == ' __main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Command to run the flask application
To run the flask application, you have to run the following command.
python app.py
* 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: on
* Restarting with watchdog (windowsapi)
* Debugger is active!
* Debugger PIN: 614-383-374
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
It will run the flask application on port 5000
. You can open the browser and type http://localhost:5000/
and it will show you the following output. It can also run on other ports. You can change the port number in the app.py
file.
You can access the the url http://localhost:5000/
from any device connected to the same network. You have to replace localhost
with the ip address of your system. You can find the ip address of your system using the following command:
{
"message": "codeperfectplus.com"
}
Use cases of Flask
Flask is widely used in web development. It is used to create REST APIs, web applications, and much more. It is a very powerful framework and it is used by many companies such as Netflix, Uber, and much more.
Top comments (0)