librelist archives

« back to archive

Implementing dynamic menus with blueprint-based app

Implementing dynamic menus with blueprint-based app

From:
Philip Pemberton
Date:
2012-12-24 @ 03:05
Hi,

I'm developing an application which uses pluggable (runtime-loaded)
blueprints to provide its functionality. Essentially, the base "app"
class handles login/logout and other housekeeping, while the actual work
is done by the blueprints.

I have a menu bar along the top of each page, which is currently
hard-coded into the templates. What I'd like to do is have each
blueprint register menu items with the Flask object ("app"), which then
generates the menu list internally and passes it to the template via a
context processor.

I've tried creating a new class which inherits the Flask class, but this
causes my application to fail:

class AppClass(Flask):
	def __init__(self, *args, **kwargs):
		print "PreInit"
		super(Flask, self).__init__(*args, **kwargs)
		print "PostInit"

app = AppClass(__name__)
app.config.from_pyfile('app.cfg')


Traceback (most recent call last):
  File "DEV_run_debug.py", line 2, in <module>
    from horizon.app import app
  File "/home/philpem/dev/horizon/horizon/__init__.py", line 2, in <module>
    from .app import app
  File "/home/philpem/dev/horizon/horizon/app.py", line 21, in <module>
    app.config.from_pyfile('horizon.cfg')
AttributeError: 'AppClass' object has no attribute 'config'


Am I going about this the right way? Is there a better way to do what
I'd like to do?

Ultimately I'd like to decouple the "base" template from the blueprints
as much as possible - so the blueprint tells the app what it provides,
then the app provides links to the entry points (via url_for and the
aforementioned menu bar).

Full source is here for anyone who wishes to peruse it:
  https://bitbucket.org/philpem/horizon/src

Thanks,
-- 
Phil.
lists@philpem.me.uk
http://www.philpem.me.uk/

Re: [flask] Implementing dynamic menus with blueprint-based app

From:
Markus Unterwaditzer
Date:
2012-12-24 @ 07:57
I think you meant super(AppClass, self) instead of super(Flask, self)

-- Markus

Philip Pemberton <lists@philpem.me.uk> wrote:

>Hi,
>
>I'm developing an application which uses pluggable (runtime-loaded)
>blueprints to provide its functionality. Essentially, the base "app"
>class handles login/logout and other housekeeping, while the actual
>work
>is done by the blueprints.
>
>I have a menu bar along the top of each page, which is currently
>hard-coded into the templates. What I'd like to do is have each
>blueprint register menu items with the Flask object ("app"), which then
>generates the menu list internally and passes it to the template via a
>context processor.
>
>I've tried creating a new class which inherits the Flask class, but
>this
>causes my application to fail:
>
>class AppClass(Flask):
>	def __init__(self, *args, **kwargs):
>		print "PreInit"
>		super(Flask, self).__init__(*args, **kwargs)
>		print "PostInit"
>
>app = AppClass(__name__)
>app.config.from_pyfile('app.cfg')
>
>
>Traceback (most recent call last):
>  File "DEV_run_debug.py", line 2, in <module>
>    from horizon.app import app
>File "/home/philpem/dev/horizon/horizon/__init__.py", line 2, in
><module>
>    from .app import app
>  File "/home/philpem/dev/horizon/horizon/app.py", line 21, in <module>
>    app.config.from_pyfile('horizon.cfg')
>AttributeError: 'AppClass' object has no attribute 'config'
>
>
>Am I going about this the right way? Is there a better way to do what
>I'd like to do?
>
>Ultimately I'd like to decouple the "base" template from the blueprints
>as much as possible - so the blueprint tells the app what it provides,
>then the app provides links to the entry points (via url_for and the
>aforementioned menu bar).
>
>Full source is here for anyone who wishes to peruse it:
>  https://bitbucket.org/philpem/horizon/src
>
>Thanks,
>-- 
>Phil.
>lists@philpem.me.uk
>http://www.philpem.me.uk/

Re: [flask] Implementing dynamic menus with blueprint-based app

From:
Philip Pemberton
Date:
2012-12-30 @ 18:09
On 24/12/12 07:57, Markus Unterwaditzer wrote:
> I think you meant super(AppClass, self) instead of super(Flask, self)
> 
> -- Markus

And that is why I really shouldn't be allowed to write code at 3:05AM!

Thanks for pointing out my mistake -- now I feel like a complete fool,
but at least my code works! :D

(Apologies for the late reply - I thought I'd replied to this, but the
message was stuck in my outbox!)

Thanks,
-- 
Phil.
lists@philpem.me.uk
http://www.philpem.me.uk/