How to provide static files in Flask
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!