Suggestions for structure of HTML-generating app

L

lanwrangler

Hi,

This isn't a strictly Python question but I wonder if someone could
give me some clues here. I've been writing a number of stand-alone
apps that use CherryPy as an embedded web server for displaying
processed data and interacting with the application. To go along with
this I've also been using CherryTemplate as a handy HTML template
generator.

My question is this - what's the best way to separate the application
code from the UI generation code from the "raw" HTML? To give you an
idea what I mean, the core application code is fairly straightforward
and writes to SQLite databases and/or dictionaries. That's the easy
bit.

The data then often needs a fair amount of massaging to make it
suitable for display. For example, one app I've got monitors network
utilisation so writes bits-per-second values to SQLite. The UI then
takes those values and generates bar charts (done by cropping an image
to size before putting it into a table cell), changes text colour if
utilisation is >90% and so on etc.

I've done this with lots of Python code embedded in the CherryTemplate
pages but that becomes a real maintenance headache as the templates
become huge and the code and HTML is scattered around with no clear
distinction between the two. I've also tried using pure-Python HTML
generation functions that output, say, entire tables made up from the
source data that are then called by the template, but then you end up
with Python functions with lots of HTML embedded in them which, again,
ends up being difficult to keep track of.

Are there any good approaches of doing this kind of thing that I've
missed, or am I resigned to having HTML and Python code mixed and so
will just have to keep all that nastiness to as few modules as
possible?

Thanks,
Matthew.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Hi,

This isn't a strictly Python question but I wonder if someone could
give me some clues here. I've been writing a number of stand-alone
apps that use CherryPy as an embedded web server for displaying
processed data and interacting with the application. To go along with
this I've also been using CherryTemplate as a handy HTML template
generator.

My question is this - what's the best way to separate the application
code from the UI generation code from the "raw" HTML? To give you an
idea what I mean, the core application code is fairly straightforward
and writes to SQLite databases and/or dictionaries. That's the easy
bit.

The data then often needs a fair amount of massaging to make it
suitable for display. For example, one app I've got monitors network
utilisation so writes bits-per-second values to SQLite. The UI then
takes those values and generates bar charts (done by cropping an image
to size before putting it into a table cell), changes text colour if
utilisation is >90% and so on etc.

I've done this with lots of Python code embedded in the CherryTemplate
pages but that becomes a real maintenance headache as the templates
become huge and the code and HTML is scattered around with no clear
distinction between the two. I've also tried using pure-Python HTML
generation functions that output, say, entire tables made up from the
source data that are then called by the template, but then you end up
with Python functions with lots of HTML embedded in them which, again,
ends up being difficult to keep track of.

Are there any good approaches of doing this kind of thing that I've
missed, or am I resigned to having HTML and Python code mixed and so
will just have to keep all that nastiness to as few modules as
possible?

The answer I can see is so terribly obvious that I surely have missed
something, but anyway: when the internal structure of the data is not
appropriate for the view, I usually use a helper function that takes the
raw data and format it the way the view expect it. Depending on the web
framework and whatnot, this can be either a special controller, or an
helper function called by the template with the data passed in by the
controller.
 
B

Bernard

Hi,

This isn't a strictly Python question but I wonder if someone could
give me some clues here. I've been writing a number of stand-alone
apps that use CherryPy as an embedded web server for displaying
processed data and interacting with the application. To go along with
this I've also been using CherryTemplate as a handy HTML template
generator.

My question is this - what's the best way to separate the application
code from the UI generation code from the "raw" HTML? To give you an
idea what I mean, the core application code is fairly straightforward
and writes to SQLite databases and/or dictionaries. That's the easy
bit.

The data then often needs a fair amount of massaging to make it
suitable for display. For example, one app I've got monitors network
utilisation so writes bits-per-second values to SQLite. The UI then
takes those values and generates bar charts (done by cropping an image
to size before putting it into a table cell), changes text colour if
utilisation is >90% and so on etc.

I've done this with lots of Python code embedded in the CherryTemplate
pages but that becomes a real maintenance headache as the templates
become huge and the code and HTML is scattered around with no clear
distinction between the two. I've also tried using pure-Python HTML
generation functions that output, say, entire tables made up from the
source data that are then called by the template, but then you end up
with Python functions with lots of HTML embedded in them which, again,
ends up being difficult to keep track of.

Are there any good approaches of doing this kind of thing that I've
missed, or am I resigned to having HTML and Python code mixed and so
will just have to keep all that nastiness to as few modules as
possible?

Thanks,
Matthew.

we use Cheetah templates to do just that at my workplace.
we use CherryPy to generate the data and then we pass it on
to the Cheetah template which handles the data and add html over it.
here's an article on that matter : http://www.onlamp.com/pub/a/python/2005/01/13/cheetah.html
 
L

lanwrangler

On 5 fév, 10:09, "(e-mail address removed)" <[email protected]>
wrote:


we use Cheetah templates to do just that at my workplace.
we use CherryPy to generate the data and then we pass it on
to the Cheetah template which handles the data and add html over it.
here's an article on that matter :http://www.onlamp.com/pub/a/python/2005/01/13/cheetah.html

Thanks for the pointer to Cheetah. At first glance it looks neat.
Cherrytemplate has done well for me so far but I don't think it's
under
active development any more so it may be time to migrate.

Matthew.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top