Tips for using Eclipse with web2py

87 views
Skip to first unread message

michaelangela

unread,
Feb 20, 2008, 12:02:08 AM2/20/08
to web2py Web Framework
I have been using PyDev with Eclipse and Django. I am trying it out
with web2py. I am going through the cookbook example just to see how
it feels in Eclipse.

Pretty nice actually.

The only notable things so far are:
- make a new project, unchecking the default, and choosing the root
folder as the location (where web2py.py is located)
- add __init__.py modules where you want to be able to import things
from your own code, i.e. controllers, models, etc.
- add the web2py root folder as a project source folder for the
python path. On the mac version I am using this is the Resources
folder, again where the web2py.py file is located
- download the web2py source and add the web2py source root as an
external source folder

With this you can do (and get auto completion!) for things like

from applications.cookbook.models.db import db

and also

from gluon.sql import SQLDB
from gluon.sql import SQLField
from gluon.validators import IS_NOT_EMPTY
from gluon.validators import IS_NOT_IN_DB
from gluon.validators import IS_IN_DB
from gluon.validators import IS_DATE

are added automatically as you are going through code

One gotcha is this line:

return dict(records=SQLTABLE(records))

There are two SQLTables, but with different case. Since this is being
used to output html, the following import made the most sense, and
worked:

from gluon.sqlhtml import SQLTABLE

Good stuff. I really like the editor built in to the web version, but
code hints and completion in Eclipse is very nice.

Are there any other tips out there for coding with regular text
editors?

Massimo Di Pierro

unread,
Feb 20, 2008, 12:07:49 AM2/20/08
to web...@googlegroups.com
would you write a wiki on alterego about this, or simply post this
email?

voltron

unread,
Feb 20, 2008, 2:37:04 AM2/20/08
to web2py Web Framework
Yes Eclipse is very nice. I use EasyEclipse for almost everything,
even the templates. I additionally installed the Aptana editor plugin
for HTML Javascript and CSS editing. Nice to see another approach. I
usually create a new project, and use the "import" dialog window to
import a copy of the Web2py source tree. I want to try to experiment
with creating a new project based on what SVN pulls from the web2py
repository

Michael Wills

unread,
Feb 20, 2008, 11:37:08 AM2/20/08
to web...@googlegroups.com
It is now posted up on AlterEgo. :-)

EasyEclipse.... very nice. I installed many of those components manually. Thanks for the tip! And it looks like they have a plugin only version as well. Very nice.

Massimo Di Pierro

unread,
Feb 20, 2008, 11:52:28 AM2/20/08
to web...@googlegroups.com
Can you email me a screenshot and I will post it on the wiki?

Massimo

Michael Wills

unread,
Feb 20, 2008, 5:48:26 PM2/20/08
to web...@googlegroups.com
Unfortunately I have run into a few problems with code completion, etc. It's fine for most things, especially seeing the available arguments on things like SQLTABLE, but the main thing I have been thinking of is referencing models. I would love to be able to have code completion for things like that.

For example, in the cookbook example, wherever I want all the recipes ordered by title I do

db().select(db.recipe.ALL,orderby=db.recipe.title)

But if that could be made into a helper function in the db.py file so that I could just call db.recipes, that would be great. Perhaps that could help with coding consistency as well? Of course I am taking this idea from Django's model manager. :-)

Michael

Massimo Di Pierro

unread,
Feb 20, 2008, 6:15:47 PM2/20/08
to web...@googlegroups.com
Not sure what you are asking. You can define in the model:

def myrecipes():
    return db().select(db.recipe.ALL,orderby=db.recipe.title)

Massimo

Michael Wills

unread,
Feb 20, 2008, 7:23:48 PM2/20/08
to web...@googlegroups.com
Ah I can't import anything from the models to get those. I can see them in code completion which is what I want but then the import statement itself causes it to fail with

ImportError: cannot import name myrecipes

Kind of odd... Without the import it works fine. it might be that I have a wrong path in the PYTHONPATH.

Michael Wills

unread,
Feb 20, 2008, 7:23:52 PM2/20/08
to web...@googlegroups.com
Ah I can't import anything from the models to get those. I can see them in code completion which is what I want but then the import statement itself causes it to fail with

ImportError: cannot import name myrecipes

Kind of odd... Without the import it works fine. it might be that I have a wrong path in the PYTHONPATH.

Massimo Di Pierro

unread,
Feb 20, 2008, 7:40:06 PM2/20/08
to web...@googlegroups.com
You do not need to import them. Anything defined in model is already visible in controllers and views.

Or are you talking about importing in Eclipse?

Massimo

Michael Wills

unread,
Feb 20, 2008, 7:51:51 PM2/20/08
to web...@googlegroups.com
Eclipse with PyDev does the import. Looking at some PyDev FAQs now...

Michael Wills

unread,
Feb 20, 2008, 8:36:22 PM2/20/08
to web...@googlegroups.com
Turning off auto-imports seems to do the trick.

Preferences > Pydev > PyDev Extensions > Auto Imports, uncheck "Do auto imports?"

Also without the imports PyDev doesn't give code hinting. I have updated AlterEgo with the notes though.

Massimo Di Pierro

unread,
Feb 21, 2008, 12:42:29 AM2/21/08
to web...@googlegroups.com
Please add this to the wiki.

Massimo

Michael Wills

unread,
Feb 21, 2008, 6:15:30 PM2/21/08
to web...@googlegroups.com
The info has been added to the wiki. Still trying with this though. :-)

I have gotten to this point. If I have in my model:

def myrecipes(category):
    db(db.recipe.category==category).select(orderby=db.recipe.title)
    return db().select(db.recipe.ALL,orderby=db.recipe.title)

I can now have

from applications.cookbook.models.db import myrecipes

and the controller recipes is now

def recipes():
    category=None
    if request.vars.category is not None: category=request.vars.category
    records=myrecipes(category)
    form=SQLFORM(db.recipe,fields=['category'])
    return dict(form=form,records=records)category)

I get a TypeError:

TypeError: myrecipes() takes no arguments (1 given)

Which I don't quite see why. Without the import of myrecipes though everything works as expected. Of course I would like to keep the import for clarity, and in fact I'd like to be able to have something like
:

from applications.cookbook.models import db as mydb

so I can have the reference and the namespace. I hope this makes sense! The code completion just helps me out tremendously in so many ways... I think it would be useful for others as well. I'll continue to see if this can be worked out.

On a side note, with this:

from gluon.sqlhtml import SQLFORM
from gluon.html import URL
from gluon.globals import Request
from gluon.globals import Session
from gluon.http import redirect

global db
global redirect
global request
global session
req=Request()
req=request
ses=Session()
ses=session

I have access to those objects with hinting and everything intact. I had hoped I could somehow "cast" the session and db variables but they aren't a type that can be cast. That is my current workaround although that may not be the best way. I am certainly open to suggestions! :-)

Michael

Michael Wills

unread,
Feb 21, 2008, 6:54:44 PM2/21/08
to web...@googlegroups.com
Ah, to clarify, standard web2py objects all work. Using auto-import for those is fine and very handy indeed. I'll clear up the wiki article soon especially if there is a way to actually import or somehow get Eclipse to see the data from db.py. Anything defined in the models, in this case models/db.py, can of course be used, but you don't get any of the hinting, etc. If you import it, you get the hinting and code completion but then the code throws an error.

As noted above, redirect is fine with it's import. The session and request variables are not though. These two are of course passed in to the controller but then the editor doesn't know anything about them. To get around this, I made req and ses to be of the right class types, and set ses to session and req to request. req and ses then have all the attributes, etc., and Eclipse can see and use them. It's a hack but it seems to work so far.

Michael Wills

unread,
Feb 21, 2008, 10:21:35 PM2/21/08
to web...@googlegroups.com
Well, in the end, since everything else seems to work, I figure just using Eclipse as a memory aid would be fine. "Just keep typing and don't hit enter!" That way you get your hint but you do lose out on the auto completion.

Michael Wills

unread,
Feb 22, 2008, 2:01:48 PM2/22/08
to web...@googlegroups.com
It turns out that my interpreters were mis-configured. It's all working now except that I can't import functions that use the database into the controller.

I tried out the identity application, loaded it up into Eclipse, got all the imports working, modified it to work with authentication ( server.login(user,password) ), defined EMAIL_SENDER which was causing an error, etc. It's all fine.

Howwever if I import and use anything that actually touches the database I get errors.

In Postgres I get:

InterfaceError: cursor already closed

In SQLite I got a ProgrammingError about the thread that created the connection is different from the current one. I thought about passing the db in use to the methods in the models (i.e. myrecipes(db,category) ), but then I get the:

TypeError: myrecipes() takes at most 1 argument (2 given)

It seems so close...
Reply all
Reply to author
Forward
0 new messages