Serving Static Files in Flask

Static Files are files which don’t change very often, for example, CSS files, JS files, font files etc. By default, Flask looks for static files in the static sub-directory inside the application directory. We can change this default, by passing name of the directory to the static_folder keyword argument while creating application instance as follows:

This will change the default location of static files to static_dir directory inside the application directory.

For now, we will stick to the default static directory. Create a directory names static inside flask_app directory. Inside the static directory create a CSS file style.css with the following content.


Recall that in lesson Flask Basics we had discussed that Flask automatically adds a route of the form /static/<filename> to handle static files. So all we need to serve static files is to create URLs using the url_for() function as follows:


Open index.html template and add <link> tag as follows (changes are highlighted):


Start the server if not already running and visit http://localhost:5000/, you should see the body of the page in red color like this:

The method described here to serve static files is only for development. In production, we use a real web server like Nginx or Apache to serve static files.

1 thought on “Serving Static Files in Flask

  1. Thanks much for the excellent write up. If I may suggest, could you please add a section where one would deploy this on a Linux/CentOS server ? I could surely benefit from that.

Leave a Comment