In this Flask Tutorial we are going to learn about Flask Introduction & Installation, we will learn that how you can install Flask and how you can create your first project in Flask. so Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website. Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins.
Installation
You can use simply pip for installing flask.
1 |
pip install Flask |
After installation, you need to open an IDE, iam using Pycharm IDE, but you can use what IDE you want. create a new Python file at name of app.py, now this is our complete code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from flask import Flask #create the object of Flask app = Flask(__name__) #creating our routes @app.route('/') def Index(): return "<h1>Geekscoders.com, Hello Flask Application</h1>" #run flask app if __name__ == "__main__": app.run(debug=True) |
All Flask Applications must create an application object, The web server passes all
requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). you can create the application object like this.
1 |
app = Flask(__name__) |
The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable
is the correct value.
Also for every Flask Application we need to define routes, you can use app.route decorator for creating routes in Flask, which registers the decorated function as a rout. as we have already done in our above complete code like this.
1 2 3 |
@app.route('/') def Index(): return "<h1>Geekscoders.com, Hello Flask Application</h1>" |
In the above code we have registered the function Index() as the handler for the applications’s root URL. functions like Index() are called view functions. a response returned by a view function can be a simple string with HTML content, but it can also take more complex forms.
At the end we need to run our web application using development server.
1 2 |
if __name__ == "__main__": app.run(debug=True) |
The __name__ == ‘__main__‘ is used here to ensure that the development web server is started only when the script is executed directly, and also you can see that we have made debugging mode True, but when you are going to publish your web application , you need to make false, for development purposes you can keep it true.
Run your code and go to http://localhost:5000/.
Also you can create different routes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from flask import Flask #create the object of Flask app = Flask(__name__) #creating our routes @app.route('/') def Index(): return "<h1>Geekscoders.com, Hello Flask Application</h1>" @app.route('/contact') def Contact(): return "<h1>Geekscoders.com, Contact Page</h1>" @app.route('/about') def About(): return "<h1>Geekscoders.com, About Page</h1>" #run flask app if __name__ == "__main__": app.run(debug=True) |