Flask is a micro python framework, which is used for the backend in web development. It is very easy to use and set up. The “micro” here means that it aims to keep the core simple but extensible. It is lightweight compared to other frameworks like Django.
Steps for starting the server
Make a folder named flask_server on your machine and then open that into your code editor. Sounds easy right?
Make an environment and activate it. Go to your terminal and type the following.
python3 -m venv venv
The first line here makes a virtual environment, which we need to have for all our dependencies.
The second step activates it.
Add a file named
app.py , which will be our server file and install Flask.
pip3 install Flask
The first step will add the file.
The second step will install Flask into your venv(virtual environment ). Make sure you do it inside the terminal once you activate the venv, in the previous step.
Now, this is how our
app.py should look.
In the first two lines, we import flask and initialize our app.
The last two are for running it in debug mode, and to run it on port 3000.
The main thing here is the
@app.route, in which we specify the path and the type of request. The default is a GET request.
So, a request coming to port 3000/ will go through this route and execute the function below it, which is send_hi. And it returns 'hi'.
Go to your terminal and type,
This should run your flask app at port 3000. You can go and check your browser at localhost:3000.
You should see 'hi' over there.
Similarly, you can have different routes with more complexity. You can perform your logic, like adding to cart, placing an order, changing user, login, and so on.
Thank you so much for reading, I hope you liked it.
If you have any questions or suggestions, please leave a comment below or feel free to reach out.