×

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

Keyboard Jump Game in Python

Keyword hop (jump) game is a speed composing game that aides in further developing the composing velocity of players The object of Keyboard Jump (hop) Game Python Project is to fabricate...

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

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

5 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python Set difference_update() method

Python Set difference_update() method The set.difference_update() method in Python removes the items that exist in both sets. Syntax set.difference_update(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return None Example 1 # Python program explaining # the set.difference_update()...

2 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

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.