Lably
Skip to content

CherryPy

Web application framework

  • 3 min read
  • 2 chapters

Related on Lably

Through Object-oriented programming, Python (programming language) and Web application

CherryPy can be a web server itself or one can launch it via any WSGI compatible environment. It does not deal with tasks such as templating for output rendering or backend access. The framework is extensible with filters, which are called at defined points in the request/response processing.

Pythonic interface

One of the goals of the project founder, Remi Delon, was to make CherryPy as pythonic as possible. This allows the developer to use the framework as any regular Python module and to forget (from a technical point of view) that the application is for the web.

For instance, the common Hello World program with CherryPy 3 would look like:

<span id="mwJg"></span><span class="kn" id="mwJw">import</span><span class="w" id="mwKA"> </span><span class="nn" id="mwKQ">cherrypy</span> <span class="k" id="mwKg">class</span><span class="w" id="mwKw"> </span><span class="nc" id="mwLA">HelloWorld</span><span class="p" id="mwLQ">:</span> <span class="k" id="mwLg">def</span><span class="w" id="mwLw"> </span><span class="nf" id="mwMA">index</span><span class="p" id="mwMQ">(</span><span class="bp" id="mwMg">self</span><span class="p" id="mwMw">):</span> <span class="k" id="mwNA">return</span> <span class="s2" id="mwNQ">"Hello World!"</span> <span class="n" id="mwNg">index</span><span class="o" id="mwNw">.</span><span class="n" id="mwOA">exposed</span> <span class="o" id="mwOQ">=</span> <span class="kc" id="mwOg">True</span> <span class="n" id="mwOw">cherrypy</span><span class="o" id="mwPA">.</span><span class="n" id="mwPQ">quickstart</span><span class="p" id="mwPg">(</span><span class="n" id="mwPw">HelloWorld</span><span class="p" id="mwQA">())</span>

Or, as the example currently shows on their website, using Python decorators to automatically expose the index route:

<span id="mwRA"></span><span class="kn" id="mwRQ">import</span><span class="w" id="mwRg"> </span><span class="nn" id="mwRw">cherrypy</span> <span class="k" id="mwSA">class</span><span class="w" id="mwSQ"> </span><span class="nc" id="mwSg">HelloWorld</span><span class="p" id="mwSw">(</span><span class="nb" id="mwTA">object</span><span class="p" id="mwTQ">):</span> <span class="nd" id="mwTg">@cherrypy</span><span class="o" id="mwTw">.</span><span class="n" id="mwUA">expose</span> <span class="k" id="mwUQ">def</span><span class="w" id="mwUg"> </span><span class="nf" id="mwUw">index</span><span class="p" id="mwVA">(</span><span class="bp" id="mwVQ">self</span><span class="p" id="mwVg">):</span> <span class="k" id="mwVw">return</span> <span class="s2" id="mwWA">"Hello World!"</span> <span class="n" id="mwWQ">cherrypy</span><span class="o" id="mwWg">.</span><span class="n" id="mwWw">quickstart</span><span class="p" id="mwXA">(</span><span class="n" id="mwXQ">HelloWorld</span><span class="p" id="mwXg">())</span>

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication