[PYTHON] Change static file storage directories and URLs in Flask

1.First of all

This time, I will explain the URL path for accessing static files in flask and how to change the storage directory. Also, since REST APIs usually don't require static files, I'd like to explain how to disable them.

1.1. Verification environment

2. Change the storage directory with static_folder

You can change the directory where static files are stored in flask with static_folder. The default setting is the static directory.

2.1. Source code

dir_change_app.py


# -*- coding: utf-8 -*-
import os
from flask import Flask

# flask
# app = Flask(__name__)
#Specified by relative path
app = Flask(__name__, static_folder='resources')
#Specify with an absolute path (directory is/)
# app = Flask(__name__, static_folder='C:/tmp/www')
#Specify with an absolute path (directory is\\)
# app = Flask(__name__, static_folder='C:\\tmp\\www')

# main
if __name__ == "__main__":
    #Show Flask mapping information
    print app.url_map
    app.run(host=os.getenv("APP_ADDRESS", 'localhost'), \
    port=os.getenv("APP_PORT", 3000))

You can see the current mapping information in ** flask's ʻurl_map`. ** **

2.2. Operation check

Leave the default, not specified


C:\demo>python dir_change_app.py
Map([<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:17:28] "GET /static/memo.txt HTTP/1.1" 200 -

By default, it starts assuming that the current directory has a static directory. So, after creating a static directory and storing memo.txt, if you access http: // localhost: 3000 / static / memo.txt with a web browser, memo.txt will be displayed. I will.

Specified by relative path


C:\demo>python dir_change_app.py
Map([<Rule '/resources/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:03:36] "GET /resources/demo.js HTTP/1.1" 200 -

If you specify the resources directory with a relative path, it will be accessed from a web browser as http: // localhost: 3000 / resources / demo.js.

Specified by absolute path


C:\demo>python dir_change_app.py
Map([<Rule '/www/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:04:57] "GET /www/demo.js HTTP/1.1" 200 -

You can also specify an absolute path. For Windows, use / or \\ to separate directories. For absolute paths, the directory name at the end of the path is the URL pathname. If the demo static_folder ='C: / tmp / www', it will be / www. How to change the URL will be explained later.

3. Change the URL to access with static_url_path

The URL to access static files in flask can be changed with static_url_path. The default setting is the same as the directory name specified in static_folder.

3.1. Source code

url_change_app.py


# -*- coding: utf-8 -*-
import os
from flask import Flask

# flask
app = Flask(__name__)
#Specify the URL path
app = Flask(__name__, static_url_path='/js')
#Use multiple delimiters for URL paths
# app = Flask(__name__, static_url_path='/h/o/g/e')
#Specify an empty string if you do not want to specify anything in the URL path
# app = Flask(__name__, static_url_path='')

# main
if __name__ == "__main__":
    #Show Flask mapping information
    print app.url_map
    app.run(host=os.getenv("APP_ADDRESS", 'localhost'), \
    port=os.getenv("APP_PORT", 3000))

Since the storage directory is not specified in static_folder here, static files are stored in the static directory of the current directory as the default.

** Please note that the value specified by static_url_path must always start with/. ** **

3.2. Operation check

Specify the URL path normally


C:\demo>python url_change_app.py
Map([<Rule '/js/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:57:08] "GET /js/demo.js HTTP/1.1" 200 -

The storage directory is static, but you can access it with /js/demo.js.

Specify multiple delimiters in the URL path


C:\demo>python url_change_app.py
Map([<Rule '/h/o/g/e/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:57:51] "GET /h/o/g/e/demo.js HTTP/1.1" 200 -

Since it is the path used when accessing by URL, you can access demo.js without any problem even if you specify multiple delimiters.

If you do not specify anything in the path


C:\demo>python url_change_app.py
Map([<Rule '/<filename>' (HEAD, OPTIONS, GET) -> static>])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Aug/2017 14:58:39] "GET /demo.js HTTP/1.1" 200 -

You can access it by simply specifying the file name in the storage directory, without specifying an unnecessary path like /demo.js.

4. Set static_folder = None to disable

4.1. Source code

no_static_app.py


# -*- coding: utf-8 -*-
import os
from flask import Flask

# flask
#Static file function disabled
app = Flask(__name__, static_folder=None)

# main
if __name__ == "__main__":
    #Show Flask mapping information
    print app.url_map
    app.run(host=os.getenv("APP_ADDRESS", 'localhost'), \
    port=os.getenv("APP_PORT", 3000))

4.2. Operation check

Static file function disabled


C:\demo>python no_static_app.py
Map([])
 * Running on http://localhost:3000/ (Press CTRL+C to quit)

** If you look at the mapping information, you can see that nothing is set and the static file function is disabled. ** **

5. Finally

This time, I explained how to change the storage directory of static files in flask and the URL to access static files.

As shown in Microservices in Python (Overview), the purpose is to create microservices (REST API), so the function of static files is originally It's unnecessary, but I saw the mapping information and found that this feature was automatically enabled.

I thought that some people might not be aware of it like myself, so I summarized it as an article this time.

As with libraries other than flask, from a security point of view, I would like to thoroughly investigate the functions that are enabled by default and understand the contents before using them.

Recommended Posts

Change static file storage directories and URLs in Flask
How to change static directory from default in Flask
Check and move directories in Python
Configuration file best practices in Flask
Write O_SYNC file in C and Python
Create and deploy Flask apps in PTVS
Automatically map controllers from URLs in Flask
Recursively get website titles and URLs in Scrapy