📜 ⬆️ ⬇️

Easy python web framework: Bottle

Introduction


Recently, I realized that on Habré there is not a single article dedicated to the wonderful Bottle framework, which, by the way, is used by quite a few people, so in this article I will try to tell you about it.

Installation


Bottle is a very lightweight framework and fits into just one file - bottle.py. You can install it from here , or make a pip install bottle.

Opportunities


Despite its minimalism, Bottle provides quite ample opportunities, which are 100% enough for small and medium-sized projects. Here is a list of key features:

Routing

Routing in the bottle, as in most python frameworks, is done with the help of decorators. For example:
@route('/hello/<name>') def index(name): return name 

You can also create dynamic urls based on regular expressions:
 @route('/news/<number:re:[0-9]*>') def show_news(number): pass 

Templates

One of the strengths of the framework is the template mechanism. To use the template engine, it is enough to write such a light construction:
 template('template_name', name=name, number=number, foo=bar) 

The first argument of the function is the name of the file, which contains the text of the template (in our case, the template will be called template_name.tpl).
In the file itself, we need to write the name of the variable in two braces:
 Hello, {{name}}, glad to see you! 

By default, it is done so that if html code is specified in brackets, then it will not be executed, in order to avoid XSS attacks. If we really need it, you can write {{! Name}}. Also Bottle gives us a very very cool opportunity: to write any python code inside the template. To call a python, it is enough to put% at the beginning of the line. For example:
 %a = 100500 %for i in xrange(a): <div class="image_{{i}}"><img src="......{{i}}.jpg"></div> %end 

You can also include templates from templates, which allows us to beautifully and neatly contain templates.
 %include template_num2 foo=bar, blabla=qweqwe 

POST-routing and form processing

What kind of normal framework can exist without the ability to process POST requests with the subsequent processing of forms?
The mechanism for processing POST requests is exactly the same as for processing GET requests, just the word route needs to be replaced by post:
 @post("/url") def foo(): pass 

To access the forms, the attributes of the name field are used. For example:
 <input name="age" placeholder=""> 

To get the contents of the form, you need to use the following construction:
 request.forms.get("age") #     age request.forms.getall("age") #     age 

You can also handle files:
 request.files.get("picture") #      picture request.files.getall("picture") #      (mult-upload) 

Cookies

Handling cookies in a bottle is very simple to set a cookie:
 response.set_cookie("name", value, max_age=100500) 

To take the value:
 request.get_cookie("name") 

Server

A simple http server is sewn into the bottle, which is suitable only for very fast testing of one page:
 run(host='localhost', port=8080) 

Naturally, for larger projects it is impossible to use it, so you must somehow associate the bottle with apache or nginx. Honestly, I myself always use apache, so I can only tell about it, but with ngninx, everything seems pretty simple too. Bottle communicates with Apache through mod_wsgi. In order to implement this, you need to do the following:
  1. Create an adapter.wsgi file with this content.
    spoiler
     import sys, os, bottle sys.path.append(os.path.dirname(os.path.abspath(__file__))) os.chdir(os.path.dirname(os.path.abspath(__file__))) import index #   application = bottle.default_app() 

  2. Install and enable mod_wsgi
  3. Add virtual host settings
    spoiler
     <VirtualHost *:80> DocumentRoot /var/www/foo </VirtualHost> <Directory /var/www/foo> Options FollowSymLinks ExecCGI AddHandler wsgi-script .wsgi Order allow,deny AllowOverride All Allow from all </Directory> 

    ')

Frequent errors and their solutions



Conclusion


Naturally, it is difficult in one post to fit all the information about the framework, I wrote only the most important thing. The benefit of bottle has pretty good documentation , so check it out and read. Have a great weekend!

Source: https://habr.com/ru/post/221659/


All Articles