SlideShare a Scribd company logo
1 of 15
Download to read offline
Bottle Web Framwork
For Python
Đinh Nhật An
http://pythonvietnam.info
atuladn@gmail.com
Bottle
• Giới thiệu
• WSGI (Web Server Gateway Interface) ?
• Bottle framework
• Install
• Request Routing
• Generating content
• Request data
• Template
• Demo
• Thảo luận
Giới thiệu
• Bottle là một framework phục vụ cho việc phát triển web
application dành cho ngôn ngữ python
• Đặc điểm chính của bottle:
– Nhẹ
– Nhanh
• Bottle là một framework dựa trên WSGI tương tự như các
framework khác flask,bobo, web.py… và bản thân nó đã bao
gồm một mini WSGIServer phục vụ cho việc phát triển
được nhanh chóng và thuận lợi.
WSGI (Web Server Gateway Interface) ?
• WSGI là 1 chuẩn định nghĩa việc tương tác giữa web
server và web application
Bottle framework
• Install
• Request Routing
• Generating content
• Request data
• Template
• Demo
Install
• Để cài bottle chúng ta có thể làm các cách sau:
– wget http://bottlepy.org/bottle.py download đến thư
mục chứa code của bạn và import
– Sử dụng pip hoặc easy_install hoặc các trình quản lý
package tương ứng trên OS như apt-get,yum,
Request Routing
• Hiện tại bottle hỗ trợ single route hoặc dynamic route dựa
trên thông tin url gởi lên nó sẽ route đến đúng function
cần xử lý:
• Single route
– @route('/hello') def hello(): return "Hello World!"
• Dynamic route:
– @route('/')
– @route('/hello/<name>')
– def greet(name='Stranger'):
– return template('Hello {{name}}, how are you?',
name=name)
Generating content
• Để tạo ra content và trả về cho client bottle hiện hỗ trợ
các loại dữ liệu sau:
– Dictionaries
– Empty Strings, False, None or other non-true values
– Unicode strings
– Byte strings
– Instances of HTTPError or HTTPResponse
– File objects
Generating content
• @app.route('/hello')
def hello():
return "Hello World!"
• @route('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root='/path/to/static/files')
• @route('/restricted')
def restricted():
abort(401, "Sorry, access denied.")
Request data
• Hiện bottle hỗ trợ xử lý các loại data như cookies,http header,form,
query variables… thông qua đối tượng request.
Ví dụ:
from bottle import route, request, response
@route('/counter')
def counter():
count = int( request.cookies.get('counter', '0') )
response.set_cookie('counter', str(count))
return 'You visited this page %d times' % count
@route('/is_ajax')
def is_ajax():
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return 'This is an AJAX request'
else:
return 'This is a normal request'
Template
• Để quá trình phát triển được nhanh bottle hỗ trợ Simpletemplate
engine cho việc render trang,tất cả việc bạn cần là gọi tên template
và truyền các biến cần thiết,mặc định nó sẽ kiếm template trong thư
mục ./views/ ngang cấp với application root folder:
• Ví dụ:
@route('/hello')
@route('/hello/<name>')
def hello(name='World'):
return template('hello_template', name=name)
Demo
• Hiện code demo mình tham khảo từ đây :
– http://www.tutorialsavvy.com/2013/04/bottle-web-framwork-for-
python.html
Demo
Thank You
Đinh Nhật An
http://pythonvietnam.info
atuladn@gmail.com

More Related Content

Viewers also liked

Slide Python Bai 2 pythonvietnam.info
Slide Python Bai 2   pythonvietnam.infoSlide Python Bai 2   pythonvietnam.info
Slide Python Bai 2 pythonvietnam.infoKhánh Nguyễn
 
Python Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterationsPython Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterationsKhánh Nguyễn
 
Lập trình Python GUI vs PySide
Lập trình Python GUI vs PySideLập trình Python GUI vs PySide
Lập trình Python GUI vs PySideChien Dang
 
Git Using - pythonvietnam.info
Git Using - pythonvietnam.infoGit Using - pythonvietnam.info
Git Using - pythonvietnam.infoKhánh Nguyễn
 
Python Beginner Class day-02-strings
Python Beginner Class day-02-stringsPython Beginner Class day-02-strings
Python Beginner Class day-02-stringsKhánh Nguyễn
 
Python Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-databasePython Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-databaseKhánh Nguyễn
 

Viewers also liked (6)

Slide Python Bai 2 pythonvietnam.info
Slide Python Bai 2   pythonvietnam.infoSlide Python Bai 2   pythonvietnam.info
Slide Python Bai 2 pythonvietnam.info
 
Python Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterationsPython Beginner Class day-04-05-06-iterations
Python Beginner Class day-04-05-06-iterations
 
Lập trình Python GUI vs PySide
Lập trình Python GUI vs PySideLập trình Python GUI vs PySide
Lập trình Python GUI vs PySide
 
Git Using - pythonvietnam.info
Git Using - pythonvietnam.infoGit Using - pythonvietnam.info
Git Using - pythonvietnam.info
 
Python Beginner Class day-02-strings
Python Beginner Class day-02-stringsPython Beginner Class day-02-strings
Python Beginner Class day-02-strings
 
Python Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-databasePython Beginner Class day-11-12-13-database
Python Beginner Class day-11-12-13-database
 

Similar to Bottle web framwork for python

Slide 01-Web Application Overview (1).pdf
Slide 01-Web Application Overview (1).pdfSlide 01-Web Application Overview (1).pdf
Slide 01-Web Application Overview (1).pdfHuyVnh4
 
Thiết kế giao diện với Liquid
Thiết kế giao diện với LiquidThiết kế giao diện với Liquid
Thiết kế giao diện với LiquidTien Nguyen
 
PHP.pdf
PHP.pdfPHP.pdf
PHP.pdfTinPh6
 
Chuong 1 tong quan về lập trình web động
Chuong 1 tong quan về lập trình web độngChuong 1 tong quan về lập trình web động
Chuong 1 tong quan về lập trình web độngTHPT Ngô Quyền
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Ham Chơi
 
Bảo mật ứng dụng ASP.NET
Bảo mật ứng dụng ASP.NETBảo mật ứng dụng ASP.NET
Bảo mật ứng dụng ASP.NETHUST
 
Lesson 22: Flash communicate
Lesson 22: Flash communicateLesson 22: Flash communicate
Lesson 22: Flash communicateHallo Patidu
 
Slide 8 - Thiết kế Web cơ bản
Slide 8 - Thiết kế Web cơ bảnSlide 8 - Thiết kế Web cơ bản
Slide 8 - Thiết kế Web cơ bảnSống Khác
 
[ST] Tài liệu thiết kế website cho người mới bắt đầu
[ST] Tài liệu thiết kế website cho người mới bắt đầu[ST] Tài liệu thiết kế website cho người mới bắt đầu
[ST] Tài liệu thiết kế website cho người mới bắt đầuTin Học KEY
 
Lecture 01ascaccacaWsacascascsacascascWW.pdf
Lecture 01ascaccacaWsacascascsacascascWW.pdfLecture 01ascaccacaWsacascascsacascascWW.pdf
Lecture 01ascaccacaWsacascascsacascascWW.pdfLê Thưởng
 
Lập trình web với các công nghệ phổ biến
Lập trình web với các công nghệ phổ biếnLập trình web với các công nghệ phổ biến
Lập trình web với các công nghệ phổ biếnSon Nguyen
 
Tạo sơ đồ trang web
Tạo sơ đồ trang webTạo sơ đồ trang web
Tạo sơ đồ trang webbrian4282
 

Similar to Bottle web framwork for python (20)

Slide 01-Web Application Overview (1).pdf
Slide 01-Web Application Overview (1).pdfSlide 01-Web Application Overview (1).pdf
Slide 01-Web Application Overview (1).pdf
 
Thiết kế giao diện với Liquid
Thiết kế giao diện với LiquidThiết kế giao diện với Liquid
Thiết kế giao diện với Liquid
 
PHP.pdf
PHP.pdfPHP.pdf
PHP.pdf
 
Bizweb theme workshop
Bizweb theme workshopBizweb theme workshop
Bizweb theme workshop
 
Chuong 1 tong quan về lập trình web động
Chuong 1 tong quan về lập trình web độngChuong 1 tong quan về lập trình web động
Chuong 1 tong quan về lập trình web động
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
 
Bảo mật ứng dụng ASP.NET
Bảo mật ứng dụng ASP.NETBảo mật ứng dụng ASP.NET
Bảo mật ứng dụng ASP.NET
 
Lesson 22: Flash communicate
Lesson 22: Flash communicateLesson 22: Flash communicate
Lesson 22: Flash communicate
 
Lab lap trinhmang_v3
Lab lap trinhmang_v3Lab lap trinhmang_v3
Lab lap trinhmang_v3
 
Web1012 slide 1
Web1012   slide 1Web1012   slide 1
Web1012 slide 1
 
Báo cáo tuần đồ án
Báo cáo tuần đồ ánBáo cáo tuần đồ án
Báo cáo tuần đồ án
 
Slide 8 - Thiết kế Web cơ bản
Slide 8 - Thiết kế Web cơ bảnSlide 8 - Thiết kế Web cơ bản
Slide 8 - Thiết kế Web cơ bản
 
Giới thiệu Embulk
Giới thiệu Embulk Giới thiệu Embulk
Giới thiệu Embulk
 
Bai th08 php voi csdl
Bai th08 php voi csdlBai th08 php voi csdl
Bai th08 php voi csdl
 
[ST] Tài liệu thiết kế website cho người mới bắt đầu
[ST] Tài liệu thiết kế website cho người mới bắt đầu[ST] Tài liệu thiết kế website cho người mới bắt đầu
[ST] Tài liệu thiết kế website cho người mới bắt đầu
 
Lecture 01ascaccacaWsacascascsacascascWW.pdf
Lecture 01ascaccacaWsacascascsacascascWW.pdfLecture 01ascaccacaWsacascascsacascascWW.pdf
Lecture 01ascaccacaWsacascascsacascascWW.pdf
 
Lập trình web với các công nghệ phổ biến
Lập trình web với các công nghệ phổ biếnLập trình web với các công nghệ phổ biến
Lập trình web với các công nghệ phổ biến
 
Tài liệu HTML5-CSS3
Tài liệu HTML5-CSS3Tài liệu HTML5-CSS3
Tài liệu HTML5-CSS3
 
Tạo sơ đồ trang web
Tạo sơ đồ trang webTạo sơ đồ trang web
Tạo sơ đồ trang web
 
Thutap
ThutapThutap
Thutap
 

Bottle web framwork for python

  • 1. Bottle Web Framwork For Python Đinh Nhật An http://pythonvietnam.info atuladn@gmail.com
  • 2. Bottle • Giới thiệu • WSGI (Web Server Gateway Interface) ? • Bottle framework • Install • Request Routing • Generating content • Request data • Template • Demo • Thảo luận
  • 4. • Bottle là một framework phục vụ cho việc phát triển web application dành cho ngôn ngữ python • Đặc điểm chính của bottle: – Nhẹ – Nhanh • Bottle là một framework dựa trên WSGI tương tự như các framework khác flask,bobo, web.py… và bản thân nó đã bao gồm một mini WSGIServer phục vụ cho việc phát triển được nhanh chóng và thuận lợi.
  • 5. WSGI (Web Server Gateway Interface) ? • WSGI là 1 chuẩn định nghĩa việc tương tác giữa web server và web application
  • 6. Bottle framework • Install • Request Routing • Generating content • Request data • Template • Demo
  • 7. Install • Để cài bottle chúng ta có thể làm các cách sau: – wget http://bottlepy.org/bottle.py download đến thư mục chứa code của bạn và import – Sử dụng pip hoặc easy_install hoặc các trình quản lý package tương ứng trên OS như apt-get,yum,
  • 8. Request Routing • Hiện tại bottle hỗ trợ single route hoặc dynamic route dựa trên thông tin url gởi lên nó sẽ route đến đúng function cần xử lý: • Single route – @route('/hello') def hello(): return "Hello World!" • Dynamic route: – @route('/') – @route('/hello/<name>') – def greet(name='Stranger'): – return template('Hello {{name}}, how are you?', name=name)
  • 9. Generating content • Để tạo ra content và trả về cho client bottle hiện hỗ trợ các loại dữ liệu sau: – Dictionaries – Empty Strings, False, None or other non-true values – Unicode strings – Byte strings – Instances of HTTPError or HTTPResponse – File objects
  • 10. Generating content • @app.route('/hello') def hello(): return "Hello World!" • @route('/static/<filename:path>') def send_static(filename): return static_file(filename, root='/path/to/static/files') • @route('/restricted') def restricted(): abort(401, "Sorry, access denied.")
  • 11. Request data • Hiện bottle hỗ trợ xử lý các loại data như cookies,http header,form, query variables… thông qua đối tượng request. Ví dụ: from bottle import route, request, response @route('/counter') def counter(): count = int( request.cookies.get('counter', '0') ) response.set_cookie('counter', str(count)) return 'You visited this page %d times' % count @route('/is_ajax') def is_ajax(): if request.headers.get('X-Requested-With') == 'XMLHttpRequest': return 'This is an AJAX request' else: return 'This is a normal request'
  • 12. Template • Để quá trình phát triển được nhanh bottle hỗ trợ Simpletemplate engine cho việc render trang,tất cả việc bạn cần là gọi tên template và truyền các biến cần thiết,mặc định nó sẽ kiếm template trong thư mục ./views/ ngang cấp với application root folder: • Ví dụ: @route('/hello') @route('/hello/<name>') def hello(name='World'): return template('hello_template', name=name)
  • 13. Demo • Hiện code demo mình tham khảo từ đây : – http://www.tutorialsavvy.com/2013/04/bottle-web-framwork-for- python.html
  • 14. Demo
  • 15. Thank You Đinh Nhật An http://pythonvietnam.info atuladn@gmail.com