Flask-FlatPages
Flask-FlatPages為Flask應(yīng)用提供一組頁(yè)面。頁(yè)面是由一些“平滑的”文本文件而非關(guān)系數(shù)據(jù)庫(kù)構(gòu)建渊抄。
- 工作于Python 2.6, 2.7 和 3.3+
- BSD licensed
- 最新文檔on Read the Docs
- Source, issues and pull requestson Github
- 發(fā)布管理 on PyPI
安裝
使用pip安裝擴(kuò)展:
$ pip install Flask-FlatPages
或者從Github上獲取源碼溢吻。
配置
開(kāi)始使用展箱,僅需要在配置好Flask application后沼溜,實(shí)例化一個(gè)FlatPages對(duì)象:
from flask import Flask
from flask_flatpages import FlatPages
app = Flask(__name__)
app.config.from_pyfile('mysettings.cfg')
pages = FlatPages(app)
你也可以通過(guò)init_app()漏麦,遲點(diǎn)傳遞Flask application:
pages = FlatPages()
def create_app(config='mysettings.cfg'):
app = Flask(__name__)
app.config.from_pyfile(config)
pages.init_app(app)
return app
Flask-FlatPages可接受下列配置值。所有值都是可選的:
FLATPAGES_ROOT:
頁(yè)面文件的目錄路徑艘策。如果relative蹈胡,解釋為相對(duì)于應(yīng)用根路徑,在static和templates目錄旁邊朋蔫。默認(rèn)為pages罚渐。FLATPAGES_EXTENSION:
頁(yè)面的文件拓展名。在FLATPAGES_ROOT目錄內(nèi)的文件驯妄,如果沒(méi)有該后綴將被忽略荷并。默認(rèn)值位.html。
0.6版起的變化:通過(guò)序列支持多個(gè)文件擴(kuò)展名青扔,如:['.htm','.html'] 或者 通過(guò)逗號(hào)分隔字符串:.htm,.html源织。FLATPAGES_ENCODING:
頁(yè)面文件的編碼。默認(rèn)為utf8微猖。FLATPAGES_HTML_RENDERER:
調(diào)用或?qū)胫辽僖粋€(gè)可調(diào)用的頁(yè)面body的Unicode字符串雀鹃,并以Unicode字符串的形式返回其HTML。默認(rèn)為pygmented_markdown()
励两。
0.5版的變化:支持將FlatPages實(shí)例作為第二個(gè)參數(shù)傳遞。
0.6版的變化:支持將Page實(shí)例作為第三個(gè)參數(shù)傳遞囊颅。
渲染函數(shù)至少需要一個(gè)參數(shù)当悔,Unicode body。第二踢代、第三個(gè)參數(shù)是可選的盲憎,允許更高級(jí)的渲染器。FLATPAGES_MARKDOWN_EXTENSIONS:
0.4版新增胳挎。
Markdown擴(kuò)展列表使用默認(rèn)的HTML渲染器饼疙。默認(rèn)為['codehilite']。
API
FlatPages
classflask_flatpages.FlatPages(*app=None*,*name=None*)
一組頁(yè)面對(duì)象慕爬。
使用示例:
pages = FlatPages(app)
@app.route('/')
def index():
# Articles are pages with a publication date
articles = (p for p in pages if 'published' in p.meta)
# Show the 10 most recent articles, most recent first.
latest = sorted(articles, reverse=True, key=lambda p: p.meta['published'])
return render_template('articles.html', articles=latest[:10])
@app.route('/<path:path>/')
def page(path):
page = pages.get_or_404(path)
template = page.meta.get('template', 'flatpage.html')
return render_template(template, page=page)
__iter__()
迭代所有的頁(yè)面對(duì)象窑眯。
get(path,default=None)
返回指定地址的頁(yè)面,若無(wú)則返回默認(rèn)頁(yè)面
get_or_404(path)
返回指定地址的頁(yè)面医窿,若無(wú)則拋出Flask的404錯(cuò)誤磅甩。
init_app(app)
用于初始化應(yīng)用,有助于延后傳遞app和app工廠(chǎng)模式姥卢。
參數(shù):app(一個(gè)Flask實(shí)例)——你的應(yīng)用
reload()
忘掉所有頁(yè)面卷要。
所有頁(yè)面將在其下一次訪(fǎng)問(wèn)是重載渣聚。
Page
classflask_flatpages.Page
一個(gè)簡(jiǎn)單的類(lèi),用來(lái)存儲(chǔ)flatpages所有必要的信息僧叉。
主要目的是用html_renderer函數(shù)來(lái)渲染頁(yè)面內(nèi)容奕枝。
使用先前定義的hello.html:
# hello.html
title: Hello
published: 2010-12-22
Hello, *World*!
Lorem ipsum dolor sit amet, …
>>> page = pages.get('hello')
>>> page.meta # PyYAML converts YYYY-MM-DD to a date object
{'title': u'Hello', 'published': datetime.date(2010, 12, 22)}
>>> page['title']u'Hello'
>>> page.bodyu'Hello, *World*!\n\nLorem ipsum dolor sit amet, \u2026'
>>> page.html
u'<p>Hello, <em>World</em>!</p>\n<p>Lorem ipsum dolor sit amet, \u2026</p>'
__getitem__(name)
快捷訪(fǎng)問(wèn)元數(shù)據(jù)。
page['title'] 或者在模板內(nèi)瓶堕,{{ page.title }}隘道,等價(jià)于 page.meta['title']
__html__()
模板內(nèi),{{ page }} 等價(jià)于 {{ page.html|safe }} 捞烟。
html
頁(yè)面內(nèi)容薄声,使用配置好的渲染器渲染為HTML。
html_renderer = None
渲染函數(shù)
meta
文件頭的元數(shù)據(jù)字典解析為YAML题画。
path = None
頁(yè)面的路徑默辨,獲取于pages.get(path)
flask_flatpages.pygmented_markdown(text,flatpages=None)
渲染Markdown文檔為HTML。
只要Pygments可用苍息,就使用CodeHilite擴(kuò)展缩幸。否則,刪掉擴(kuò)展列表里的“codehilite”竞思。
如果你想用其他擴(kuò)展表谊,使用FLATPAGES_MARKDOWN_EXTENSIONS,設(shè)置其字符串序列盖喷。
flask_flatpages.pygments_style_defs(style='default')
返回值:CodeHiliteMarkdown插件定義的CSS爆办。
參數(shù):sytle——使用的Pygment風(fēng)格。
僅當(dāng)Pygments可用時(shí)课梳。
更新日志
Version 0.6
Released on 2015-02-09
- Python 3 support.
- Allow multiple file extensions for FlatPages.
- The renderer function now optionally takes a third argument, namely thePage
instance. - It is now possible to instantiate multiple instances ofFlatPages
with different configurations. This is done by specifying an additional parametername
to the initializer and adding the same name in uppercase to the respective Flask configuration settings.
Version 0.5
Released on 2013-04-02
- Change behavior of passingFLATPAGES_MARKDOWN_EXTENSIONS
to renderer function, now theFlatPages
instance is optionally passed as second argument. This allows more robust renderer functions.
Version 0.4
Released on 2013-04-01
- AddFLATPAGES_MARKDOWN_EXTENSIONS
config to setup list of Markdown extensions to use with default HTML renderer. - Fix a bug with non-ASCII filenames.
Version 0.3
Released on 2012-07-03
- AddFlatPages.init_app()
- Do not use namespace packages anymore: rename the package from flaskext.flatpages
toflask_flatpages - Add configuration files for testing with tox and Travis.
Version 0.2
Released on 2011-06-02
Bugfix and cosmetic release. Tests are now installed alongside the code.
Version 0.1
Released on 2011-02-06.
First public release.