This is my first time delving into web development in python. My only other experience is PHP, and I never used a framework before, so I'm finding this very intimidating and confusing.
I'm interested in learning CherryPy/Jinja2 to make a ZFS Monitor for my NAS. I've read through the basics of the docs on CherryPy/Jinja2 but I find that the samples are disjointed and too simplistic, I don't really understand how to make these 2 things "come together" gracefully.
Some questions I have:
Is there a simple tutorial shows how you make CherryPy and Jinja2 work together nicely? I'm either finding samples that are too simple, like the samples on CherryPy / Jinja2 docs, or way to complex. (example: https://github.com/jovanbrakus/cherrypy-example).
Is there a standardized or "expected" way to create web applications for CherryPy? (example: What should my directory structure look like? Is there a way to declare static things; is it even necessary?)
Does anyone have recommended literature for this or is the online documentation the best resource?
Congratulations on choosing Python, I'm sure you'll learn to love it as have I.
Regarding CherryPy, I'm not an expert, but was also in the same boat as you a few days ago and I'd agree that the tutorials are a little disjointed in parts.
For integrating Jinja2, as in their doc page, the snippet of HTML should have been specified that it is the template file and as such saved in the path /templates/index.html. They also used variables that didn't match up in the template code sample and controller sample.
The below is instead a complete working sample of a simple hello world using CherryPy and Jinja2
/main.py:
/templates/index.html:
Then in your shell/command prompt, serve the app using:
And in your browser you should be able to see it at
http://localhost:8080
That hopefully helps you to connect Jinja2 templating to your CherryPy app. CherryPy really is a lightweight and very flexible framework, where you can choose many different ways to structure your code and file structures.
Application structure
First about standard directory structure of a project. There is none, as CherryPy doesn't mandate it, neither it tells you what data layer, form validation or template engine to use. It's all up to you and your requirements. And of course as this is a great flexibility as it causes some confusion to beginners. Here's how a close to real-word application directory structure may look like.
Then standing in the root of virtual environment you usually do the following to start CherryPy in development environment.
cherryd
is CherryPy's suggest way of running an application.Templating
Now let's look closer to the template directory and what it can look like.
To harness nice Jinja2's feature of template inheritance, here are layouts which define structure of a page, the slots that can be filled in a particular page. You may have layout for a website and layout for email notifications. There's also a directory for a part, reusable snippet used across different pages. Now lets see the code that corresponds the structure above.
I've made the following also available as a runnable which is easier to navigate files, you can run and play with it. The paths start with
.
like in the first section's tree.website/config.py
website/serve.py
website/application/__init__.py
Notable part here is a CherryPy tool which helps to avoid boilerplate related with rendering templates. You just need return a
dict
from CherryPy page handler with data for the template. Following convention-over-configuration principle, the tool when not provided with template name will useclassname/methodname.html
e.g.user/profile.html
. To override the default template you can use@cherrypy.tools.template(name = 'other/name')
. Also note that the tool exposes a method automatically, so you don't need to append@cherrypy.expose
on topwebsite/application/controller.py
As you can see with use of the tool page handlers look rather clean and will be consistent with other tools, e.g.
json_out
.In this demo app I used blueprint css file, to demonstrate how static resource handling works. Put it in
website/application/public/resource/css/blueprint.css
. The rest is less interesting, just Jinja2 templates for completeness.website/application/view/layout/main.html
website/application/view/page/index/index.html
website/application/view/page/news/list.html
website/application/view/page/news/show.html
website/application/view/page/user/profile.html
website/application/view/page/error.html
It's a 404-page.
website/application/view/part/menu.html
References
Code above goes closely with backend section of qooxdoo-website-skeleton. For full-blown Debain deployment of such application, cherrypy-webapp-skeleton may be useful.