Why Flask in Micro Framework?
Flaks is termed as a microframework because it does not require particular libraries or external tools. Following are not available in Flask.
- Database abstraction layer
- form validation
- Other components where pre-existing third-party libraries provide common functions.
Applications Written in Flask Framework
Components of Framework
The microframework Flask is part of the Pallets Projects (formerly Pocoo), and based on several others of them.
- Werkzeug (German for “tool”) is a utility library for the Python programming language, in other words a toolkit for Web Server Gateway Interface (WSGI) applications, and is licensed under a BSD License. Werkzeug can realize software objects for request, response, and utility functions.
- Jinja, also written by Ronacher, is a template engine for the Python programming language and is licensed under a BSD License.
- MarkupSafe is a string handling library for the Python programming language, licensed under a BSD license.
- ItsDangerous is a safe data serialization library for the Python programming language, licensed under a BSD license. It is used to store the session of a Flask application in a cookie without allowing users to tamper with the session contents.
How to Install Flask?
You can install flask-python directly from PyCharm IDE or command line using following command.
pip install Flask
How to Write App in Flask?
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<h1>Hello, World!</h1>" @app.route("/user") def hello_user(): return "<h1>Hello User!</h1>" if __name__ == "__main__": app.run()
By executing above code, your output will be:
* Serving Flask app "__main__" (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: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Your basic and first web application is ready and can be accessed using URL (127.0.0.1:5000), where 5000 is the port on which this web app is running. Type this URL in browser, you will see “Hello World” in h1 HTML tag. Now lets understand the basic flask code.
This decorator is used to tell application, on which URL function should be called. “/” shows the base URL, it means that when someone type 127.0.0.1:5000, the function will be called. When someone type 127.0.0.1:5000/user, then hello_user() function will be called. So it is basically routing in Flask.
If 127.0.0.1:5000 not showing application then try localhost:5000
How to Create Externally Visible Server in Flask?
In above program the service is running on your local machine and people from remote locations cannot access your web application. Now how to create server that is externally visible i-e people can access from anywhere using your IP address.
The first step is you have to disable the debugger by passing argument as in app.run() as shown:
Now execute your flask program using following cmd command
flask run --host=0.0.0.0
For above command to work, your application file name must be app.py
Alternatively you can add another argument host=”0.0.0.0″ to same function to make publicly available server.
If you have the debugger disabled or trust the users on your network, you can make the server publicly available
This argument tells your operating system to listen on all public IPs.
How to execute Flask app on different port?
By default flask application or service is executing on port 5000, but you can change this port by passing an argument port=PORT NUMBER in app.run() function. In given code, service will be available on port 9000.
- Development server and debugger
- Integrated support for unit testing
- RESTful request dispatching
- Uses Jinja templating
- Support for secure cookies (client side sessions)
- 100% WSGI 1.0 compliant
- Extensive documentation
- Google App Engine compatibility
- Extensions available to enhance features desired