Better asset handling

This commit is contained in:
Brian Duggan 2018-05-24 09:56:02 -04:00
parent d696540371
commit de0fce88f1
4 changed files with 29 additions and 3 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules
.sass-cache/
static/fonts/*
.webassets-cache
scss/assets

22
app.py
View File

@ -3,19 +3,37 @@
import tornado.ioloop import tornado.ioloop
import tornado.web import tornado.web
import os import os
from webassets import Environment, Bundle
# Set up assets.
static_path = os.path.join(os.path.dirname(__file__), "static")
scss_path = os.path.join(os.path.dirname(__file__), "scss")
assets = Environment(directory=scss_path, url='/static')
css = Bundle('atat.scss', output='assets/out.css')
assets.register('css', css)
helpers = {
'assets': assets
}
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
def get_template_namespace(self):
ns = super(MainHandler, self).get_template_namespace()
ns.update(helpers)
return ns
def get(self): def get(self):
self.render("hello.html.to") self.render("hello.html.to")
def make_app(): def make_app():
return tornado.web.Application([ app = tornado.web.Application([
(r"/", MainHandler), (r"/", MainHandler),
], ],
debug=os.getenv('DEBUG',False), debug=os.getenv('DEBUG',False),
template_path=os.path.join(os.path.dirname(__file__), "templates"), template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"), static_path=static_path
) )
return app
if __name__ == "__main__": if __name__ == "__main__":
app = make_app() app = make_app()

View File

@ -1 +1,2 @@
tornado==5.0.2 tornado==5.0.2
webassets==0.12.1

View File

@ -4,7 +4,9 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{% block title %}JEDI{% end %}</title> <title>{% block title %}JEDI{% end %}</title>
<link rel="stylesheet" href="static/assets/atat.css"> {% for url in assets['css'].urls() %}
<link rel="stylesheet" href="{{ url }}" type="text/css">
{% end %}
</head> </head>
<body> <body>
<header class="usa-header usa-header-basic" role="banner"> <header class="usa-header usa-header-basic" role="banner">