What is Python Bottle?

Bottle is a useful framework for various web development scenarios such as:

  1. Prototyping ideas

  2. Learning how web frameworks are built

  3. Building and running simple personal web applications

Key Features

  1. It is a single file.

  2. It can run as a web server itself or be used behind any web server.

  3. It has a built-in​ template engine called Simple Template Engine.

  4. It contains various plugins for popular databases.

Installation

First of all, we need to install bottle. Write the following command in the terminal:

pip install bottle

Code

In this code, we run the router on the specific host and port address.

  • The code starts by running the application using this command,

    app = bottle.default_app().

  • The user writes the Response which they want to be viewed on the screen. Command: return Response().

  • Write your string within the brackets of Response().

import os
import bottle
# import the following features from bottle
from bottle import route, run, Response
app = bottle.default_app()
@route('/')
def index():
# Text within response is showed on the localhost
return Response("Welcome to my bottle. Hello user.")
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
run(host='127.0.0.1', port=port, debug=True)

Open a web browser after executing main.pymain.py. You should be able to connect to http://localhost:5000/ and see your application running!

Tada!

Output

svg viewer
Copyright ©2024 Educative, Inc. All rights reserved