[Grok-dev] Initializing an app and module structure
faassen at startifact.com
Mon Jan 19 09:42:08 EST 2009
Jeroen Michiel wrote:
> I'm trying to get a Grok app going, but I'm having a bit of trouble knowing
> what to put where.
> First of all, what is the best way to 'prepare an app': My app relies on
> some containers to exist within the app container. How do you take care of
> them being created?
> I tried with an __init__() for my App class, but that only gets invoked if
> you create your app (which is not that bad a thing), but it requires the app
> to know all the classes for the subcontainers, but that leads to mutual
You could do it with an event:
def added(app, event):
app['mycontainer'] = MyContainer()
You don't have to place these in the same file as where you define
You could also use (sparingly) imports inside functions to break
dependencies. I much prefer imports on top of a module but one or two of
such special imports per application are all right with me.
> That leads us to my second problem: what is the best way to divide your app
> in submodules?
> What I had in mind way make a submodule per Model, and put all views and
> viewlets for that model in there, as well as the container for that module.
Typically I divide it as one module per model, where a model can be a
grok.Model or a grok.Container. I treat containers as another model,
which actually they are.
> As my viewlets need to register to a viewletmanager which I defined in the
> main app.py, I would get a mutual import. I solved this by putting the
> container class definitions in the main app.py, but it feels wrong...
You could instead look into separating out the viewletmanager into its
You could also soften dependencies by associating things with an
interface instead of with a class-based context directly. That said, I
usually find another way to avoid circular dependencies.
Generally model code does not have to depend on view code (or viewlet
code) - the dependency relation is the other way around. If you get
circular dependencies you might think about separating the two. That
said, I usually keep my views in the same module as the models.
> How do you guys generally structure a large app, or am I completely missing
> the point, here?
I structure a large app by growing it incrementally. :)
Anyway, I structure a larger application as I described above. In
relational database applications I sometimes instead place all models in
a special module model.py, and then import them into separate modules
and use a module-level grok.context so I can then declare views for them.
This is one relatively large app that I wrote that is public. It's
rather special as it uses REST all over the place and has very little
normal views or UI, but it might help you discover how one can organize
More information about the Grok-dev