In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Flask provides static file case analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
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.
Knowledge points to add:
How to provide static files in Flask
Import os.pathfrom flask import Flask, Responseapp = Flask (_ name__) app.config.from_object (_ _ name__) def root_dir (): # pragma: no cover return os.path.abspath (os.path.dirname (_ _ file__)) def get_file (filename): # pragma: no cover try: src = os.path.join (root_dir () Filename) # Figure out how flask returns static files # Tried: #-render_template #-send_file # This should not be so non-obvious return open (src). Read () except IOError as exc: return str (exc) @ app.route ('/', methods= ['GET']) def metrics (): # pragma: no cover content = get_file (' jenkins_analytics.html') return Response (content Mimetype= "text/html") @ app.route ('/', defaults= {'path':'}) @ app.route ('/') def get_resource (path): # pragma: no cover mimetypes = {".css": "text/css", ".html": "text/html", ".js": "application/javascript",} complete_path = os.path.join (root_dir ()) Path) ext = os.path.splitext (path) [1] mimetype= mimetypes.get (ext, "text/html") content = get_file (complete_path) return Response (content, mimetype=mimetype) if _ _ name__ = ='_ main__': # pragma: no cover app.run (port=80) is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.