SlideShare a Scribd company logo
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

PDF
Chuyen de flask -- pythonvietnam.info
PDF
Authentication and Authorization
PDF
Python Beginner Class day-10-class
PDF
Python Beginner Class day-09-fileio
PDF
Pbc day-01-introduction
PDF
Bai 1 pythonvietnam.info
PDF
Python Beginner Class day-07-08-module
PDF
Python Beginner Class day-03-flow
Chuyen de flask -- pythonvietnam.info
Authentication and Authorization
Python Beginner Class day-10-class
Python Beginner Class day-09-fileio
Pbc day-01-introduction
Bai 1 pythonvietnam.info
Python Beginner Class day-07-08-module
Python Beginner Class day-03-flow

Viewers also liked (6)

PDF
Slide Python Bai 2 pythonvietnam.info
PDF
Python Beginner Class day-04-05-06-iterations
PPTX
Lập trình Python GUI vs PySide
PDF
Git Using - pythonvietnam.info
PDF
Python Beginner Class day-02-strings
PDF
Python Beginner Class day-11-12-13-database
Slide Python Bai 2 pythonvietnam.info
Python Beginner Class day-04-05-06-iterations
Lập trình Python GUI vs PySide
Git Using - pythonvietnam.info
Python Beginner Class day-02-strings
Python Beginner Class day-11-12-13-database
Ad

Similar to Bottle web framwork for python (20)

PDF
Slide 01-Web Application Overview (1).pdf
PPTX
Thiết kế giao diện với Liquid
PDF
PHP.pdf
PDF
Chương 1 - Tổng quan về JSP - Servlet.pdf
PPTX
Bizweb theme workshop
PPTX
Chuong 1 tong quan về lập trình web động
PDF
Bai1 gioi thieu_servlet_va_jsp_8952
PDF
Bảo mật ứng dụng ASP.NET
PPT
Lesson 22: Flash communicate
DOCX
Lab lap trinhmang_v3
PDF
Web1012 slide 1
DOCX
Báo cáo tuần đồ án
PDF
Slide 8 - Thiết kế Web cơ bản
PDF
Giới thiệu Embulk
PDF
Bai th08 php voi csdl
DOC
[ST] Tài liệu thiết kế website cho người mới bắt đầu
PDF
Lecture 01ascaccacaWsacascascsacascascWW.pdf
PPTX
Lập trình web với các công nghệ phổ biến
PDF
Tài liệu HTML5-CSS3
PPTX
Tạo sơ đồ trang web
Slide 01-Web Application Overview (1).pdf
Thiết kế giao diện với Liquid
PHP.pdf
Chương 1 - Tổng quan về JSP - Servlet.pdf
Bizweb theme workshop
Chuong 1 tong quan về lập trình web động
Bai1 gioi thieu_servlet_va_jsp_8952
Bảo mật ứng dụng ASP.NET
Lesson 22: Flash communicate
Lab lap trinhmang_v3
Web1012 slide 1
Báo cáo tuần đồ án
Slide 8 - Thiết kế Web cơ bản
Giới thiệu Embulk
Bai th08 php voi csdl
[ST] Tài liệu thiết kế website cho người mới bắt đầu
Lecture 01ascaccacaWsacascascsacascascWW.pdf
Lập trình web với các công nghệ phổ biến
Tài liệu HTML5-CSS3
Tạo sơ đồ trang web
Ad

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