user2212461 user2212461 - 1 year ago 133
Python Question

How to access flask config in javascript?

I need the config variables in javascript in my flask application. Currently, I access the config variables via:

def get_config_variable():
return config['VARIABLENAME']

Is this the only way to get the flask config in javascript? What would be the best practice to do this?

Answer Source

You could load the specific variable into your jinja2 globals like so:


Then in an always included base-template load the variable into your javascript like so.

     var config = {};

Or just throw the whole config into your jinja2 env like so:


And then insert the specific Variable only if it is found in config.

{% if config['VARIABLENAME'] %}
         var config = {};
         config.VARIABLENAME = {{config['VARIABLENAME']}};
{% endif %}