How to do basic CRUD apps with Python

W

walterbyrd

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.
 
M

Michael Bentley

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.
 
G

g_teodorescu

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.
 
G

g_teodorescu

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

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:
<...

Creating a SqlSoup gateway is just like creating an SqlAlchemy engine:

or, you can re-use an existing metadata:

You can optionally specify a schema within the database for your
SqlSoup:

# >>> db.schema = myschemaname

Loading objects

Loading objects is as easy as this:
[MappedUsers(name='Joe Student',email='(e-mail address removed)',
password='student',classname=None,admin=0),
MappedUsers(name='Bhargan Basepair',email='(e-mail address removed)',
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',email='(e-mail address removed)',
password='basepair',classname=None,admin=1),
MappedUsers(name='Joe Student',email='(e-mail address removed)',
password='student',classname=None,admin=0)]

Field access is intuitive:
u'(e-mail address removed)'

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.users.c.email=='(e-mail address removed)')
>>> db.users.select(where, order_by=[desc(db.users.c.name)])
[MappedUsers(name='Joe Student',email='(e-mail address removed)',
password='student',classname=None,admin=0),
MappedUsers(name='Bhargan Basepair',email='(e-mail address removed)',
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:
MappedUsers(name='Bhargan Basepair',email='(e-mail address removed)',
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/datamapping.myt#datamapping_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:

(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:

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.

You can similarly update multiple rows at once.
This will change the book_id to 1 in all loans whose book_id is 2:
[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.
[MappedJoin(name='Joe Student',email='(e-mail address removed)',
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:
<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:
[MappedJoin(name='Joe Student',email='(e-mail address removed)',
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:
[MappedUsersLoansJoin(users_name='Joe Student',
users_email='(e-mail address removed)',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=, 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.
Bhargan Basepair (e-mail address removed)
Joe Student (e-mail address removed)

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

g_teodorescu

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("Cleanlooks"))
QApplication.setStyle(QStyleFactory.create("Plastique"))
form = DbForm()
form.avc_init()
form.show()
sys.exit(app.exec_())
 
B

Bruno Desthuilliers

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.
 
J

Joshua J. Kugler

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
 
J

James T. Dennis

Bruno Desthuilliers said:
walterbyrd a ?crit :
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,_read,_update_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.
 
J

Joshua J. Kugler

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
 
H

half.italian

Admittedly I had to look up the meaning of CRUD in this context:
(http://en.wikipedia.org/wiki/Create,_read,_update_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:

'<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.

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top