VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-serve-static-files-in-flask/

⇱ How to serve static files in Flask - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to serve static files in Flask

Last Updated : 11 Jun, 2026

Flask uses a dedicated static directory to serve files that do not change dynamically, such as stylesheets, JavaScript code, images, videos, and other media assets. These files can be linked directly from templates and are automatically made available by the Flask application. This guide will show how to serve different types of static files in a Flask web application efficiently.

File Structure 

👁 File-Structure
File Structure

HTML File

To serve HTML files in Flask, just create a templates folder in the project root and add your HTML files (e.g., templates/index.html). Using Jinja ({{ message }}), you can display text stored in a variable.

main.py

In main.py, we use Flask's render_template() function to render the HTML file when the app runs. The final code looks like this:

Output:

The Flask is up and running on localhost port http://127.0.0.1:5000/

👁 Localhost
Localhost

Serve CSS file in Flask

Now serving a CSS file is the same as an HTML file but instead of /templates folder, we create a static folder in the root directory and add all CSS files to it, For simplicity, we have used a very simple CSS file.

Now, let us link it with the HTML template file using the link tag referring to the CSS file in the static folder.

Output:

👁 Localhost
localhost:5000

Serve JavaScript file in Flask

To serve Javascript it is the same as a CSS file create a javascript file in the static folder.

Now link it with the HTML and run the Flask app.

Output:

👁 Javascript-static-file
Javascript static file

Serve Media files in Flask (Image, Video, Audio)

Flask can also serve media files like images, videos, audio, text files, and PDFs using the static folder. Just like CSS and JavaScript, media files are stored in static/ and linked to HTML files.

Steps to Serve Media Files:

  • Place all media files inside the static/ folder.
  • Link them in HTML using the /static/ path. Example: <img src="{{ url_for('static', filename='images/sample.jpg') }}" alt="Sample Image">
  • The url_for('static', filename=...) function generates the correct URL for files stored inside the static directory and is the recommended way to reference static assets in Flask templates.
  • In main.py, create routes for any static files you need to render.
  • Run the Flask app, and media files will be served along with the web pages.

Images

Create an image.html file in the templates folder and add the following code to the main.py and image.html respectively.

templates/images.html

Output:

👁 How to serve static files in Flask
Image Route

Video Files

To serve a video file, create a video.html file in your templates folder and add the following code to your main.py and video.html files.

templates/video.html

As you see the mp4 video file is been served by Flask over localhost.

Output:

👁 How to serve static files in Flask\
Video Route

Audio Files

Respectively an audio file can be served by creating an audio.html template file and adding the following code to the main.py.

templates/audio.html

Output:

👁 audio-route
Audio Route

Serving PDF and Text Files in Flask

In addition to media files, Flask can serve documents such as PDF and text files directly from the static directory. These files can be linked in HTML using the url_for() function, allowing users to view or download them.

1. Serving a PDF File

Place a PDF file inside the static folder and add the following link to an HTML template:

2. Serving a Text File

Similarly, a text file stored in the static directory can be accessed using:

Explanation:

  • Store Files in the Static Folder: PDF and text files should be placed inside the static directory.
  • Generate URLs Dynamically: The url_for('static', filename=...) function generates the correct path to the file.
  • Support File Downloads: Users can view or download documents directly through their browser.
  • Serve Static Documents Efficiently: Flask automatically makes files stored in the static folder accessible without creating additional routes.

Complete Flask Code

For simplicity, we have created a simple Flask application for a better understanding of how to serve static files in Flask.

Let's test the Flask app by running it, to run the app just run the python main.py which will serve output as shown above:

👁 main-py
main.py
Comment
Article Tags:
Article Tags: