English 中文(简体)
采用Flavik和jinja2号油轮
原标题:Using sass with Flask and jinja2

我愿在我的弗拉茨克申请中加入一个编集器。 是否有普遍接受的办法这样做?

最佳回答

推广(使用webassets/ Library)可用于此目的。 http://pypi.python.org/pypi/pyScs” rel=“nofollow noreferer”>pyScsSCSS(implemented in-030)编辑:

from flask import Flask, render_template
from flask_assets import Environment, Bundle

app = Flask(__name__)

assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle( foo.scss ,  bar.scss , filters= pyscss , output= all.css )
assets.register( scss_all , scss)

模板包括:

{% assets "scss_all" %}
<link rel=stylesheet type=text/css href="{{ ASSET_URL }}">
{% endassets %}

还将以减速方式汇编特别安全局的文件。

pyScs只支持SCSS syntax,但还有其他过滤器(sass,scscompass),这些过滤器使用原来的Ruby。

问题回答

我想到最短的。 这种办法是使用libsass的一条线解决办法。 在您的<代码> 进口 sass之后,简单地将汇编方法与代号关键词联系起来,例如:

sass.compile(dirname=( path/to/sass ,  path/to/css ))

选择确定产出风格,例如:

sass.compile(dirname=( path/to/sass ,  path/to/css ), output_style= compressed )

如果你想看到一个文件或目录,以便自动汇编每个编辑使用的文件boussole

自这一问题在2013年解答以来,一些事情发生了变化。

你们可以拥有安装在玩具的同时的c,并期望过滤器像在接受的答复中那样工作。

scss = Bundle( foo.scss ,  bar.scss , filters= pyscss , output= all.css )

我的错误是:

File "/home/sri/crap/example/flask/lib/python2.7/site-packages/webassets/filter/pyscss.py", line 110, in setup
scss.config.STATIC_ROOT = self.static_root or self.ctx.directory

您必须去除胎体(即pip uninstall scss),并确保安装 p具(即pip stalpyscs)。

2. 又注意到为了让玩具发挥作用,你必须确定一些环境变量:

app = Flask(__name__)

assets     = Environment(app)
assets.url = app.static_url_path
scss       = Bundle( index.scss , filters= pyscss , output= all.css )


assets.config[ SECRET_KEY ] =  secret! 
assets.config[ PYSCSS_LOAD_PATHS ] = assets.load_path
assets.config[ PYSCSS_STATIC_URL ] = assets.url
assets.config[ PYSCSS_STATIC_ROOT ] = assets.directory
assets.config[ PYSCSS_ASSETS_URL ] = assets.url
assets.config[ PYSCSS_ASSETS_ROOT ] = assets.directory

assets.register( scss_all , scss)

see the documentation on the pyscss filter for more info: http://webassets.readthedocs.io/en/latest/builtin_filters.html#pyscss

我希望这节省了其他人很多时间,因为我整整天浪费了。

目前,对这一问题有更好的处理方式,见。 Flask-Scs

You just have to install it: pip install Flask-Scss

在对申请进行配置后,对物体进行分类(见manage.py文档):

from flask import Flask
from flask.ext.scss import Scss

app = Flask(__name__)
Scss(app)

By default, the extension will look for your .scss files in {app.root_dir}/assets/scss or {app.root_dir}/assets and will put the generate .css files in {default_static_dir}/css or {default_static_dir}.

Libsass is a good solution for this.

# Your __init__.py file

from flask import Flask
from sassutils.wsgi import SassMiddleware

app = Flask(__name__)

app.wsgi_app = SassMiddleware(app.wsgi_app, {
     myapp : ( static/sass ,  static/css ,  /static/css )
})
# Your HTML (Jinja) file

<link href="{{ url_for( static , filename= css/style.scss.css ) }}"
      rel="stylesheet" type="text/css">

More info: https://sass.github.io/libsass-python/index.html





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签