Sunday, November 17, 2024
Google search engine
HomeLanguagesIntroduction to Bottle Web Framework – Python

Introduction to Bottle Web Framework – Python

There are many frameworks in python which allows you to create webpage like bottle, flask, django. In this article you will learn how to create simple app bottle.Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.

Routing: Requests to function-call mapping with support for clean and dynamic URLs.

Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.

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

Server: Built-in HTTP development server and support for paste, fapws3, bjoern, gae, cherrypy or any other WSGI capable HTTP server.

In order to create the app using bottle  we have to install it first

Windows

pip install bottle

Ubuntu

pip3 install bottle

By default, if we pass a template name to the SimpleTemplate and it will look for a file by that name in a subdirectory views with the extension .tpl.

First we have to create the directory for our project Test_project

Inside that create a file and name it as app.py

app.py

Python3




from bottle import route, run, template
  
  
@route('/')
def index():
    return template('index.tpl')
  
  
run(host='localhost', port=8080,debug=True)


Then create the new directory  views

Inside that create a file index.tpl

HTML




<html>
    <head>
        <title>GFG</title>
    </head>
    <body>
         <h1>Welcome to GFG</h1>
    </body>
</html>


To run this app open cmd or terminal

Windows

python app.py

Ubuntu

python3 app.py

Output :

To handle POST method in bottle we have to write two functions one for GET method and one for POST method.

Python3




from bottle import get,post,request,Bottle,run,template
  
  
app = Bottle()
  
@app.get('/updateData'# For GET method
def login_form():
    return template('index.tpl')
  
@app.post('/updateData')   #For POST method
def submit_form():
    name = request.forms.get('name')
    print(name)
    return f'<h1>{name}</h1>'
  
run(app, host='0.0.0.0', port=8000)


Inside views directory create new file forms.tpl

HTML




<html>
    <head>
        <title>GFG</title>
    </head>
    <body>
        <form method="post" action="/updateData">
             <input type="text" name="name">
             <button type="submit">Save</button>
        </form>
    </body>
</html>


To run this app open cmd or terminal

Windows

python app.py

Ubuntu

python3 app.py

Output :

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments