THE RISE OF FLASK
About the article
- Flask Introduction
- requirements and setup
- Flask template
- Explanation
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.
Flask is intended for web applications and is “micro” because it omits native tools and libraries for a “install what you need” approach
PYTHON
Python is one of the most preferred languages to build backend applications. Even when facing good old PHP and full-stack JavaScript, Python has its place because of the ease of implementation, and a supportive community that keeps beginners hooked.
Requirements and setup
- To Install python jump here
- Visual studio or PyCharm would be great (IDE)
- Installing flask using
pip install flask
Flask Template
from flask import Flask # import flask
app = Flask(__name__) # create an app instance
@app.route("/") # at the end point /
def hello(): # call method hello
return "Hello World!" # which returns "hello world"
if __name__ == "__main__": # on running python app.py
app.run(port=8003, debug=True) # run the flask app
To Start the Application
Run the application by running the `app.py` file. By default, the flask runs a local server at port 5000.
python app.py
Explanation
When we create our Flask object, we pass __name__
as a single argument. This sets the current file as the app’s anchor, letting Flask know how to import and build when necessary.
To create a route, we use our Flask object with the method .route()
as a decorator. The single forward slash is the actual path we’re defining for the URL. If we had designate the route as ("/flask")
, then we would need to enter localhost:5000/flask
to access the subsequent function.
Speaking of, our function home()
will be executed when the route is accessed. The function can be more robust, but the important takeaway here is that we return a value that is then printed to the screen.
Debug Mode
This is because we are running the server in a production model. For development purposes, we use something called as debug
mode. When debug=True
is set the server restarts as we add new code to our Flask Application. In order to set the debug mode do the following
- Modify the line
app.run()
toapp.run(debug=True).
- Stop the running server and restart it again.
You will see “debugger is active” which means that the debug mode has taken effect. Now you can go on and edit your file as much as you want, and the server will be restarted.