- Bottle framework is for Python based web development.
- The main features for this framework is light and fast.
- It is WSGI (Web Server Gateway Interface) based framework.
- WSGI is an interface specification for server and application communicates.
- In this Demo, “We will see how bottle web framework .The python file will run and listen to request on port 9093 and passes the request to specific “.tpl” and send the response to browser.”.
- you can check the below URL to know about the installation of bottle framework in python:-
- Python Bottle Web Project Structure:-
- The bottle_demo.py file contains the following code :-
from bottle import route, run,template @route('/') @route('/myapp') @route('/myapp/') def myApp(): return '<b style="color:green">Hello Sandeep .Bottle Framework demonstration route.</b>' @route('/myapp/<name>') def myName(name): return template('<b style="color:green">Hello <b style="color:red"> {{name}}</b>. Bottle Framework demonstration route with template string.', name=name) @route('/myapp/favourite/') @route('/myapp/favourite/<item>') def favourite(item): return template('favourite_template', item=item) run(host='localhost', port=9093)
- The template file “favourite_template.tpl” for web project,
<!DOCTYPE html> <html> <head> <title> Tutorialsavvy.com : Bottle framework template demo </title> <style> .searchkeyword{ color:blue; } .fruit{ color:green; } .flower{ color:orange; } .else-style{ color:grey; } </style> </head> <body> <h3>Your Favourite <span class='searchkeyword'>{{item}}</span> are :</h3> %if 'fruit'== item: <ul class="fruit"> <li>Orange</li> <li>Apple</li> <li>Mango</li> <li>Water Melon</li> <li>Grapes</li> </ul> %elif 'flower' == item: <ul class="flower"> <li>Rose</li> <li>Jasmine</li> <li>Lotus</li> <li>Tulip</li> <li>Lily</li> </ul> %else : <h3 class="else-style">No Fruit or Flower catagory is Selected</h3> </body> </html>
- The Python Run As for .py file is as below,
- “route” notation forwards the request to a specific function.It can have separate template file for response also.Below screenshots are for different URL with different parameters,
— [http://localhost:9093/ , http://localhost:9093/myapp/] screenshot,
— [http://localhost:9093/myapp/sangeeta] screenshot,
— [http://localhost:9093/myapp/sangeeta] screenshot,
—[http://localhost:9093/myapp/favourite/fruit] screenshot,
—[http://localhost:9093/myapp/favourite/flower] screenshot,
—[http://localhost:9093/myapp/favourite/xyz] screenshot,
— After running these URL we can see the console in Eclipse for logs,