123
返回列表 发新帖
楼主: jieforest

利用Python Flask构建Web网站

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
21#
 楼主| 发表于 2012-10-19 13:59 | 只看该作者
In our module’s __init__.py:
  1. import os

  2. # ...

  3. env = os.environ.get('EXAMPLE_ENV', 'prod')
  4. app.config.from_object('example.settings.%sConfig' % env.capitalize())
  5. app.config['ENV'] = env
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
22#
 楼主| 发表于 2012-10-19 14:00 | 只看该作者
Assets

With the rise of rich frontends and of alternative syntaxes to css and javascript, assets manaegement has become an important aspect of web apps.

We’ll once again use a great extension, Flask-Assets, which is a wrapper for the webassets python library.
  1. $ pip install Flask-Assets
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
23#
 楼主| 发表于 2012-10-19 14:00 | 只看该作者
I store my assets file in static/, organizing them inside a css/, js/ and vendor/ folders. Below you can see that I’ve added jquery and bootstrap in the vendor dir.
  1. example/
  2.   static/
  3.     css/
  4.       layout.less
  5.     js/
  6.       main.js
  7.     vendor/
  8.       bootstrap/
  9.         css/
  10.           bootstrap.css
  11.         js/
  12.           bootstrap.min.js
  13.       jquery/
  14.         jquery-1.7.2.min.js
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
24#
 楼主| 发表于 2012-10-20 09:26 | 只看该作者
With webassets, files are grouped as bundles. Each bundle can have custom filters (eg: transform less files to css). I define my bundles inside an assets.py file:
  1. from flask_assets import Bundle

  2. common_css = Bundle(
  3.     'vendor/bootstrap/css/bootstrap.css',
  4.     Bundle(
  5.         'css/layout.less',
  6.         filters='less'
  7.     ),
  8.     filters='cssmin', output='public/css/common.css')

  9. common_js = Bundle(
  10.     'vendor/jquery/jquery-1.7.2.min.js',
  11.     'vendor/bootstrap/js/bootstrap.min.js',
  12.     Bundle(
  13.         'js/main.js',
  14.         filters='closure_js'
  15.     ),
  16.     output='public/js/common.js')
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
25#
 楼主| 发表于 2012-10-20 09:26 | 只看该作者
Here I have defined 2 bundles, one for css files and one for js files. I’ve also use nested bundles to apply specific filters to some files.

To include the bundles in our pages, webassets provides some jinja2 helpers (which we should add tolayout.html):
  1. {% assets "common_css" %}
  2.     <link rel="stylesheet" type="text/css" href="{{ ASSET_URL }}" />
  3. {% endassets %}
  4. {% assets "common_js" %}
  5.     <script type="text/javascript" src="{{ ASSET_URL }}"></script>
  6. {% endassets %}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
26#
 楼主| 发表于 2012-10-20 09:26 | 只看该作者
Now we’ll need to configure the webassets environment in __init__.py:
  1. from flask_assets import Environment
  2. from webassets.loaders import PythonLoader as PythonAssetsLoader
  3. import assets

  4. # ...

  5. assets_env = Environment(app)
  6. assets_loader = PythonAssetsLoader(assets)
  7. for name, bundle in assets_loader.load_bundles().iteritems():
  8.     assets_env.register(name, bundle)
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
27#
 楼主| 发表于 2012-10-20 09:27 | 只看该作者
As you can see, I use webassets’s PythonLoader to load bundles from the assets module and register each bundle into the environment.

You can add ASSETS_DEBUG=True in DevConfig to get debugging info. There are many other configuration parameters listed here. Param names should be prefixed with ASSETS_ and uppercased (eg.Environment.versions becomes ASSETS_VERSIONS).

Finally, the Flask-Assets extension provides some command line utilities that we’ll need to register in the manage.py file:
  1. from flask_assets import ManageAssets
  2. from example import assets_env

  3. # ...

  4. manager.add_command("assets", ManageAssets(assets_env))
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
28#
 楼主| 发表于 2012-10-20 09:28 | 只看该作者
Available commands are listed in webassets documentation but the most important one is rebuild which regenerates all your bundle files:
  1. $ ./manage.py assets rebuild
复制代码
Deploying to production

Now that we have a fully working Flask application, we’ll need to deploy it on a production machine. I like to use uWSGI + Nginx + Supervisor for my setup.

Note: this part assumes Ubuntu as your Linux distribution

Nginx acts as the frontend web server and will serve static files. uWSGI acts as the WSGI server which runs our flask app. Finally, I use supervisor to manage processes. I like to use Supervisor instead of init.d scripts as I often have other processes to manage.
  1. $ sudo apt-get install nginx supervisor
  2. $ pip install uwsgi
复制代码

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表