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',
    NavEntry('Account', children=[
        NavEntry('Settings', url=Route('settings')),
        NavEntry('Log out', url=Route('logout')),
    NavEntry('What the world is saying 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>
    <li py:for="item in menu" class="${item.css_class(request)}">
      <a href="${item.href(request)}">${item.label}</a>

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"
      <b class="caret"></b></a>
      <ul class="dropdown-menu">
        % for subitem in item.children:
          <li class="${subitem.css_class(request)}"><a
        % endfor
  % else:
    <li class="${item.css_class(request)}"><a
  % endif
% endfor

Bases: object

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


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.


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]

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.


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.


Return the static_path() of this instance.

Return type:str