Flask tricks for your web development


(last time edited: 2021-03-24)

tags: linux, webdev

Python bytecode in local development

There's a nice little environment variable for your shell which can make local web development a lot easier. You should put it in shell rc (for example .bashrc) or in .profile or .xprofile (Xorg login)


Turn this boolean buddy to true and you won't have to deal with annoying Python cache binaries.

This can make a lot whole easier the task of uploading your Git project repository online by not having to deal with them.

Make sure to turn it false back again in your production server.

Flask, proxy pass and NGINX

If you use Gunicorn and NGINX to deploy your WSGI apps, don't forget to use these settings in the NGINX configuration.

This kind of settings work along with a proxy_pass directive.

proxy_pass http://unix:/tmp/gunicorn.sock;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;

More info on Gunicorn + NGINX proxy pass deployment here.

Flask, subdomains and CORS policy on NGINX

If you use static subdomain in your Flask webapp, you may get errors like this:

Access to font at https://static.your_domain.com/path/to/font.woff2 from origin https://your_domain.com has been blocked by CORS policy: No 'Access Control Allow-Origin' header is present on the requested resource.

Failed to load resource: net::ERR_FAILED

This can be fixed by adding Access-Control-Allow-Origin headers in the NGINX server block.

add_header Access-Control-Allow-Origin *;

or more specific

add_header Access-Control-Allow-Origin https://static.$host$request_uri;

$scheme translates to https or http $server_name translates to your_domain.com $host translates to subdomain+domain $request\uri translates to the requested uri the space in the server you want to get access to.