Do you think IIT Guwahati certified course can help you in your career?
No
✨Introduction
Plugins are software extensions that enable the customization of website content as well as computer applications, mobile apps, and web browsers.
While plugins are still used as add-ons to tweak programs and apps, they are less frequently utilized in web browsers these days in favour of browser extensions.
✨Bottle Framework and Plugins
Bottle is a Python WSGI micro web framework that is quick, easy, and lightweight. It is supplied as a single file module and only requires the Python Standard Library as a dependency.
The main capabilities of Bottle address the majority of use cases; however, being a micro-framework, it has its limitations. "Plugins" are used in this situation. Plugins automate some monotonous tasks, integrate external libraries, and add the necessary functionality to the framework.
A growing number of plugins are now available, and most of them are intended to be portable and transferable between applications. There is a good probability that someone has previously addressed your issue and created a ready-to-use plugin.
✨Plugin Example
Depending on the particular plugin, each has a different set of effects and APIs. For instance, the SQLitePlugin plugin recognizes callbacks that need a db keyword argument and generates a new database connection object each time the callback is made. This makes using a database very practical:
from bottle import install, route, template
from bottle_sqlite import SQLitePlugin
install(SQLitePlugin(dbfile='/tmp/test.db'))
@route('/show/<post_id:int>')
def show(db, post_id):
c = db.execute('SELECT title, content FROM posts WHERE id = ?', (post_id,))
row = c.fetchone()
return template('show_post', title=row['title'], text=row['content'])
@route('/contact')
def contact_page():
''' No database connection is necessary for this callback. The 'db' keyword parameter is absent, so the sqlite plugin entirely disregards this callback.'''
return template('contact')
You can also try this code with Online Python Compiler
Plugins can be added to the entire program or simply to a few particular routes that require further functionality. The majority of plugins are wise enough to avoid adding overhead to callbacks that do not require their functionality and may be implemented without risk on all routes.
Consider the SQLitePlugin plugin as an illustration. It only impacts route callbacks that need a database connection. The other routes are not used. This allows us to install the plugin throughout the entire application without incurring any additional overhead.
Simply call install() with the plugin as the first argument to install a plugin:
from bottle_sqlite import SQLitePlugin
install(SQLitePlugin(dbfile='/tmp/test.db'))
You can also try this code with Online Python Compiler
👉The plugin has not yet been implemented for the route callbacks. To ensure that no routes are missed, this is being delayed. If you like, you can install plugins first and add routes later. However, the sequence in which the plugins are installed matters. Install the database plugin first if a plugin needs to connect to a database.
✨Uninstalling Plugins
A previously installed plugin can be uninstalled using a name, class, or instance as shown below:
# installing the plugin first
sqlite_plugin = SQLitePlugin(dbfile='/tmp/test.db')
install(sqlite_plugin)
uninstall(sqlite_plugin) # uninstalling a specific plugin
uninstall(SQLitePlugin) # uninstalling all plugins of that type
uninstall('sqlite') # uninstalling all plugins with that name
uninstall(True) # uninstalling all plugins at once
You can also try this code with Online Python Compiler
👉Plugins can be added and taken away at any time, even in the middle of processing requests. This helps us with some neat tricks (installing slow debugging or profiling plugins only when needed) but should not be used frequently. The route cache is flushed, and all plugins are reapplied each time the list of plugins is updated.
✨Route Specific Installation
If you simply want to install plugins on a few routes, the apply option of the route() decorator is useful:
sqlite_plugin = SQLitePlugin(dbfile='/tmp/test.db')
@route('/create', apply=[sqlite_plugin])
def create(db):
db.execute('INSERT INTO ...')
You can also try this code with Online Python Compiler
For a few routes, you might want to explicitly disable a plugin. For this reason, the route() decorator has a skip parameter:
sqlite_plugin = SQLitePlugin(dbfile='/tmp/test1.db')
install(sqlite_plugin)
dbfile1 = '/tmp/test1.db'
dbfile2 = '/tmp/test2.db'
@route('/open/<db>', skip=[sqlite_plugin])
def open_db(db):
# The 'db' keyword argument is not touched by the plugin this time.
# The plugin handle can be used for runtime configuration, too.
if db == 'test1':
sqlite_plugin.dbfile = dbfile1
elif db == 'test2':
sqlite_plugin.dbfile = dbfile2
else:
abort(404, "No such database.")
return "Database File switched to: " + sqlite_plugin.dbfile
You can also try this code with Online Python Compiler
👉A list of values or a single value can be passed as a skip argument. The plugin that is to be skipped can be identified by name, class, or instance. To skip all plugins at once, set skip=True.
✨Plugins and Sub-Applications
The majority of plugins are unique to the program for which they were installed. Therefore, they shouldn't have an impact on auxiliary applications mounted via Bottle.mount (). Here's an illustration:
Bottle creates a proxy-route on the main application whenever you mount an application, which routes all requests to the mounted application. Plugins are by default disabled for this style of proxy-route. As a result, the routes of the /blog sub-application are unaffected by our (fictitious) WTForms plugin, but it has an impact on the /contact route.
👉Although it can be changed, this default behavior is meant to be sensible. The example below reactivates each plugin for a certain proxy-route:
root.mount('/blog', apps.blog, skip=None)
You can also try this code with Online Python Compiler
👉However, there is a problem: The plugin views the entire sub-application as just one route, specifically the aforementioned proxy-route. You must specifically apply the plugin to the mounted application in order for it to influence each unique route of the sub-application.
✨List of Some Plugins in Bottle Framework
Here is the list of some most frequently used plugins for bottle framework.
Plugin Name
Description
Bottle-Beaker
Beaker to session and caching library with WSGI Middleware
Bottle-Cork
For Bottle-based online applications, Cork offers a straightforward set of methods for implementing authentication and authorization.
Bottle-Cors-plugin
The simplest method to integrate cors into your bottle online application is with the cors-plugin.
Bottle-Extras
Bottle plugin installation using the meta package.
Bottle-Flash
flash plugin for bottle framework.
Bottle-Hotqueue
Building a FIFO Queue for Bottle on redis.
Bottle-Memcache
Memcache integration for Bottle.
Bottle-Mongo
MongoDB integration for Bottle.
Bottle-Sqlalchemy
SQLAlchemy integration for Bottle.
Bottle-Sqlite
SQLite3 database integration for Bottle.
Bottle-Web2pydal
Web2py Dal integration for Bottle.
Frequently Asked Questions
What is bottle API?
Bottle is a Python WSGI micro web framework that is quick, easy, and lightweight. It is supplied as a single file module and only requires the Python Standard Library as a dependency.
Is Flask better than bottle?
Bottle and Flask are both extensively used as Python's backend web frameworks. In any case, Flask is thought to be more effective, and as a result, programmers frequently select it over Bottle.
Is bottle an MVC framework?
Similar to most frameworks, Bottle uses an MVC software paradigm. Model, View, and Controller, or MVC, is an acronym for the choice to divide a user interface's various functions.
How do you run Python bottle?
Go to http://localhost:8080/ in your browser after running the script or pasting it into a Python console. Use pip install bottle to install the most recent stable version, or download bottle.py (unstable) and place it in your project directory.
What is bottle used for in Python?
It is supplied as a single file module and only requires the Python Standard Library as a dependency. Routing: Support for clean and dynamic URLs and requests to function-call mapping.
Conclusion
In this article, we have learned to generate content in the bottle framework. Also, we have discussed about static files, HTTP errors and redirects, response object, and cookies. That’s all from the article. I hope you all like it.