Hello guys!
After SVATTT CTF competition with consolation prize, I decide to try hard and spend more time learning, seriously.
Today I move on with Flask - an important and interesting framework in Python.
1. Definition
+ Distinguish Module, Library and Framwork
- Module: abstract interface with explicit exports and imports, implementation and interface are separate, there may be multiple implementations and the implementation is hidden.
- Library: collection of related functionality
- Framework: something abstracts some low-level computer science concepts, you so can focus on the high level details
- Microframework: A microframework is a term used to refer to minimalistic web application frameworks. It is contrasted with full-stack frameworks.
“Micro” does not mean that your whole web application has to fit into a single Python file (although it certainly can), nor does it mean that Flask is lacking in functionality. The “micro” in microframework means Flask aims to keep the core simple but extensible
+ What is Web framework
A Web Application Framework or a simply a Web Framework represents a collection of libraries and modules that enable web application developers to write applications without worrying about low-level details such as protocol, thread management, and so on.
+ What is Flask
Flask is a web application framework written in Python. Flask is based on the Werkzeg WSGI toolkit and the Jinja2 template engine.
- jinja2 :a popular template engine for Python.A web template system combines a template with a specific data source to render a dynamic web page.
Hello world in Flask
2. Installation
pip install Flask
pip install virtualenv
3. Run a sample
- Create folder which contains file (helloworld.py)
- Move to that folder.
- Create a virtual environment which will allow you to keep all the things you need for flask app into one project.
py -m venv <name>
Environment "Hello" has been created |
- Activate it
<name>\Scripts\activate
- Choose what file will be started
set FLASK_APP=<file_name>.py
- Run it
flask run
- Here is the result:
It's hard to imagine a more critical feature of web frameworks than routing: the humble act of mapping URLs to actions, such as serving pages or data.
- Reserve a URL path, such as /
or /home
, associate this with a page template, and serve said template to the user, perhaps with added business logic. That perspective works fine for small-scale applications, but meaningful applications (or APIs) aren't static one-to-one mappings. To build products larger than ourselves, we need to arm them with the ability to grow in ways we can't foresee, which means defining dynamic routing opportunities that can potentially grow endlessly.
Here is the result :
redirect() is the last of our big three route resolutions.
Redirect accepts a string, which will be the path to redirect the user to. This can be a relative path, absolute path, or even an external URL.its best practice to refer to routes by their names and not by their URL patterns.
url_for() : this built-in function takes the name of a view function as input, and will output the URL route of the provided view.
Result:
+ HTTP METHODS
By default, a route only answer to GET method. You can use the methods argument of the route() decorator to handle different HTTP methods.
With GET:
With POST:
+ The REQUEST object
+ ERROR HANDLING
What happens when a user of our app experiences a fatal error? Flask provides us a decorator called errorhandler() which serves as a catch-all route for a given HTTP error.
+ STATIC FILES
Dynamic web applications also need static files. That’s usually where the CSS and JavaScript files are coming from. Ideally your web server is configured to serve them for you, but during development Flask can do that as well. Just create a folder called static in your package or next to your module and it will be available at /static on the application.
See the demo below
Link [#3] Flask Framework - Python [#3]
Happy Learning!
Không có nhận xét nào:
Đăng nhận xét