在烧瓶中轻松使用xstatic文件
Flask-XStatic-Files的Python项目详细描述
烧瓶X静态文件图片::https://travis-ci.org/agx/flask-xstatic-files.svg?branch=master
:目标:https://travis ci.org/agx/flask xstatic files
…highlight::python
提供“xstatic”文件的“flask”扩展。如果您
不使用资产管道并且希望直接提供xstatic packated
类似“jquery”的javascript文件,
setup
----
在初始化时告诉flask您要使用的xstatic模块,这将非常有用。本例使用jquery和d3::
app=flask(\uu name\uu)
app.config.from_object(\uu name\uu)
app.config。['xstatic\u modules']=['jquery','d3']
xsf=xstaticfiles(app)
d3``.
提供文件
----
扩展已经为您安装了路由处理程序,因此
```/xstatic/<;module>;/``下的文件将自动提供。要生成这些
url,您可以使用'url-for`::
代码块::html
<;script type=text/javascript src=“(module='jquery',path='jquery.min.js')}{xstatic\u url}”>;
这样做的好处是,以后可以通过调整“xstatic_root”和“xstatic_proto”来从静态web服务器上提供文件,而无需修改任何代码。
如果要从其他url上提供xstatic文件,请使用“service”或“service”或“404”:
@app.route('/somewhere/jquery/jquery.min.js')
def serve_jquery():
返回xsf.serve_或_404('jquery','jquery.min.js')
…_烧瓶:http://flask.pocoo.org/
…_ xstatic:https://xstatic.readthedocs.io/en/latest/
。_ jquery:https://pypi.python.org/pypi/xstatic-jquery
:目标:https://travis ci.org/agx/flask xstatic files
…highlight::python
提供“xstatic”文件的“flask”扩展。如果您
不使用资产管道并且希望直接提供xstatic packated
类似“jquery”的javascript文件,
setup
----
在初始化时告诉flask您要使用的xstatic模块,这将非常有用。本例使用jquery和d3::
app=flask(\uu name\uu)
app.config.from_object(\uu name\uu)
app.config。['xstatic\u modules']=['jquery','d3']
xsf=xstaticfiles(app)
d3``.
提供文件
----
扩展已经为您安装了路由处理程序,因此
```/xstatic/<;module>;/``下的文件将自动提供。要生成这些
url,您可以使用'url-for`::
代码块::html
<;script type=text/javascript src=“(module='jquery',path='jquery.min.js')}{xstatic\u url}”>;
这样做的好处是,以后可以通过调整“xstatic_root”和“xstatic_proto”来从静态web服务器上提供文件,而无需修改任何代码。
如果要从其他url上提供xstatic文件,请使用“service”或“service”或“404”:
@app.route('/somewhere/jquery/jquery.min.js')
def serve_jquery():
返回xsf.serve_或_404('jquery','jquery.min.js')
…_烧瓶:http://flask.pocoo.org/
…_ xstatic:https://xstatic.readthedocs.io/en/latest/
。_ jquery:https://pypi.python.org/pypi/xstatic-jquery