Bottle is a great python web framework for building small web applications, rapid prototyping and getting started with web development. I like it very much and one of the reason is the fact that it is distributed as a single file module and has no dependencies other than the Python Standard Library.
Routing: Requests to function-call mapping with support for clean and dynamic URLs.
Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.
Utilities: Convenient access to form data, file uploads, cookies, headers and other HTTP-related metadata.
Server: Built-in HTTP development server and support for paste, fapws3, bjoern, Google App Engine, cherrypy or any other WSGI capable HTTP server.
How To Download And Install bottle In Your Machine
You can easily install bottle in Ubuntu and Debian based systems with the pip python package manager. Install pip with the following command.
sudo apt-get install python-pip
Then install bottle.
sudo pip install bottle
Or install the python micro web-framework directly with the following command.
sudo apt-get install python-bottle
After the installation is finished open a new terminal, run python in interactive mode and try to see if you can import bottle.
If import bottle does not produce any error it means the installation of bottle is done right. Is there any way to use bottle without installing it? Yes, it is.
You can download bottle into your project directory and start coding immediatly. Use the wget utility to download it like shown below.
After the download is finished copy bottle.py to your project directory and start programming. Now that you have installed bottle it is time to start and code a simple web application.
Simple Web Application In Bottle
Create the app.py file with your favourite text editor. I will use vim for this tutorial. You can easily install vim in Ubuntu and debian based systems with the following command.
sudo apt-get install vim
Create app.py file and open it for editing.
In order to create our simple web application and make it work we need to import route and run from bottle like shown below.
from bottle import route, run
What is route? What about run? Why do we need to import them? How are we going to use them in our script? Can you explain that to us?
route() is a decorator that will help us to bind a piece of code to an url path. For example the following code binds the my_app() function to '/myapp' path.
The piece of code shown below is not part of our script. I am showing it to you only for learning purposes so you can easily understand how the route() decorator works later when we build our simple script.
return "My first web app"
run() will start a built-in development server which will run on localhost on port 8080 and will serve requests.
Now that you know all that is needed for building our first web application in python using the bottle micro web-framework it is time to put everything you learned in practise and enjoy the results.
from bottle import route, run
Then type the piece of code shown in the following picture.
We created a decorator and binded the test() function to /test path. Then run() calls the builtin development server like shown below.
Save the app.py file, quit and run the following command to execute it.
To see the results visit http://localhost:8080/test
You should see the same output as shown in the following picture.