Table of contents
1.
Introduction
2.
Installation of Python and Bottle 👨‍🏫
3.
Setting up the Workspace 
4.
Connection to the Database
5.
Creating Python File👩‍💻
6.
Running the Application 
7.
Frequently Asked Questions
7.1.
What is Python?
7.2.
How bottle work in web applications?
7.3.
Give a comparison between flask and bottle.
7.4.
Is Bottle an MVC framework?
7.5.
What is a WSGI application?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Creating First Web Application Using Bottle Framework

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

Introduction

This article will try to create a web application using bottle-web framework. We will be using python and bottle web frameworks. In the database section, we will be using MySQL. A database can be of your choice. Bottle is a web server gateway interface (WSGI) that is compatible with a single source web - framework, and the only dependency it needs is Python standard library. 

Installation of Python and Bottle 👨‍🏫

python

To start with your web application, it is essential that you already have python and bottle installed in your system. To download the python and bottle framework from their website, you can click here python Download.

Download the bottle from here.

Bottle image

You will be using Visual Studio Code as an IDE for our application.

Setting up the Workspace 

First, you create a folder anywhere on your laptop. Now you open your VS code, and in the terminal, you type 

 py -m venv

 to create a virtual environment. It will take some time to set up.

To complete the setup, we further in the terminal type 

.\Scripts\activate

and press enter.

Scripts

After running the script above, you will give the virtual environment setup.

If the bottle framework has not been installed, you can directly install it from the terminal by writing

 pip install bottle

 Now, as you need MySQL connectivity, you further install the MySQL connector by the following command

pip install MySQL-connector-python
sql install

Connection to the Database

To connect to MySQL, you can use many other GUI tools like a workbench, phpMyAdmin , SQL yoke, table, plus any of those GUI applications, but here, you will use the  MySQL connector that is present in the VS Code.

For downloading that, go to the vs. code plugins section and search MySQL. The MySQL client for the VS Code is the plugin you need to install on your laptop. 

My SQL

 Now you will click on the table bar, put the password, and create a database with the following command.

utf8mb4 :- This is used so that you can be allowed to set the character with emoji. 

CREATE DATABASE company CHARACTER SET utf8mb4 


After this, you will execute the above command by typing 

CTRL+SHIFT+P.

 You will get the run command and perform the run MYSQL Query.

Now your database has been successfully created. Select the Company database and create a new query as shown below

CREATE TABLR users{
user_id SERIAL,
user_name VARCHAR2(20) NOT NULL,
user_last_name VARCHAR2(20) NOT NULL
)ENGINE=InnoDB
create table

Run the query, and your table gets created successfully.

Creating Python File👩‍💻

Now you will create a python file and name those files as server.py(you can name anything).

Here, we import bottle and its functions by using a single line written below.

From bottle import default_app, run, get
Import JSON
Import MySQL.connector


Configure the database in your file

db_config = {
   “host”: “localhost,”
   “user”: ”root”,
   “password”: ”root”,
    “database”: ”company”,
}


Connect the database with the following code in the file

db=mysql.connector.connect(db_config)
Cursor = db.cursor()
Connect to the localhost by writing the following in the py file.
run=(host = ”localhost”, port = 80, debug = True, reloader = True)
user created
@get("/seed-users-table")
def ssed_users_table();
cursor.execute("INSERT INTO users VLAUES(null,'A','A')")
db.commit()
return "USER CREATED";

Running the Application 

After creating the file, you will start your postman and hit the URL below.

localhost/seed-users-table
postman

Congratulations! You have just created your first web application using bottle framework.

Frequently Asked Questions

What is Python?

Python is a multifunctional language. It is used in software testing, website development, and machine learning. It has many frameworks, making it easier to use, create, and build applications.

How bottle work in web applications?

Bottle is a lightweight, easy-to-use, fast-performing web framework that helps build applications faster than other frameworks. It has a single dependency on Python Standard Library.

Give a comparison between flask and bottle.

Bottle is fast and helps to build applications quickly. It contains less information and assistance. Flask, compared to bottle, has extensive files and helps build large and complex applications as it has more significant support and extensions installed.

Is Bottle an MVC framework?

Bottle is fast and helps to build applications quickly. Bottle uses a variation of the MVC design pattern. MVC refers to model, view, and controller. It gives us the ability to split the user interface functions.

What is a WSGI application?

WSGI stands for Web Server Gateway Interface. It is written in python programming language. Web servers forward request using a straightforward calling convention to web apps or frameworks, making them easier to use.

Conclusion

In this article, you have understood how you can create a web application by using the bottle framework. It further gives you insights into how you can hit the API and fetch the data from the database. 

You can further learn more about python frameworks and Python VS JavaScrpit.

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

Happy Learning, Ninjas!

Thankyou image
Live masterclass