Python Web Development | Build Best Website using Bottle Framework

Python Web Development | Build Best Website using Bottle Framework

Python Web Development | Build Best Website using Bottle Framework

Do you want to learn- how you can develop a simple website using Python?

In an earlier post, Heena has composed what are the 5 top programming languages one should learn to get a good opportunity in the software job. And of course, Python is one of the prominent languages.

Python provides a wide range of libraries that are useful for many diversified applications. Python Web Development is one of the most considerable reasons for its popularity.

So if you are looking for a framework for your backend web application, Python bottle framework is the far most right choice.

I am working on a bottle framework for one of the projects on python web programming. And I found it as simple, proven and easiest way for web development.

In this tutorial, we will see how to use bottle framework to create the interactive website.

Python Web Development bottle framework

Defining bottle framework: is a micro web framework for python. It provides the Web Server Gateway Interface (WSGI). Not necessary but if you are interested to know more detail about WSGI, you can read Python standard described in detail in PEP 3333.

Set up Require for Python Web Development using Bottle Framework:

  • Install Python on your system.
  • Create HTTP localhost server.
  • Create the project folder in the www folder in localhost.
  • Install bottle python library. Use command “pip install bottle” to install. Otherwise, the easiest way is to Copy from Github into the project folder.

So now the setup is ready.

You can check out simply by running localhost:80 in a browser. Ensure localhost is working fine.

Let’s begin with programming.

Import libraries required for Python Web Development:

First, you need to import route, run and template from bottle framework.

from bottle import route, run, template

These are the three basic units we need for python web programming.

Route function is used to decide which function to be called for a particular URL. It makes the mapping between the URL and the function to accept a user request and give a response.

The function will get called if the URI you entered in browser match to the route parameter. This we see in the example in detail.

The run function starts the bottle server with the IP address provided in the first parameter and port provided in the second parameter.

run(host='localhost', port=8080)

Note: In our case, we have mentioned the localhost as host as we are running this code on a local machine. If you are deploying this project on the server using the server IP address as the host.
Port number can be any dynamic port number. Here port 8080 is an alternative port for HTTP 80.

Templet is used to creating the HTML view as a response.

We have seen all the 3 important components. Let’s merge it and consolidate a simple hello world program.

Hello World using Bottle Framework

Python web development Example: “Hello World” using a bottle framework.

Write the following code in in the project folder.

from bottle import route, run, template

def index():
    return template('<b>Hello World!</b>')

run(host='localhost', port=8080)

Now the server is ready to run.

Run the python program in python console.

Open the browser and run command http://localhost:8080/hello/

That’s it! This creates a static content web page. You can see the HTML response as Hello World.

Now let’s make it little interactive.

Copy and paste the following code in

from bottle import route, run, template

def index(val):
    return template('<b>Hello {{name}}</b>!', name=val)

run(host='localhost', port=8080)

It receives the name as a parameter which can be used to make an HTML page with dynamic content as per the URL.

This parameter value can be used for data processing inside the function as well.

Open the browser and run command.


HTML Output looks like “Hello Ani!”

This makes the content responsive and personalized, doesn’t it?

Explore python web programming Further:

This is basic for web development. A web developer does not stop here. They need to explore it further.

So allow me to discuss more points here.

You can handle multiple URL using a single function.

What if we integrate both the above code and make something awesome?

from bottle import route, run, template

def index(val=None):
    if val != None:
    	return template('<b>Hello Stranger!</b>')
	return template('<b>Hello {{name}}!</b>', name=val)

run(host='localhost', port=8080)

greetUser() function is called for both of the following URLs.

Case 1: 
URL: http://localhost:8080/hello/Ani.
Output: "Hello Ani!"

Case 2:
URL: http://localhost:8080/hello/
Output: "Hello Stranger!"

Up to now, we made the client request using URL in a browser.

Let us see.

How web developer make use of curl command?

Like URL in browser same way, you can even use curl command from the command prompt or terminal (in the case of Linux) to request server.

Simple curl command to get bottle server response.

curl -X GET http://localhost:8080/

Many of us will ask what is the use of curl command if we have a web browser to run URL?

In curl command, we can make a complete header request along with URL. The header may include a body (containing data to post on the server), URL request method (POST/PUT/PATCH).

If you are building an application that allows the user to log in. It is not good practice to mention login credential in URL. I mean you should not use it, STRICTLY, as per the security is a concern. So you can pass login username and password in URL header body using curl command.

Python Web development Example: User Login Web page

The simple way is to pass the username and password in the header body.

This is quite difficult to send through the browser request.

The best part of the bottle framework is that route handles the method type in the request.

Write the below code in to handle URL request method type.

from bottle import route, run, template

def index(val=None):
	pass = "secrete"
 	request.method == 'PATCH':
            strBodyText =
	    if strBodyText == pass:
        	return template('<b>Hello {{name}}!</b>', name=val)
		return template('<b>Password is invalid!</b>')

run(host='localhost', port=8080)

login() function is used to validate the login password.

Here is a simple way to pass the value in the body of HTML request.

Case 1:
Command: curl -X GET -d '{"secrete123"}'
Output: "Welcome Ani!"

Case 2:
Command: curl -X GET -d '{"secrete122"}'
Output: "Password is invalid!"

Restful APIs are one of the emerging topics for accessing web resources. You can use these REST APIs for python web programming.

This is the first web development tutorial I perceive to share interestingly. Hope this Python web development tutorial makes your understanding over bottle framework to start developing your own website.

Let’s have a fair discussion if you have any concern about python web programming. Do post your query below in the comment.

Feel free to share with your friends if they are indulging in website or web app development. I am sure they will find this python web development as one of the easiest and prominent perk.

Leave a Reply

Your email address will not be published. Required fields are marked *