BOTTLE Python Web Framework
The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module and has no dependencies, excluding the Python standard library. There are four aspects in Bottle, which are:
Routing: It requests function call mapping with support for clean and dynamic URLs.
Templates: This is a python built-in template engine that supports mako, jinja2, and cheetah templates.
Utilities: Access to data like uploading a file, cookies, headers, and other HTTP media gets easier.
Server: This server has built-in HTTP development and support for paste, Bjoern, gae, etc.
To install bottle framework
As Bottle is independent of any external libraries, so you need to download bottle.py into the project you are using:
$ wget https://bottlepy.org/bottle.py
With this command, you will install the newly developed snapshot that includes all new features. But if you need a stable environment, you can use some stable releases already available on PyPI, which could be installed using pip.
For example
$ sudo pip install bottle
$ sudo easy_install Bottle
$ sudo apt-get install python-bottle
If you are installing Bottle on windows use this command:
Pip install Bottle
If you are installing Bottle on ubuntu or Linux, use this command:
Pip3 install bottle
But you must ensure that before you install or use the bottle framework, your system must have a python version of 2.7 or above. And before using any framework, you must ensure that the virtual environment is set. Otherwise you have to create it using the following command
$ virtualenv develop
$ source develop/bin/activate
$ pip install -U bottle
So, with these three lines we created a virtual environment and changed its default to python virtual and also install Bottle to virtual environment.
As we installed Bottle and the suitable virtual environment, let us look at a basic example using the bottle framework
from bottle import route, run
@route('/hello')
def hello():
return "Hello World."
run(host='localhost', port=8080, debug=True)
you can get the output of this code through this http://localhost:8080/hello
by looking at the code, we find route() it's a python framework decorator that joins a code to a URL path. In the above code, we linked /hello path to hello() function. This is the most important concept of the framework. There is no limit to this you can create as many routes as you need. When there is a request for a URL then the associated function is called, and the return value is sent back.
Run () call is used in the last line of the given code that calls starts a built-in development server. That server runs on localhost port 8080 and won't get terminated until you press control-c. This requires no setup, and it is an easy way to run your applications for local tests.
We also used debug, which is very useful during the starting stages to ensure there are no errors. And we have to be sure that the debug mode gets off when it is in use with a general application.
HTTP request methods
The HTTP protocol defines many request methods for different tasks. GET is the default for all routes. There will be no need to specify any method. All the routes have to match through GET request. Other methods like POST, PUT, DELETE, or PATCH are handled by adding a method keyword argument to the route(); else we use the shortcut of these, which are get(), post(), put(), delete(), or patch().
Example
from bottle import get,post,request,Bottle,run,template
app = Bottle()
@app.get('/updateData')
def login_form():
return template('hello world')
@app.post('/updateData')
def submit_form():
name = request.forms.get('name')
print(name)
return f'<h1>{name}</h1>'
run(app, host='0.0.0.0', port=8000)
Output
As output is a URL if you execute that code you get the following link in the terminal http://localhost:8080/hello/world
In the above example, the /updateData URL is linked to two different callbacks, one for GET and the other for POST requests. The first one displays an HTML form to the user while the second one calls back on form submission and returns hello world.
Conclusion
This covers the basic structure for the Bottle framework. Working on the proper environment for starting a project and a basic example of the construction of a bottle framework. Bottle framework is almost the same as flask but has useful functions like dynamic routing and template responses.