Flask-FlatPages
Flask-FlatPages為Flask應用提供一組頁面。頁面是由一些“平滑的”文本文件而非關系數據庫構建。
- 工作于Python 2.6, 2.7 和 3.3+
- BSD licensed
- 最新文檔on Read the Docs
- Source, issues and pull requestson Github
- 發布管理 on PyPI
安裝
使用pip安裝擴展:
$ pip install Flask-FlatPages
或者從Github上獲取源碼。
配置
開始使用,僅需要在配置好Flask application后,實例化一個FlatPages對象:
from flask import Flask
from flask_flatpages import FlatPages
app = Flask(__name__)
app.config.from_pyfile('mysettings.cfg')
pages = FlatPages(app)
你也可以通過init_app(),遲點傳遞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:
頁面文件的目錄路徑。如果relative,解釋為相對于應用根路徑,在static和templates目錄旁邊。默認為pages。FLATPAGES_EXTENSION:
頁面的文件拓展名。在FLATPAGES_ROOT目錄內的文件,如果沒有該后綴將被忽略。默認值位.html。
0.6版起的變化:通過序列支持多個文件擴展名,如:['.htm','.html'] 或者 通過逗號分隔字符串:.htm,.html。FLATPAGES_ENCODING:
頁面文件的編碼。默認為utf8。FLATPAGES_HTML_RENDERER:
調用或導入至少一個可調用的頁面body的Unicode字符串,并以Unicode字符串的形式返回其HTML。默認為pygmented_markdown()
。
0.5版的變化:支持將FlatPages實例作為第二個參數傳遞。
0.6版的變化:支持將Page實例作為第三個參數傳遞。
渲染函數至少需要一個參數,Unicode body。第二、第三個參數是可選的,允許更高級的渲染器。FLATPAGES_MARKDOWN_EXTENSIONS:
0.4版新增。
Markdown擴展列表使用默認的HTML渲染器。默認為['codehilite']。
API
FlatPages
classflask_flatpages.FlatPages(*app=None*,*name=None*)
一組頁面對象。
使用示例:
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__()
迭代所有的頁面對象。
get(path,default=None)
返回指定地址的頁面,若無則返回默認頁面
get_or_404(path)
返回指定地址的頁面,若無則拋出Flask的404錯誤。
init_app(app)
用于初始化應用,有助于延后傳遞app和app工廠模式。
參數:app(一個Flask實例)——你的應用
reload()
忘掉所有頁面。
所有頁面將在其下一次訪問是重載。
Page
classflask_flatpages.Page
一個簡單的類,用來存儲flatpages所有必要的信息。
主要目的是用html_renderer函數來渲染頁面內容。
使用先前定義的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)
快捷訪問元數據。
page['title'] 或者在模板內,{{ page.title }},等價于 page.meta['title']
__html__()
模板內,{{ page }} 等價于 {{ page.html|safe }} 。
html
頁面內容,使用配置好的渲染器渲染為HTML。
html_renderer = None
渲染函數
meta
文件頭的元數據字典解析為YAML。
path = None
頁面的路徑,獲取于pages.get(path)
flask_flatpages.pygmented_markdown(text,flatpages=None)
渲染Markdown文檔為HTML。
只要Pygments可用,就使用CodeHilite擴展。否則,刪掉擴展列表里的“codehilite”。
如果你想用其他擴展,使用FLATPAGES_MARKDOWN_EXTENSIONS,設置其字符串序列。
flask_flatpages.pygments_style_defs(style='default')
返回值:CodeHiliteMarkdown插件定義的CSS。
參數:sytle——使用的Pygment風格。
僅當Pygments可用時。
更新日志
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.