Package: growler
master‘devGrowler is a web framework built atop asyncio, the asynchronous library described inPEP
3156and added to the standard library in python 3.4.
It takes a cue from theConnect&Expressframeworks in thenodejsecosystem, using a
single application object and series of middleware to process HTTP requests.
The custom chain of middleware provides an easy way to implement complex applications.InstallationGrowler is installable via pip:$pipinstallgrowlerThe source can be downloaded/cloned from github athttp://github.com/pyGrowler/Growler.ExtrasThe pip utility allows packages to provide optional requirements, so features may be installed
only upon request.
This meshes well with the minimal nature of the Growler project: don’t install anything the
user doesn’t need.
That being said, there are (will be) community packages that areblessedby the growler
developers (after ensuring they work as expected and are well tested with each version of
growler) that will be available as extras directly from the growler package.For example, if you want to use the popularmakohtml
template engine, you can add support easily by adding it to the list of optionals:$pipinstallgrowler[mako]This will automatically install the mako-growler packge, or growler-mako, or whatever it is
named - you don’t care, it’s right there, and it works! Very easy!The goal here is to provide a super simple method for adding middleware packages that the user
can be sure works with that version of growler (i.e. has been tested), and has the blessing of
the growler developer(s).The coolest thing would be to describe your web stack via this command, so if you want mako,
coffeescript, and some postgres ORM, your install command would look likegrowler[mako,coffee,pgorm]; anybody could look at that string and get the birds-eye
view of your project.When multiple extras are available, they will be listed here.UsageThe core of the framework is thegrowler.Appclass, which links the asyncio server to your
project’s middleware.
Middeware can be any callable or coroutine.
The App object creates a request and a response object when a client connects and passes the
pair to this middleware chain.
Note: The middleware are processed in thesame orderthey are specified - this could cause
unexpected behavior (errors) if a developer is not careful, so be careful!
The middleware can manipulate the request and response, adding features or checking state.
If any respond to the client, the middleware chain is finished.
This stream/filter model makes it very easy to modularize and extend web applications with many
features, backed by the power of python.Example UsageimportasynciofromgrowlerimportAppfromgrowler.middlewareimport(Logger,Static,StringRenderer)loop=asyncio.get_event_loop()# Construct our application with name GrowlerServerapp=App('GrowlerServer',loop=loop)# Add some growler middleware to the applicationapp.use(Logger())app.use(Static(path='public'))app.use(StringRenderer("views/"))# Add some routes to the application@app.get('/')defindex(req,res):res.render("home")@app.get('/hello')defhello_world(req,res):res.send_text("Hello World!!")# Create the server - this automatically adds it to the asyncio event loopServer=app.create_server(host='127.0.0.1',port=8000)# Tell the event loop to run forever - this will listen to the server's# socket and wake up the growler application upon each connectionloop.run_forever()This code creates an application which is identified by ‘GrowlerServer’ (this name does nothing
at this point), and a reference to the event loop.
Requests are passed to some middleware provided by the Grower package: Logger, Static, and
StringRenderer.
Logger simply prints the ip address of the connecting client to stdout.
Static will check a request url path against files in views/, if one of the files match, the
file type is determined, proper content-type header is set, and the file content is sent.
Renderer adds the ‘render’ method to the response object, allowing any following function to
call res.render(‘/filename’), where filename exists in the “views” directory.Decorators are used to add endpoints to the application, so requests with path matching ‘/’
will callindex(req, res)and requests matching ‘/hello’ will callhello_world(req, res).
Because ‘app.get’ is used, only HTTPGETrequests will match these endpoints.
Other HTTP ‘verbs’ (post, put, delete, etc) are available as well as ‘all’, which matches any
method.
Verb methods must match a path in full.The ‘use’ method takes an optional path parameter (e.g.app.use(Static("public"),'/static')), which calls the middleware anytime the request pathbeginswith the parameter.The asyncio package provides a Server class which does the low-level socket handling for the
developer, this is how your application should be hosted.
Callingapp.create_server(...)creates an asyncio Server object with the event loop given
in app’s constructor, and the app as the target for incomming connections; this is the
recommended way to setup a server.
You can’t do much with the server directly, so after creation the event loop must be given
control of the thread
The easiest way to do this is to useloop.run_forever()afterapp.create_server(...).
Or do it in one line withapp.create_server_and_run_forever(...).ExtensionsGrowler introduces the virtual namespacegrowler_extto which other projects may add their
own growler-specific code.
Of course, these packages may be imported in the standard way, but Growler provides an
autoloading feature via the growler.ext module (note the ‘.’ in place of ‘_’) which will
automatically import any packages found in the growler_ext namespace.
This not only provides a standard interface for extensions, but allows for different
implementations of an interface to be chosen by the environment, rather than hard-coded in.
It also can reduce the number of import statements at the beginning of the file.
This specialized importer may be imported as a standalone module:fromgrowlerimportApp,extapp=App()app.use(ext.MyGrowlerExtension())...or a module to import ‘from’:fromgrowlerimportAppfromgrowler.extimportMyGrowlerExtensionapp=App()app.use(MyGrowlerExtension())...This works by replacing the ‘real’ ext module with an object that will import submodules in the
growler_ext namespace automatically.
Perhaps unfortunately, because of this there is no way I know of to allow theimport growler.ext.my_extensionsyntax, as this skips the importer object and raises an
import error.
Usersmustuse thefrom growler.ext import ...syntax instead.The best practice for developers to add their middleware to growler is now to put their code in
the python module growler_ext/my_extension.
This will allow your code to be imported by others viafrom growler.ext import my_extensionor the combination offrom growler import extandext.my_extension.An example of an extension is theindexerpackge, which hosts an automatically generated
index of a filesystem directory.
It should implement the best practices of how to write extensions.MoreAs it stands, Growler is single threaded, partially implemented, and not fully tested.
Any submissions, comments, and issues are greatly appreciated, but I request that you please
follow the Growlercontributingguide.The name Growler comes from thebeer bottlekeeping in line with the theme of giving
python micro-web-frameworks fluid container names.LicenseGrowler is licensed underApache 2.0.