×

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.


Related Topics

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

Python sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

How to slice a list in python

When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to...

5 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.