Table of contents
1.
Introduction
2.
Overview
3.
Requirements
4.
Installing Bottle Module
5.
Importing Bottle
6.
Creating a Dictionary 
7.
Getting Employee Details
8.
Testing the API
9.
Get a Particular Employee's Detail
10.
Adding the Employee
11.
Deleting the Employee Details
12.
Frequently Asked Questions
12.1.
What is Bottle API?
12.2.
What is Django and the Bottle?
12.3.
Is Apache a WSGI?
12.4.
Is REST API a framework?
12.5.
What does WSGI stand for?
13.
Conclusion
Last Updated: Oct 28, 2024
Easy

Building rest API with Bottle Framework

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The bottle is a python micro-framework by which you can build the rest API in minutes. It is distributed as a single file module and has only Python Standard Library dependency. Bottle helps in creating web development fastly and easily. 

Bottle framework

The bottle consists of some terms which are as follows:

Templates: Bottle has a Pythonic and a fast built-in template engine and support for jinja2, mako, and cheetah templates.

Routing: It requests function-call mapping with support for dynamic and clean URLs.

Server: Bottle has a built-in HTTP development server, and it supports gae,bjoern, paste,cherypy, or other WSGI-capable HTTP servers.

Utilities: Suitable access to form data, file uploads, headers, cookies, and other HTTP-related metadata.

Overview

First, we will create a dictionary that will store all the employees' information.

Next, we will implement the get service method to display all employee information.

Then we will implement a get service for displaying information of a particular employee according to the name.

Overview

Then we have to create post service method for adding information about employees to the database.

And also create a delete service method for deleting the employee's information.

Requirements

Requirements
  • Vs code (or any other Ide according to you)
  • BOTTLE framework
  • POSTMAN REST client for testing
  • And some basic knowledge about python

Installing Bottle Module

  • Now first, make sure that you have python installed on your system. Now go to Command prompt and type the command pip install bottle; this command will install the bottle on your system.
  • Next step, you have to open your vs code and, under the folder, create the rest.py file.
Screenshot 1

Importing Bottle

You have to type the code inside the rest.py file

importing bottle

Bottle library consists of the run() method. This method is used to start a server instance. Here, as we can see, run() contains two parameters:

  • reloader: whenever we make changes in our code, we have to restart the server, and in that case, the reloader starts auto-reloading the server. By default, its value is False.
  • debug: debug is useful in early development. It gives complete information whenever there is an error.

Creating a Dictionary 

Here, we will create a dictionary containing information about employees.

creating a dictionary

The dictionary name here is the employee. It has two items name and age. It's up to you to take details of an employee as I have taken two here.

Getting Employee Details

First, remember to import get service.

importing get

This statement means any get request coming in between will be passed in the function underneath the Decorator.

Getting employee details

📌 we have to create a decorator named @get().

📌 Decorator is nothing but a python object which is used for modifying a function or a class.

📌 Moreover, for a considerable list of decorators, check Python Decorator Library

📌 we also pass a null point called employee.

📌 Then we need to call a function getAllEmployee(). And it will simply return the dictionary with key: employees and value: employee.

📌 This will return all the items stored in the dictionary.

 

The complete code is as follows:

Getting detail 2

Testing the API

Hence when we will run the above code, we will get an URL as shown below:

Testing API

Now here we are using the POSTMAN REST client for testing API. Below shown is the postman screen.

Postman

Now when we will type the URL and click send button, we will get the following result:

Postman diag1

Get a Particular Employee's Detail

If we want, we can also get a particular employee's detail instead of all the employees' details. Let's see how.

Fetching detaill by name

📌Again, we have taken a decorator and passed a variable name.

📌 And then we have defined a method get_Emp_by_name(name). This method takes the parameter that the variable has passed to the Decorator.

📌 And now we have created a variable that searches the dictionary by name.

📌 and at last, we have returned the selected employee.

 

Now, if we will run the postman application with the new URL along with the employee name, we will get the following result:

Postman snapshot

Adding the Employee

Now let's try to add the new employee to the list of employees. In order to do that, we must import the request and post method.

Importing POST Method
Adding Employee

📌 Here, we have created a new post decorator

📌 And then we have called a function add_emp(). The task of this function is to tale JSON objects with the information given in the dictionary.

📌 Also, we have created a variable new_emp that will add the employee details.

📌 append() method will add the new employee detail to the existing one.

 

Now in the postman application, select the POST from the dropdown menu. Click on the Body and then select raw and in the text option, select JSON. And then enter the details of the employee :

Adding detail in postman

On clicking the send button, we will get the following result:

Result

Here we can see the name: Brian and age:32 is added.

Deleting the Employee Details

Now the last we are doing is to delete the employee detail. For this, we have to write the code given below:

Import the delete library.

importing delete method

Now we will create a delete decorator. The code is the same as getting employee detail with just a little change. Let's see the code:

Deleting employee

📌 In the above code, we have called a method remove_emp() and passed the parameter name i.e., employee name.

📌 Then, we created a variable for searching the selected employee name in the dictionary

📌  and we have used the remove() method to delete the selected object from the employee table.

📌 and at last we have returned the updated list of employees.

 

For running the above code in the postman application, select enter URL along with the employee name to be deleted and select DELETE from the dropdown menu. We will get the result as shown below:

Postman result

Finally, we can see employee Henry is deleted from the list.

We have successfully built a rest API using the Bottle framework by the above code. For creating this Rest API we have used VS code you can choose IDE according to you. 

Frequently Asked Questions

What is Bottle API?

The bottle is a python micro-framework by which you can just build the rest API in minutes. It is distributed as a single file module and has only Python Standard Library dependency. Bottle helps in creating web development fastly and easily. 

What is Django and the Bottle?

Model-template-view (MTV) is the basis for its design. It includes many tools that application developers require, including an ORM framework, admin panel, directory structure, and more.

Is Apache a WSGI?

A Python application is embedded within the Apache HTTP Server using the mod WSGI module, which enables communication via the Python WSGI interface as specified in Python PEP 333. One Python method for creating high-quality, high-performance web apps is WSGI.

Is REST API a framework?

The REST API is nothing but a part of the framework that handles requests from external consumers.

What does WSGI stand for?

The Web Server Gateway Interface (pronounced whiskey or WIZ-ghee) is a straightforward calling standard used by web servers to route requests to web applications or frameworks created in the Python programming language.

Conclusion

This article discussed the bottle framework and how to build the rest API using Bottle Framework. We started with introducing the bottle framework, and then we saw the requirements to make the rest API and how to install the bottle module, import the bottle, create a dictionary, get employee details, Test the API, and add and delete the employee from the list.

To learn more about  Bottle Web Framework, see Bottle DocumentationBottleand Python Frameworks.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thankyou
Live masterclass