Data Output Python

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Data Output Python

Post by maboroshi »

Hello

I am trying to find a method to output some data the best way possible.

currently I have this method.

Code: Select all

def build(item):
    if isinstance(item, list):
        #UL(*[LI(build(x)) for x in item])
        return TABLE(*[TR(build(x)) for x in item])
    else:
        return item
Which outputs an HTML table and Row for every item. The data is a list of items. What I would like is for it to cycle through the first row of the list before creating a new instance of the items so it will go

Code: Select all

Table
TR Row 1
TD Item1 TD Item2 TD item3
/TR
TR Row 2
TD Item1 TD Item2 D Item3
/TABLE
This is the data I am cycling through

Code: Select all

google_browser_connection_info = account.get_data(start_date=start_date, end_date=end_date, 
        dimensions=['browser', 'browserVersion', 'flashVersion', 'javaEnabled',
            'operatingSystem', 'operatingSystemVersion',],
             metrics=['pageviews',])
    google_browser_connection_info = google_browser_connection_info.list
Sorry if this is a bit to vague

Any ideas

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

I would use templates instead of a HTML generation library, in the spirit of not mixing code and presentation. I like Jinja2.

Here's what it would look like if I understand the list structure correctly (you didn't give an example of an actual list):

Code: Select all

render_template("the_one_below.html", google_browser_connection_info=google_browser_connection_info)

Code: Select all

<table>
{% for item in google_browser_connection_info %}
{% if loop.index is divisibleby 3 %}<tr>{% endif %}
<td>{{ item }}</td>
{% if loop.index is divisibleby 3 %}</tr>{% endif %}
{% endfor %}
</table>

Post Reply