Get the App
SLTechnology News&Howtos  ›  Development  › 

How to provide static files in Flask

Shulou Source: shulou.com Published: 2022-06-03 17:31:53 11月13日 Update

This article mainly explains "how to provide static files in Flask". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to provide static files in Flask.

1. You can use send_from_directory to send files from a directory, which is very convenient in some cases.

From flask import Flask, request, send_from_directory # set the project root directory as the static folder, you can set others.app = Flask (_ _ name__, static_url_path='') @ app.route ('/ js/') def send_js (path): return send_from_directory ('js', path) if _ _ name__ = "_ _ main__": app.run ()

2. You can use app.send_file or app.send_static_file, but it is strongly recommended not to do so.

Because it may cause a security risk to the path provided by the user.

Send_from_directory aims to control these risks.

Finally, the preferred approach is to use NGINX or other Web servers to provide static files, which will be more efficient than Flask.

At this point, I believe you have a deeper understanding of "how to provide static files in Flask". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File static content method risk learning practical deeper effective security interest practicality practical suggestion situation easy to operate more friends servers users Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Microsoft Huawei Redmi