Enable Dark Mode!
By: Mufeeda PP

What is Werkzeug Library in Python

Technical Odoo 16

Werkzeug is a collection of libraries that you can use to build Web Server Gateway Interface (WSGI) compliant web applications in Python. It started as a simple collection of miscellaneous utilities for WSGI applications and has grown into one of the most advanced WSGI utility libraries. The choice of template engine, database adapter, and request handling is entirely up to the developer. In other words, don't enforce dependencies. This includes a debugger, request, and response objects, cache control objects, cookie handling, file uploads, and many community added add-ons. Released under the BSD license.


pip install -U Werkzeug
A WSGI application is one that can be called by passing it an environment dict and a callable start_response. The environment contains all the incoming information, and you can mark the start of the response with the start_response function. The tools let you use Request and Response objects, so you don't have to deal with either directly. Request data takes an environment object and allows you to access data from that environment in the appropriate way. The Response object is itself a WSGI application and provides a better way to construct responses.
Let's look at a code example.
from werkzeug.wrappers import Request, Response
def application(environ, start_response):
    request = Request(environ)
    text = f"Hello {request.args.get('name', 'World')}!"
    response = Response(text, mimetype='text/plain')
    return response(environ, start_response)

The Base structure:

class Shortly(object):
    def __init__(self, config):
        self.redis = redis.Redis(
            config['redis_host'], config['redis_port'], decode_responses=True
    def dispatch_request(self, request):
        return Response('Hello World!')
    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)
    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)
def create_app(redis_host='localhost', redis_port=6379, with_static=True):
    app = Shortly({
        'redis_host':       redis_host,
        'redis_port':       redis_port
if with_static:
        app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
            '/static':  os.path.join(os.path.dirname(__file__), 'static')
    return app
if __name__ == '__main__':
    from werkzeug.serving import run_simple
    app = create_app()
    run_simple('', 5000, app, use_debugger=True, use_reloader=True)

Shortly classes are WSGI applications. __call__ methods are sent to wsgi_app. Then you can wrap the wsgi_app and apply the middleware as you would in the create_app function. The actual wsgi_app method then creates a request object and calls the dispatch_request method. This will return a response object and evaluate it as a WSGI application. Both the short class we create and all request objects in the tool implement the WSGI interface. As a result, you can even return another WSGI application from the dispatch_request method. You can use the create_app function to create a new instance of your application. Add a WSGI middleware that exports a static file and some parameters as configuration to your application as needed, as well as being passed. This way, you can access files from your static folder even if you haven't configured your server to serve them. This is very useful for development.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message