Do you think IIT Guwahati certified course can help you in your career?
No
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 👨🏫
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.
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.
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
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.
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
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
After creating the file, you will start your postman and hit the URL below.
localhost/seed-users-table
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.