By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,240 Members | 791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,240 IT Pros & Developers. It's quick & easy.

How to do basic CRUD apps with Python

P: n/a
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.

May 13 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a

On May 13, 2007, at 6:20 PM, walterbyrd wrote:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.
You got answers on django-users! The best IMHO was the one from
dballanc: "Django is good for providing the backend, but most of your
functionality is probably going to be provided by ajax/javascript
which django will happily communicate with, but does not provide."

Django provides an ORM, but you don't have to use it. If you want,
you can connect directly to your database just like you did with
php. I've actually done that because something just "feels wrong"
about using an ORM for CRUDy applications.

May 14 '07 #2

P: n/a

walterbyrd a scris:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.
Try SqlSoup from SqlAlchemy. I can send examples in PyQt4.

May 14 '07 #3

P: n/a

walterbyrd a scris:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.
SqlAlchemy-SqlSoup Example:
# SqlSoup. CRUD with one table
from sqlalchemy.ext.sqlsoup import SqlSoup
# connection: 'postgres://user:password@address:port/db_name'
db = SqlSoup('postgres://postgres:postgres@localhost:5432/testdb')

# read data
person = db.person.select()
print person

# index is not the same with primary key !!!
print person[0].firstname

# write in column firstname
person[0].firstname = "George"

# effective write
db.flush()

print person[0]

print db.person.count()

for i in range(0, db.person.count()):
print person[i]

db.person.insert(id=1000, firstname='Mitu')
db.flush

# after insert, reload mapping:
person = db.person.select()

# delete:
# record select
mk = db.person.selectone_by(id=1000)
# delete
db.delete(mk)
db.flush()

person = db.person.select()

print person
"""
FROM DOCUMENTATION:

=======
SqlSoup
=======

Introduction

SqlSoup provides a convenient way to access database tables without
having to
declare table or mapper classes ahead of time.

Suppose we have a database with users, books, and loans tables
(corresponding to
the PyWebOff dataset, if you're curious).
For testing purposes, we'll create this db as follows:
>>from sqlalchemy import create_engine
e = create_engine('sqlite:///:memory:')
for sql in _testsql: e.execute(sql) #doctest: +ELLIPSIS
<...

Creating a SqlSoup gateway is just like creating an SqlAlchemy engine:
>>from sqlalchemy.ext.sqlsoup import SqlSoup
db = SqlSoup('sqlite:///:memory:')
or, you can re-use an existing metadata:
>>db = SqlSoup(BoundMetaData(e))
You can optionally specify a schema within the database for your
SqlSoup:

# >>db.schema = myschemaname

Loading objects

Loading objects is as easy as this:
>>users = db.users.select()
users.sort()
users
[MappedUsers(name='Joe Student',em************@example.edu',
password='student',classname=None,admin=0),
MappedUsers(name='Bhargan Basepair',em*************@example.edu',
password='basepair',classname=None,admin=1)]

Of course, letting the database do the sort is better
(".c" is short for ".columns"):
>>db.users.select(order_by=[db.users.c.name])
[MappedUsers(name='Bhargan Basepair',em*************@example.edu',
password='basepair',classname=None,admin=1),
MappedUsers(name='Joe Student',em************@example.edu',
password='student',classname=None,admin=0)]

Field access is intuitive:
>>users[0].email
u'*******@example.edu'

Of course, you don't want to load all users very often.
Let's add a WHERE clause.
Let's also switch the order_by to DESC while we're at it.
>>from sqlalchemy import or_, and_, desc
where = or_(db.users.c.name=='Bhargan Basepair',
db************************@example.edu')
>>db.users.select(where, order_by=[desc(db.users.c.name)])
[MappedUsers(name='Joe Student',em************@example.edu',
password='student',classname=None,admin=0),
MappedUsers(name='Bhargan Basepair',em*************@example.edu',
password='basepair',classname=None,admin=1)]

You can also use the select...by methods if you're querying on a
single column.
This allows using keyword arguments as column names:
>>db.users.selectone_by(name='Bhargan Basepair')
MappedUsers(name='Bhargan Basepair',em*************@example.edu',
password='basepair',classname=None,admin=1)

Select variants

All the SqlAlchemy Query select variants are available.
Here's a quick summary of these methods:

* get(PK): load a single object identified by its primary key
(either a scalar, or a tuple)
* select(Clause, **kwargs): perform a select restricted by the
Clause
argument; returns a list of objects.
The most common clause argument takes the form
"db.tablename.c.columname == value."
The most common optional argument is order_by.
* select_by(**params): select methods ending with _by allow using
bare
column names. (columname=value) This feels more natural to
most Python
programmers; the downside is you can't specify order_by or
other
select options.
* selectfirst, selectfirst_by: returns only the first object
found;
equivalent to select(...)[0] or select_by(...)[0], except None
is returned
if no rows are selected.
* selectone, selectone_by: like selectfirst or selectfirst_by, but
raises
if less or more than one object is selected.
* count, count_by: returns an integer count of the rows selected.

See the SqlAlchemy documentation for details:

* http://www.sqlalchemy.org/docs/datam...amapping_query
for general info and examples,
* http://www.sqlalchemy.org/docs/sqlconstruction.myt
for details on constructing WHERE clauses.

Modifying objects

Modifying objects is intuitive:
>>user = _
user.email = 'b**************@example.edu'
db.flush()
(SqlSoup leverages the sophisticated SqlAlchemy unit-of-work code, so
multiple
updates to a single object will be turned into a single UPDATE
statement
when you flush.)

To finish covering the basics, let's insert a new loan, then delete
it:
>>book_id = db.books.selectfirst(db.books.c.title=='Regional Variation in Moss').id
db.loans.insert(book_id=book_id, user_name=user.name)
MappedLoans(book_id=2,user_name='Bhargan Basepair',loan_date=None)
>>db.flush()
>>loan = db.loans.selectone_by(book_id=2, user_name='Bhargan
Basepair')
>>db.delete(loan)
db.flush()
You can also delete rows that have not been loaded as objects.
Let's do our insert/delete cycle once more, this time using the loans
table's
delete method. (For SQLAlchemy experts: note that no flush() call is
required
since this delete acts at the SQL level, not at the Mapper level.)
The same where-clause construction rules apply here as to the select
methods.
>>db.loans.insert(book_id=book_id, user_name=user.name)
MappedLoans(book_id=2,user_name='Bhargan Basepair',loan_date=None)
>>db.flush()
db.loans.delete(db.loans.c.book_id==2)
You can similarly update multiple rows at once.
This will change the book_id to 1 in all loans whose book_id is 2:
>>db.loans.update(db.loans.c.book_id==2, book_id=1)
db.loans.select_by(db.loans.c.book_id==1)
[MappedLoans(book_id=1,user_name='Joe
Student',loan_date=datetime.datetime(2006,
7,
12, 0, 0))]

Joins

Occasionally, you will want to pull out a lot of data from related
tables all
at once. In this situation, it is far more efficient to have the
database
perform the necessary join. (Here we do not have "a lot of data," but
hopefully
the concept is still clear.) SQLAlchemy is smart enough to recognize
that loans
has a foreign key to users, and uses that as the join condition
automatically.
>>join1 = db.join(db.users, db.loans, isouter=True)
join1.select_by(name='Joe Student')
[MappedJoin(name='Joe Student',em************@example.edu',
password='student',classname=None,admin=0,book_id= 1,
user_name='Joe Student',loan_date=datetime.datetime(2006, 7,
12, 0, 0))]

If you're unfortunate enough to be using MySQL with the default MyISAM
storage
engine, you'll have to specify the join condition manually, since
MyISAM does
not store foreign keys.
Here's the same join again, with the join condition explicitly
specified:
>>db.join(db.users, db.loans, db.users.c.name==db.loans.c.user_name, isouter=True)
<class 'sqlalchemy.ext.sqlsoup.MappedJoin'>

You can compose arbitrarily complex joins by combining Join objects
with tables
or other joins. Here we combine our first join with the books table:
>>join2 = db.join(join1, db.books)
join2.select()
[MappedJoin(name='Joe Student',em************@example.edu',
password='student',classname=None,admin=0,book_id= 1,
user_name='Joe Student',loan_date=datetime.datetime(2006, 7, 12,
0, 0),
id=1,title='Mustards I Have
Known',published_year='1989',authors='Jones')]

If you join tables that have an identical column name, wrap your join
with
"with_labels", to disambiguate columns with their table name:
>>db.with_labels(join1).select()
[MappedUsersLoansJoin(users_name='Joe Student',
users_em************@example.edu',users_password=' student',
users_classname=None,users_admin=0,loans_book_id=1 ,
loans_user_name='Joe Student',
loans_loan_date=datetime.datetime(2006, 7, 12, 0, 0))]

Advanced Use
Mapping arbitrary Selectables

SqlSoup can map any SQLAlchemy Selectable with the map method.
Let's map a Select object that uses an aggregate function; we'll use
the
SQLAlchemy Table that SqlSoup introspected as the basis.
(Since we're not mapping to a simple table or join, we need to tell
SQLAlchemy
how to find the "primary key," which just needs to be unique within
the select,
and not necessarily correspond to a "real" PK in the database.)
>>from sqlalchemy import select, func
b = db.books._table
s = select([b.c.published_year, func.count('*').label('n')],
from_obj=[b], group_by=[b.c.published_year])
>>s = s.alias('years_with_count')
years_with_count = db.map(s, primary_key=[s.c.published_year])
years_with_count.select_by(published_year='1989' )
[MappedBooks(published_year='1989',n=1)]

Obviously if we just wanted to get a list of counts associated with
book years
once, raw SQL is going to be less work. The advantage of mapping a
Select is
reusability, both standalone and in Joins. (And if you go to full
SQLAlchemy,
you can perform mappings like this directly to your object models.)

Raw SQL

You can access the SqlSoup's engine attribute to compose SQL
directly.
The engine's execute method corresponds to the one of a DBAPI cursor,
and returns a ResultProxy that has fetch methods you would also see on
a cursor.
>>rp = db.engine.execute('select name, email from users order by
name')
>>for name, email in rp.fetchall(): print name, email
Bhargan Basepair ba*************@example.edu
Joe Student st*****@example.edu

You can also pass this engine object to other SQLAlchemy constructs.
"""

May 14 '07 #4

P: n/a

walterbyrd a scris:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.
SqlAlchemy - SqlSoup - PyQt4 (fragment) example:

import sys
from PyQt4.Qt import *
from PyQt4 import uic
from avc.avcqt4 import *
from sqlalchemy.ext.sqlsoup import SqlSoup

from ui_db import Ui_DbForm

class DbForm(QWidget,AVC):
def __init__(self, parent=None):
QWidget.__init__(self,parent)

self.ui = Ui_DbForm()
self.ui.setupUi(self)

# current index
self.crtIndex = 0
self.crtPk = 0

# avc variables, same names as form widgets (lineEdit, combo,
etc....)
self.edtId = 0
self.edtFirstName = ""
self.edtLastName = ""
self.edtSalary = 0.0

self.connect(self.ui.btnQuit, SIGNAL("clicked()"),
qApp, SLOT("quit()"))

self.connect(self.ui.btnFirst, SIGNAL("clicked()"),
self.goFirst)
self.connect(self.ui.btnPrior, SIGNAL("clicked()"),
self.goPrior)
self.connect(self.ui.btnNext, SIGNAL("clicked()"),
self.goNext)
self.connect(self.ui.btnLast, SIGNAL("clicked()"),
self.goLast)

self.connect(self.ui.btnSave, SIGNAL("clicked()"),
self.doSave)
self.connect(self.ui.btnAdd, SIGNAL("clicked()"), self.doAdd)
self.connect(self.ui.btnDel, SIGNAL("clicked()"),
self.doDel)

# connection: 'postgres://user:password@address:port/db_name'
self.db = SqlSoup('postgres://postgres:postgres@localhost:5432/
testdb')

self.goFirst()

def goFirst(self):
self.crtIndex = 0
self.doRead(self.crtIndex)

def goPrior(self):
if self.crtIndex 0:
self.crtIndex = self.crtIndex - 1
else:
self.crtIndex = 0
self.doRead(self.crtIndex)

def goNext(self):
maxIndex = self.db.person.count() - 1
if self.crtIndex < maxIndex:
self.crtIndex = self.crtIndex + 1
else:
self.crtIndex = maxIndex
self.doRead(self.crtIndex)

def goLast(self):
maxIndex = self.db.person.count() - 1
self.crtIndex = maxIndex
self.doRead(self.crtIndex)

def doSave(self):
if self.crtPk == 0:
# aflu pk-ul si adaug o inregistrare goala
newPk = self.db.engine.execute("select
nextval('person_id_seq')").fetchone()[0]
self.crtPk = newPk
self.db.person.insert(id=self.crtPk, firstname='',
lastname='', salary=0.0)
self.db.flush()
person = self.db.person.selectone_by(id=self.crtPk)
person.firstname = self.edtFirstName
person.lastname = self.edtLastName
person.salary = self.edtSalary
self.db.flush()

def doAdd(self):
self.crtPk = 0
self.edtId = self.crtPk
self.edtFirstName = ""
self.edtLastName = ""
self.edtSalary = 0.0
# inregistrarea trebuie salvata explicit
# prin apasarea butonului "Save"

def doDel(self):
mk = self.db.person.selectone_by(id=self.crtPk)
self.db.delete(mk)
self.db.flush()
self.goNext()

def doRead(self, index):
person = self.db.person.select()
self.edtId = person[index].id
self.edtFirstName = person[index].firstname
self.edtLastName = person[index].lastname
self.edtSalary = person[index].salary
# invariant pt. toate operatiile, mai putin adaugare
inregistrare
# pk-ul nu se poate modifica prin edtId !!!
self.crtPk = person[index].id
if __name__ == "__main__":
app = QApplication(sys.argv)
# QApplication.setStyle(QStyleFactory.create("Cleanl ooks"))
QApplication.setStyle(QStyleFactory.create("Plasti que"))
form = DbForm()
form.avc_init()
form.show()
sys.exit(app.exec_())

May 14 '07 #5

P: n/a
walterbyrd a écrit :
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?
You may want to have a look at turbogears's widgets.
May 14 '07 #6

P: n/a
Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ote:
walterbyrd a ?crit :
>With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.
>With Python, finding such library, or apps. seems to be much more
difficult to find.
>I thought django might be a good way, but I can not seem to get an
answer on that board.
>I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.
>Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?
You may want to have a look at turbogears's widgets.
Admittedly I had to look up the meaning of CRUD in this context:
(http://en.wikipedia.org/wiki/Create%...ate_and_delete
create, read, update, and delete).

I'm looking at Turbogears' Widgets in another window as I type
this ... but it will be awhile before I can comment on how they
might apply to the OP's needs. Actually I'm wholly unqualified
to comment on his or her needs ... but I can comment on how I
interpreted the question.

Even with the SQLAlchemy SQLSoup examples there's still an
annoying about of boilerplate coding that has to be done in order
to create a web application for doing CRUD on a database.

The missing element seems to be the ability to autogenerate
web forms and reports with the requisite controls in them.

Imagine, for a moment, being able to do something like:
>>import sqlalchemy.ext.webcrud as crud
db = crud.open(....)
db.displayTopForm()
'<HTML ....
....
</HTML>'

... and having a default "top level" web page generated with
options to query the database (or some specific table in the
database to be more specific, add new entries, etc).

I'm thinking of some sort of class/module that would generate
various sorts of HTML forms by default, but also allow one to
sub-class each of the form/query types to customize the contents.

It would use introspection on the database columns and constraints
to automatically generate input fields for each of the columns and
even fields for required foreign keys (or links to the CRUD for those
tables?). Ideally it would also automatically hide autogenerated
(index/key) fields, and map the table column IDs to form names (with
gettext support for l10n of those).

I think that's the sort of thing the OP was looking for. Not the
ORM ... the the glue between the web framework and the ORM.
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

May 15 '07 #7

P: n/a
On Sunday 13 May 2007 15:20, walterbyrd wrote:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don't advertise your PHP/Ajax apps.
Turbogears has catwalk, which is already an interface to a database, but
there is a CRUD template for TG:
http://docs.turbogears.org/1.0/CRUDTemplate

Might be what you're looking for.

j

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #8

P: n/a
On Monday 14 May 2007 18:46, James T. Dennis wrote:
I'm thinking of some sort of class/module that would generate
various sorts of HTML forms by default, but also allow one to
sub-class each of the form/query types to customize the contents.
Turbogears has catwalk, which is already an interface to a database, but
there is a CRUD template for TG:
http://docs.turbogears.org/1.0/CRUDTemplate

Might be what you're looking for.

j

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #9

P: n/a
Sorry about the duplicate post! My news reader never showed my first reply!

j

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #10

P: n/a
On May 14, 7:46 pm, "James T. Dennis" <jades...@idiom.comwrote:
Bruno Desthuilliers <bruno.42.desthuilli...@wtf.websiteburo.oops.comwr ote:
walterbyrd a ?crit :
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.
With Python, finding such library, or apps. seems to be much more
difficult to find.
I thought django might be a good way, but I can not seem to get an
answer on that board.
I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it's not entirely necessary.
Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?
You may want to have a look at turbogears's widgets.

Admittedly I had to look up the meaning of CRUD in this context:
(http://en.wikipedia.org/wiki/Create%...ate_and_delete
create, read, update, and delete).

I'm looking at Turbogears' Widgets in another window as I type
this ... but it will be awhile before I can comment on how they
might apply to the OP's needs. Actually I'm wholly unqualified
to comment on his or her needs ... but I can comment on how I
interpreted the question.

Even with the SQLAlchemy SQLSoup examples there's still an
annoying about of boilerplate coding that has to be done in order
to create a web application for doing CRUD on a database.

The missing element seems to be the ability to autogenerate
web forms and reports with the requisite controls in them.

Imagine, for a moment, being able to do something like:
>>import sqlalchemy.ext.webcrud as crud
>>db = crud.open(....)
>>db.displayTopForm()
'<HTML ....
....
</HTML>'

... and having a default "top level" web page generated with
options to query the database (or some specific table in the
database to be more specific, add new entries, etc).

I'm thinking of some sort of class/module that would generate
various sorts of HTML forms by default, but also allow one to
sub-class each of the form/query types to customize the contents.

It would use introspection on the database columns and constraints
to automatically generate input fields for each of the columns and
even fields for required foreign keys (or links to the CRUD for those
tables?). Ideally it would also automatically hide autogenerated
(index/key) fields, and map the table column IDs to form names (with
gettext support for l10n of those).

I think that's the sort of thing the OP was looking for. Not the
ORM ... the the glue between the web framework and the ORM.

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
Sounds like you're talking about rails. Do any of the python packages
compare with the ease of rails? I got in just deep enough to see the
possibilities of it, and then had to stop to do real work in php. I'd
be very interested in Python on Rails!

~Sean

May 17 '07 #11

P: n/a
ha**********@gmail.com a écrit :
(snip)
>
Sounds like you're talking about rails. Do any of the python packages
compare with the ease of rails?
Turbogears, Django and Pylons.
May 20 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.