Web Application Framework or simply Web Framework represents a collection of libraries and modules that enables a web application developer to write applications without having to bother about low-level details such as protocols, thread management etc.
What is Flask?
Flask is a web application framework written in Python. It is developed by Armin Ronacher, who leads an international group of Python enthusiasts named Pocco. Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
WSGI
Web Server Gateway Interface (WSGI) has been adopted as a standard for Python web application development. WSGI is a specification for a universal interface between the web server and the web applications.
Werkzeug
It is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it. The Flask framework uses Werkzeug as one of its bases.
Jinga2
Jinga2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.
Flask is often referred to as a micro framework. It aims to keep the core of an application simple yet extensible. Flask does not have built-in abstraction layer for database handling, nor does it have form a validation support. Instead, Flask supports the extensions to add such functionality to the application. Some of the popular Flask extensions are discussed later in the tutorial.
1. ติดตั้ง virtualenv
pip install virtualenv
virtualenv (virtual environment) ช่วยในการแยก env ในการทำงาน กรณีมีหลายโปรเจคออกจาก เนื่องจากแต่ละโประจค มีการใช้ python version, library ,library version แตกต่างกันไป ref1 ref2
2. สร้าง project (directory) และเริ่มใช้งาน env
mkdir newproj cd newproj virtualenv venv
venv คือ ชื่อเพื่อระบุ env ที่เราสร้าง
-p python3 คือ การระบุ python version ที่ต้องการใช้
เช่น
virtualenv -p python34 py34
เริ่มใช้งาน virtualenv
venv/bin/activate
3. ติดตั้ง Flask
pip install Flaskoutput จะได้ประมาณนี้
Collecting Flask Downloading Flask-0.10.1.tar.gz (544kB) 100% |################################| 544kB 410kB/s Collecting Werkzeug>=0.7 (from Flask) Downloading Werkzeug-0.11.4-py2.py3-none-any.whl (305kB) 100% |################################| 307kB 531kB/s Collecting Jinja2>=2.4 (from Flask) Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB) 100% |################################| 266kB 935kB/s Collecting itsdangerous>=0.21 (from Flask) Downloading itsdangerous-0.24.tar.gz (46kB) 100% |################################| 49kB 1.6MB/s Collecting MarkupSafe (from Jinja2>=2.4->Flask) Downloading MarkupSafe-0.23.tar.gz Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask Running setup.py install for MarkupSafe Running setup.py install for itsdangerous Running setup.py install for Flask Successfully installed Flask-0.10.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.4 itsdangerous-0.24
** ในการ Dev มีการใช้งาน package หลายตัว ซึ่งเวลาจะไป deploy ต้องสร้าง รายชื่อ package ที่ต้องการใช้งาน โดย เราสามารถใช้ คำสั่งนี้ เพื่อ รวม package list เพื่อ เตรียมไป deploy ได้
pip freeze > requirements.txtตัวอย่าง requirements.txt
click==6.7 Flask==0.12.2 itsdangerous==0.24 Jinja2==2.9.6 MarkupSafe==1.0 Werkzeug==0.12.2
และ เมื่ออยุ่บนระบบที่เราต้องการ deploy ให้ใช้ command
pip install -r requirements.txt
ไม่มีความคิดเห็น:
แสดงความคิดเห็น