Formatting output

23 views
Skip to first unread message

jason

unread,
Feb 16, 2011, 11:57:52 PM2/16/11
to PyDbLite
Hi,

How do I go about formatting a record such as this one:

[{'City': 'Berlin', 'Fax': '030-0076545', 'CompanyName': 'Alfreds
Futterkiste', 'Country': 'Germany', 'Address': 'Obere Str. 57',
'Phone': '030-0074321', '__id__': 0, 'Postal Code': 12209,
'__version__': 0, 'CustomerID': 'ALFKI'}]

into an HTML table where the keys are the table headers and the values
are the rows. Do I use standard Python list functions and if so, which
ones? Or do I use the functions included with PyDbLite, and if so,
which ones? I'm using the Karrigell framework to generate the output
on the server side. I would greatly appreciate any assistance you can
provide me!

Jason

Jim Eggleston

unread,
Feb 17, 2011, 8:19:16 PM2/17/11
to PyDbLite
Hi Jason,

Here are some pointers and some untested sample code. This is
Karrigell specific.

- Read the section on HTMLTags in the Karrigell reference
- Note how you can use the sum function to concatenate HTMLTag objects
- Have a look at the Karrigell demo apps and look at the code of any
app that produces tables

With HTMLTags you should be able to do something like this (completely
untested.) This will produce a table with table headings and with one
line per record.

keys = ['CustomerID','CompanyName','Address','City','Postal
Code','Country','Phone','Fax']
lines = TR(sum([TH(k) for k in keys]))
for record in records:
line += TR(sum([TD(record[k]) for k in keys]))
print TABLE(lines)

If you want format just one record, you could do something like this:

keys = ['CustomerID','CompanyName','Address','City','Postal
Code','Country','Phone','Fax']
print TABLE(sum([TR(TD(k) + TD(record[0][k]) for k in keys]))

In either case you can add, remove or change the order of the fields
simply by editing the keys list.

I am a bit rusty, so some things may not be quite right. None the less
it will give you something to start with.

Cheers,
Jim

Pierre Quentel

unread,
Feb 18, 2011, 7:54:39 AM2/18/11
to PyDbLite
> > Jason- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

Hi,

PyDbLite, Karrigell and HTMLTags are all excellent choices ;-)

Not much to add to Jim's answer, except that you can get the field
names by db.fields where db is the PyDbLite.Base instance

Example :

from HTMLTags import *
import PyDbLite

def index():
db = PyDbLite.Base('programmes.pdl').open()
table = TABLE()
table <= TR(Sum(TH(f) for f in db.fields))
for row in db:
table <= TR(Sum(TD(row[f]) for f in db.fields))
print table

- Pierre

Jason Castellucci

unread,
Feb 19, 2011, 6:15:53 PM2/19/11
to pydb...@googlegroups.com
Thank you so much, Pierre and Jim, for your help and being so patient
with newbies like me:-) . I'm especially pleased to see that your
examples are Karrigell-specific! After my project is completed, I'll
share it with this group.
Reply all
Reply to author
Forward
0 new messages