Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Static Files

Flask Static Files

No complete Web application can do without static resources such as CSS, JavaScript, and images.

This chapter introduces how Flask manages and serves static files.


Default static file conventions

Flask serves static files from the static/ folder under the application directory by default.

You only need to create this folder and place your static resources in it:



myflaskapp/

├── app.py

├── templates/

│   └── index.html

└── static/

    ├── css/

    │   └── style.css          # 样式文件

    ├── js/

    │   └── main.js            # JavaScript 文件

    └── images/

        └── logo.png           # 图片文件


Referencing static files in templates

Do not hardcode paths; use url_for('static', filename='...') to generate URLs for static files.

The benefit is that even if the application is later mounted at a subpath (such as /myapp), the URLs will be adjusted automatically.

Example

   

   

<p>RUNOOB Flask Tutorial</p>

   

   

   

   

   

   

RUNOOB Logo

   

Welcome to RUNOOB

   

This is a Web application built with Flask.

   

   


Customizing static file paths

If you do not want to use the default static/ folder, you can customize it when creating the Flask application:

Example

from

flask

import

Flask

static_folder: The folder path on the server

static_url_path: The access path in the URL

app

=

Flask

(

name

,

           static_folder

=

"public"

,

       

Static files are placed in the public/ directory

           static_url_path

=

"/assets"

)

   

Accessed via the /assets path

In this case, accessing /assets/css/style.css corresponds to the file public/css/style.css

Parameters Default value Description
static_folder "static" The local folder that stores static files
static_url_path None (use the name of static_folder) The URL prefix under which static files are exposed

CSS file example

Create a basic stylesheet to make the page look more professional:

Example

/* File path: static/css/style.css */

body

{

   

font-family

:

-apple-system

,

BlinkMacSystemFont

,

"Segoe UI"

,

Roboto

,

sans-serif

;

   

max-width

:

800px

;

   

margin

:

0

auto

;

   

padding

:

20px

;

   

line-height

:

1.6

;

   

color

:

#333

;

}

h1

{

   

color

:

#2c3e50

;

   

border-bottom

:

2px

solid

#3498db

;

   

padding-bottom

:

10px

;

}

nav

{

   

background

:

#f8f9fa

;

   

padding

:

10px

;

   

border-radius

:

4px

;

   

margin-bottom

:

20px

;

}

nav a

{

   

color

:

#3498db

;

   

text-decoration

:

none

;

   

margin-right

:

15px

;

}

footer

{

   

margin-top

:

40px

;

   

padding-top

:

20px

;

   

border-top

:

1px

solid

#eee

;

   

color

:

#999

;

   

font-size

:

14px

;

}

Visit http://127.0.0.1:5000/, and the result is as follows:


The difference between development and production environments

Flask's built-in static file serving is only suitable for Development environment。

In a production environment, static files should be handled directly by professional Web servers such as Nginx or Apache, which is far more efficient than serving them through Python code.

Details of production deployment will be covered in later chapters.