title: Dynamic navigation in Flask
body: <p>I have a pretty simple site working in Flask that's all powered from an sqlite db. Each page is stored as a row in the page table, which holds stuff like the path, title, content.</p>
  
  <p>The structure is hierarchical where a page can have a parent. So while for example, 'about' may be a page, there could also be 'about/something' and 'about/cakes'. So I want to create a navigation bar with links to all links that have a parent of '/' (/ is the root page). In addition, I'd like it to also show the page that is open and all parents of that page.</p>
  
  <p>So for example if we were at 'about/cakes/muffins', in addition to the links that always show, we'd also see the link to 'about/cakes', in some manner like so:</p>
  
  <pre><code>- About/
    - Cakes/
      - Muffins
      - Genoise
    - Pies/
  - Stuff/
  - Contact
  - Legal
  - Etc.[/]
  </code></pre>
  
  <p>with trailing slashes for those pages with children, and without for those that don't.</p>
  
  <p>Code:</p>
  
  <pre><code>@app.route('/')
  def index():
      page = query_db('select * from page where path = "/"', one=True)
      return render_template('page.html', page=page, bread=[''])
  
  @app.route('/&lt;path&gt;')
  def page(path=None):
      page = query_db('select * from page where path = "%s"' % path, one=True)
      bread = Bread(path)
      return render_template('page.html', page=page, crumbs=bread.links)
  </code></pre>
  
  <p>I already feel like I'm violating DRY for having two functions there. But doing navigation will violate it further, since I also want the navigation on things like error pages.</p>
  
  <p>But I can't seem to find a particularly Flasky way to do this. Any ideas?</p>