×

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 History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute 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.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.