bag.web.pyramid.nav module

A simple navigation menu system for web apps.

The menu structure can be defined in advance, before any http requests come. Example:

from bag.web.pyramid.nav import NavEntry, Route, Static

menu = [
    NavEntry('Home', url=Route('home')),
    NavEntry('Support', url=Route('support')),
    NavEntry('Terms and conditions',
             url=Static('my_app:static/terms.html')),
    NavEntry('Account', children=[
        NavEntry('Settings', url=Route('settings')),
        NavEntry('Log out', url=Route('logout')),
    ]),
    NavEntry('What the world is saying about us',
             url='https://www.google.com/search?q=about+us')
]

A Kajiki template using the Pure CSS framework, without submenus:

<div class="pure-menu pure-menu-open pure-menu-horizontal">
  <a href="#" class="pure-menu-heading">My website</a>
  <ul>
    <li py:for="item in menu" class="${item.css_class(request)}">
      <a href="${item.href(request)}">${item.label}</a>
    </li>
  </ul>
</div>

Another example template using Mako and Bootstrap 3:

<ul class="nav nav-pills pull-left">
% for item in menu:
  % if item.children:
    <li class="dropdown"><a class="dropdown-toggle"
        data-toggle="dropdown">${item.label}
      <b class="caret"></b></a>
      <ul class="dropdown-menu">
        % for subitem in item.children:
          <li class="${subitem.css_class(request)}"><a
              href="${subitem.href(request)}">${subitem.label}</a></li>
        % endfor
      </ul>
    </li>
  % else:
    <li class="${item.css_class(request)}"><a
        href="${item.href(request)}">${item.label}</a></li>
  % endif
% endfor
</ul>

Bases: object

Abstract base class for objects that have an href() method.

abstract href(request)[source]

Compute and return the link.

Return type

str

class bag.web.pyramid.nav.NavEntry(label=None, img=None, icon=None, tooltip=None, url='##', children=None, **kw)[source]

Bases: object

Represents a navigation menu item, possibly with children.

ACTIVE_ITEM_CSS_CLASS = 'active'
css_class(request)[source]

Return “active” if this NavEntry corresponds to the current URL.

Return type

str

href(value, request)[source]

Compute the link previously planned in this instance.

Return type

Optional[str]

to_dict(request)[source]

Convert this instance into a dict, usually for JSON output.

Return type

Dict[str, Any]

class bag.web.pyramid.nav.Route(route_name)[source]

Bases: bag.web.pyramid.nav.BaseLink

A link that is defined by a Pyramid route name.

href(request)[source]

Return the route_path() of this instance.

Return type

str

class bag.web.pyramid.nav.Static(url_spec)[source]

Bases: bag.web.pyramid.nav.BaseLink

A link that is defined by a Pyramid static URL spec.

href(request)[source]

Return the static_path() of this instance.

Return type

str