Add global variables to Flask templates

My Requirement

I was trying to add some information regarding my code into my Flask app. Like version and build time. This is needed for all templates as a footer it should be processed once.

My version information is stored in __version__.py in this format:

__title__ = 'fcapp'
__description__ = 'My app'
__url__ = 'https://felixchr.github.com/'
__version__='1.0.4'
__build__='0x5ee41d6d'
__author__ = 'Felix Cao'
__author_email__ = 'felix.cao@XXX.com'
__license__ = 'MIT'

The Solution

I studied online to find out the resolution. And eventually I found it on stackoverflow and here is my code here:

__init__.py of my Flask app

I added code to read and load the variables immediately after app is initialized:

app = Flask(__name__)
app.config.from_object('config')

@app.context_processor
def inject_stage_and_region():
    here = os.path.abspath(os.path.dirname(__file__))
    about = dict()
    with open(os.path.join(here, '__version__.py'), 'r') as f:
        exec(f.read(), about)
return {'about': about}

The return value is a dict. Part of this code is borrowed from Kenneth Reitz’’s requests.

The base.html of my main template

In my base.html now I can add those to this code:

  <div>
      <hr/>
      Version: {{about.__version__}}<br/>
      Build Time: {{about.__build__}}
  </div>

It works now!