title: Accessing Flask Session variables from Flask Navigation for dynamic navigation menu
body: <p>I want to have a dynamic navigation menu that shows "Login" if the user is not currently logged on, and "Logout" if the user is logged in.</p>
  
  <p>I'm using code similar to the following:</p>
  
  <pre><code>import flask
  import flask_nav
  import flask_nav.elements as fne
  
  frontend = flask.Blueprint('frontend', __name__)
  
  application = flask.Flask(__name__)
  mySess = flask_session.Session()
  
  flask_appconfig.AppConfig(application)
  flask_bootstrap.Bootstrap(application)
  application.register_blueprint(frontend)
  application.config['BOOTSTRAP_SERVE_LOCAL'] = True
  application.config['SSL'] = True
  application.secret_key = SECRET_KEY
  application.config['SESSION_TYPE'] = SESSION_TYPE
  
  mySess.init_app(application)
  
  nav = flask_nav.Nav()
  
  class CustomRenderer(flask_bootstrap.nav.BootstrapRenderer):
      def visit_Navbar(self, node):
          nav_tag = super(CustomRenderer, self).visit_Navbar(node)
          nav_tag['class'] = 'navbar navbar-default navbar-fixed-top'
          return nav_tag
  
  flask_nav.register_renderer(application, 'custom', CustomRenderer)
  
  nav.init_app(application)
  
  @nav.navigation()
  def top_nav():
      items = [ fne.View('Home',              '.index') ]
  
      if 'google_token' in flask.session:
          items.append(fne.View('Logout',         '.logout'))
      elif 'auth_url' in flask.session:
          items.append(fne.View('Login',          flask.session['auth_url']))
      else:
          items.append(fne.View('Login',          '.login'))
  
      items.append(fne.View('About',              '.about'))
      items.append(fne.View('Contact',            '.contact'))
      items.append(fne.View('Shop',               '.shop'))
      items.append(fne.View('Help &amp; Feedback',    '.help'))
  
      return fne.Navbar('', *items)
  
  nav.register_element('frontend_top', top_nav())
  </code></pre>
  
  <p>Unfortunately, the Flask session variables are out-of-scope for the nav object, so I cannot access flask.session from within top_nav.</p>
  
  <p>I have the same difficulty when I make any stand-alone function for accessing flask-session outside of my application, for example</p>
  
  <pre><code>def user_is_logged_in():
      if 'google_token' in flask.session:
          return True
      else:
          return False
      return False
  </code></pre>
  
  <p>These functions give the expected error "RuntimeError: Working outside of request context."</p>
  
  <p>I do NOT want to use a global variable in my application.py code for the user for security reasons and so multiple people can access the application at the same time without errors.  I believe the SESSION should be storing whether the user is currently logged in or not.</p>
  
  <p>How do I get my flask_nav.Nav() to see my application's flask.session?</p>