hug*_*own 453 python static-files flask
所以这很尴尬.我有一个应用程序,我把它放在一起,Flask
现在它只是提供一个静态HTML页面,其中包含一些CSS和JS的链接.我无法在文档Flask
中找到返回静态文件的位置.是的,我可以使用,render_template
但我知道数据没有模板化.我曾经想过send_file
或者url_for
是对的,但是我无法让它们发挥作用.与此同时,我正在打开文件,阅读内容,并Response
使用适当的mimetype来装配:
import os.path
from flask import Flask, Response
app = 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('/<path:path>')
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)
Run Code Online (Sandbox Code Playgroud)
有人想为此提供代码示例或网址吗?我知道这很简单.
atu*_*pal 569
首选方法是使用nginx或其他Web服务器来提供静态文件; 他们能够比Flask更有效率地做到这一点.
但是,您可以使用send_from_directory
从目录发送文件,这在某些情况下非常方便:
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/<path:path>')
def send_js(path):
return send_from_directory('js', path)
if __name__ == "__main__":
app.run()
Run Code Online (Sandbox Code Playgroud)
千万不能使用send_file
或send_static_file
与用户提供的路径.
send_static_file
例:
from flask import Flask, request
# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_url_path='')
@app.route('/')
def root():
return app.send_static_file('index.html')
Run Code Online (Sandbox Code Playgroud)
Ric*_*unn 86
如果您只想移动静态文件的位置,那么最简单的方法是在构造函数中声明路径.在下面的示例中,我已将模板和静态文件移动到名为的子文件夹中web
.
app = Flask(__name__,
static_url_path='',
static_folder='web/static',
template_folder='web/templates')
Run Code Online (Sandbox Code Playgroud)
static_url_path=''
从URL中删除任何前面的路径(即默认值/static
).static_folder='web/static'
将告诉Flask发送的文件
web/static
.template_folder='web/templates'
同样,这会更改模板文件夹.使用此方法,以下URL将返回CSS文件:
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">
Run Code Online (Sandbox Code Playgroud)
最后,这里是文件夹结构的一个快照,其中flask_server.py
是Flask实例:
b4s*_*ien 76
我相信你会在那里找到你需要的东西:http://flask.pocoo.org/docs/quickstart/#static-files
基本上你只需要一个包的根目录下的"静态"文件夹,然后你就可以使用http://example.com/static/foo.barurl_for('static', filename='foo.bar')
来直接链接到你的文件.
编辑:正如评论中建议的那样,您可以直接使用'/static/foo.bar'
URL路径但 url_for()
开销(性能明智)非常低,使用它意味着您以后可以轻松自定义行为(更改文件夹,更改URL路径,将您的静态文件移动到S3等).
sha*_*dow 76
你也可以,这是我的最爱,将文件夹设置为静态路径,以便每个人都可以访问里面的文件.
app = Flask(__name__, static_url_path='/static')
Run Code Online (Sandbox Code Playgroud)
使用该集,您可以使用标准HTML:
<link rel="stylesheet" type="text/css" href="/static/style.css">
Run Code Online (Sandbox Code Playgroud)
小智 38
我使用的(它工作得很好)是一个"模板"目录和一个"静态"目录.我将所有.html文件/ Flask模板放在templates目录中,static包含CSS/JS.无论你使用Flask的模板语法的程度如何,render_template对我所知的通用html文件都很好.下面是我的views.py文件中的示例调用.
@app.route('/projects')
def projects():
return render_template("projects.html", title = 'Projects')
Run Code Online (Sandbox Code Playgroud)
当您想要在单独的静态目录中引用某些静态文件时,请确保使用url_for().你可能最终会在html中的CSS/JS文件链接中这样做.例如...
<script src="{{ url_for('static', filename='styles/dist/js/bootstrap.js') }}"></script>
Run Code Online (Sandbox Code Playgroud)
这里是"规范"非正式Flask教程的链接 - 这里有很多很棒的技巧可以帮助你实现目标.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Bla*_*mba 38
你可以使用这个功能:
send_static_file(filename)
内部使用的函数将静态文件从静态文件夹发送到浏览器.
app = Flask(__name__)
@app.route('/<path:path>')
def static_file(path):
return app.send_static_file(path)
Run Code Online (Sandbox Code Playgroud)
Epi*_*rce 36
基于其他答案的最简单的工作示例如下:
from flask import Flask, request
app = Flask(__name__, static_url_path='')
@app.route('/index/')
def root():
return app.send_static_file('index.html')
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
使用名为index.html的HTML :
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<div>
<p>
This is a test.
</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
重要提示:而且index.html的是一个文件夹,名为在静态,含义<projectpath>
有.py
文件,<projectpath>\static
有html
文件.
如果您希望服务器在网络上可见,请使用 app.run(debug=True, host='0.0.0.0')
编辑:如果要求显示文件夹中的所有文件,请使用此选项
@app.route('/<path:path>')
def static_file(path):
return app.send_static_file(path)
Run Code Online (Sandbox Code Playgroud)
这本质上BlackMamba
是答案,所以给他们一个upvote.
对于创建下一个文件夹树的角度+样板流程:
backend/
|
|------ui/
| |------------------build/ <--'static' folder, constructed by Grunt
| |--<proj |----vendors/ <-- angular.js and others here
| |-- folders> |----src/ <-- your js
| |----index.html <-- your SPA entrypoint
|------<proj
|------ folders>
|
|------view.py <-- Flask app here
Run Code Online (Sandbox Code Playgroud)
我使用以下解决方案:
...
root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ui", "build")
@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
return send_from_directory(root, path)
@app.route('/', methods=['GET'])
def redirect_to_index():
return send_from_directory(root, 'index.html')
...
Run Code Online (Sandbox Code Playgroud)
它有助于将"静态"文件夹重新定义为自定义.
默认情况下,名为“static”的文件夹包含所有静态文件,代码示例如下:
<link href="{{ url_for('static', filename='vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)
一种简单的做法。 干杯!
演示文件
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug = True)
Run Code Online (Sandbox Code Playgroud)
现在创建名为templates 的文件夹名称。在模板文件夹中添加index.html文件
索引.html
<!DOCTYPE html>
<html>
<head>
<title>Python Web Application</title>
</head>
<body>
<div>
<p>
Welcomes You!!
</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
项目结构
-demo.py
-templates/index.html
Run Code Online (Sandbox Code Playgroud)
app = Flask(__name__, static_folder="your path to static")
Run Code Online (Sandbox Code Playgroud)
如果您的根目录中有模板,如果包含此文件的文件也在同一位置,则放置 app=Flask( name ) 将起作用,如果此文件位于其他位置,则必须指定模板位置以启用Flask 指向位置
所以我让事情有效(基于@ user1671599答案),并希望与你们分享.
(我希望我做得对,因为它是我在Python中的第一个应用程序)
我这样做了 -
项目结构:
server.py:
from server.AppStarter import AppStarter
import os
static_folder_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "client")
app = AppStarter()
app.register_routes_to_resources(static_folder_root)
app.run(__name__)
Run Code Online (Sandbox Code Playgroud)
AppStarter.py:
from flask import Flask, send_from_directory
from flask_restful import Api, Resource
from server.ApiResources.TodoList import TodoList
from server.ApiResources.Todo import Todo
class AppStarter(Resource):
def __init__(self):
self._static_files_root_folder_path = '' # Default is current folder
self._app = Flask(__name__) # , static_folder='client', static_url_path='')
self._api = Api(self._app)
def _register_static_server(self, static_files_root_folder_path):
self._static_files_root_folder_path = static_files_root_folder_path
self._app.add_url_rule('/<path:file_relative_path_to_root>', 'serve_page', self._serve_page, methods=['GET'])
self._app.add_url_rule('/', 'index', self._goto_index, methods=['GET'])
def register_routes_to_resources(self, static_files_root_folder_path):
self._register_static_server(static_files_root_folder_path)
self._api.add_resource(TodoList, '/todos')
self._api.add_resource(Todo, '/todos/<todo_id>')
def _goto_index(self):
return self._serve_page("index.html")
def _serve_page(self, file_relative_path_to_root):
return send_from_directory(self._static_files_root_folder_path, file_relative_path_to_root)
def run(self, module_name):
if module_name == '__main__':
self._app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
redirect
和url_for
from flask import redirect, url_for
@app.route('/', methods=['GET'])
def metrics():
return redirect(url_for('static', filename='jenkins_analytics.html'))
Run Code Online (Sandbox Code Playgroud)
这会为您的 html 中引用的所有文件(css 和 js...)提供服务。
我遇到的问题与使用和index.html
时未为目录提供文件有关。static_url_path
static_folder
这是我的解决方案:
import os
from flask import Flask, send_from_directory
from flask.helpers import safe_join
app = Flask(__name__)
static = safe_join(os.path.dirname(__file__), 'static')
@app.route('/')
def _home():
return send_from_directory(static, 'index.html')
@app.route('/<path:path>')
def _static(path):
if os.path.isdir(safe_join(static, path)):
path = os.path.join(path, 'index.html')
return send_from_directory(static, path)
Run Code Online (Sandbox Code Playgroud)