0

I have a web application using Flask and Jinja2. I would like to inject different API-keys depending on if I'm serving the site in production or testing.

I could inject them using the Jinja templating engine and use inline <script>-tags to then access them from my other JS. But preferably I would like to have some kind of really simple template-string in my .js-files, like this:

_ready: Keen.ready(function() {
    var client = new Keen({
        projectId: $KEEN_PROJECT_ID$, 
        writeKey: $KEEN_WRITE_KEY$
    });

and then replace those keywords when Flask serve the file. Of course changing the value of the constant depending on which environment the server is running.

Are there any good ways of doing this?

Filip Nilsson
  • 115
  • 2
  • 11
  • 3
    If these are static files, Flask won't alter them. Instead, you could put a ` – dirn Aug 12 '15 at 14:09
  • @dirn: Thanks for your comment. Yeah, I realise that Flask won't alter them, I'm looking for some good way to *make* Flask alter them. Some kind of middle layer to modify the outgoing responses. And yeah, they can be public safely. – Filip Nilsson Aug 12 '15 at 15:15

1 Answers1

0

A good way to do this is by adding a global to jinja. See this answer

You can then inject the api key wherever you need and keep your template code clean by having the lookup logic somewhere else

Community
  • 1
  • 1
Ciaran Liedeman
  • 779
  • 5
  • 13