Flaskドライバの静的サイトジェネレータ
8773 ワード
Layout:post title:“Flask駆動静的サイトジェネレータ”discription:“翻訳”category:python tags:[flask,python]
disqus: true
{% include JB/setup %}
Dead easy yet powerful static website generator with Flask
以下は本文で、jekyllに似た感じですが、は?34行のコードは、静的サイトジェネレータを完了します.
私は私のオンラインアイデンティティを単独の管理場所に統合したいと思っています.そのため、あなたが今閲覧しているこのサイトがあります.私も静的なウェブサイトのアーキテクチャを探してしばらく経って、たくさん試しましたが、一つも気に入りませんでした.これは本当にがっかりさせられる.
そしてArmin Ronacherのこのtweetに出会った.
Frozen-Flask is really, really useful. Should have used that earlier.— Armin Ronacher (@mitsuhiko) February 6, 2012
ArminはFlaskというPythonマイクロフレームワークの作者で、flaskの簡潔さが好きです.だからこのtweetは機転が利いて、私はFrozen-Flaskの遊び方を探求し始めました.
Frozen-FlaskはFlaskを適用します_フリーズ_静的ファイルにすることで、高速で痛くもなく配置できます.さらにFlask-FiltPagesを補佐すると、フレームワークを使用して得られる特性を持つ静的サイトツールセットを完璧に生成することができます.クールurlsと簡単なパス管理 強力なテンプレート ローカル動的サービス 静的バージョン管理 第1ラウンド:プロジェクト構築
新しいフォルダに新しいvirtualenvを作成し、pipを使用して必要なパッケージをインストールします.
私たちの最初のバージョンの
実行;似たようなものを見るべきです.
ブラウザで開く_http://:127.0.0.1:8000_正常かどうかを見る.
次のラウンド:純粋なテキストページの追加
Flask-FiltPagesはあなたのFlaskアプリケーションにページを提供します.相対ダイナミックページはリレーショナル・データベースから構築され、静的ページは純粋なテキスト・ファイルから構築されます.
プロジェクトとフォルダの下に
$ mkdir pages $ vi pages/hello-world.md
ご覧のようにページ内容にMarkdownを書き込むことができます.ファイル名で任意の純粋なテキストにサービスを提供するために、アプリケーションを書き直しましょう.
次のラウンド:テンプレートの追加
flaskはjinja 2テンプレートエンジンを使用して、ページを飾るテンプレートを作成します.まず、プロジェクトルートディレクトリの下に
私たちのアプリケーションは次のようになります.
幽霊に会って、私たちはさっき何をしましたか?はアプリケーションのテンプレートを作成した.共通レイアウト(base.html)とページテンプレート(page.html) ページテンプレート基本テンプレートを拡張して、各ページに同じ内容がコピーされないようにします.
次のラウンド:ホームページにページリストを表示
今、私たちのホームページは弱く爆発しました.存在するすべてのページをリストします.
アプリケーションの
ホームページをリロードすると、ページリストが表示されます.ほんとうの彼×簡単です.
次のラウンド:ページにメタデータを追加
Flask-FiltPagesでは、
ページにラベルを追加したいと仮定すると、
メタデータはYAMLで記述されているので、文字列、ブール、整数、浮動小数点、リスト、辞書を使用して、Pythonに対応する内在的な等価物に変換することができます.
共有セクションを含む2つの異なるテンプレートを使用して、一般ページとラベルページをリストします.
新しい
アプリケーションに新しいtagパスを追加し、新しい
注意:pythonのリスト導出式が好きでなかったら、今できます.
≪静的ページの生成|Generate Static Page|emdw≫
はい、今、ファイルシステムに格納されている純粋なテキストページにサービスを提供する動的なWebサイトがあります.くだらない話です.しかし、私たちはもちろんFlaskアプリケーションではなく、静的なファイルの山であり、アプリケーションサーバを省くことができます.
Frozen-Flaskに入ります.その使用は本当に××単純:
次に、次のように実行します.
コンストラクションフォルダを開くには、次のコマンドを入力します.
考え方:びっくり
もちろん、今のバージョンは弱くて爆発して、あなたは少しずつそれのためにいろいろな特性を追加することができます.
disqus: true
{% include JB/setup %}
Dead easy yet powerful static website generator with Flask
以下は本文で、jekyllに似た感じですが、は?34行のコードは、静的サイトジェネレータを完了します.
私は私のオンラインアイデンティティを単独の管理場所に統合したいと思っています.そのため、あなたが今閲覧しているこのサイトがあります.私も静的なウェブサイトのアーキテクチャを探してしばらく経って、たくさん試しましたが、一つも気に入りませんでした.これは本当にがっかりさせられる.
そしてArmin Ronacherのこのtweetに出会った.
Frozen-Flask is really, really useful. Should have used that earlier.— Armin Ronacher (@mitsuhiko) February 6, 2012
ArminはFlaskというPythonマイクロフレームワークの作者で、flaskの簡潔さが好きです.だからこのtweetは機転が利いて、私はFrozen-Flaskの遊び方を探求し始めました.
Frozen-FlaskはFlaskを適用します_フリーズ_静的ファイルにすることで、高速で痛くもなく配置できます.さらにFlask-FiltPagesを補佐すると、フレームワークを使用して得られる特性を持つ静的サイトツールセットを完璧に生成することができます.
新しいフォルダに新しいvirtualenvを作成し、pipを使用して必要なパッケージをインストールします.
$ mkdir sample_project && cd !$
$ mkvirtualenv --no-site-packages `pwd`/env
$ source env/bin/activate
$ pip install Flask Frozen-Flask Flask-FlatPages
私たちの最初のバージョンの
sitebuilder.py
を書きます.from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello World!"
if __name__ == "__main__":
app.run(port=8000)
実行;似たようなものを見るべきです.
$ python sitebuilder.py
* Running on http://127.0.0.1:8000/
* Restarting with reloader
ブラウザで開く_http://:127.0.0.1:8000_正常かどうかを見る.
次のラウンド:純粋なテキストページの追加
Flask-FiltPagesはあなたのFlaskアプリケーションにページを提供します.相対ダイナミックページはリレーショナル・データベースから構築され、静的ページは純粋なテキスト・ファイルから構築されます.
プロジェクトとフォルダの下に
pages/
ディレクトリを作成し、hello-wolrd.md
ディレクトリを新規作成して投げ込みます.$ mkdir pages $ vi pages/hello-world.md
hello-world.md
ファイル:title: Hello World
date: 2012-03-04
**Hello World**, from a *page*!
ご覧のようにページ内容にMarkdownを書き込むことができます.ファイル名で任意の純粋なテキストにサービスを提供するために、アプリケーションを書き直しましょう.
from flask import Flask
from flask_flatpages import FlatPages
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
app = Flask(__name__)
app.config.from_object(__name__)
pages = FlatPages(app)
@app.route('/')
def index():
return "Hello World"
@app.route('/<path:path>/')
def page(path):
return pages.get_or_404(path).html
if __name__ == '__main__':
app.run(port=8000)
http://127.0.0.1:8000/hello-world/
にアクセスすると、レンダリングされた純粋なテキストが表示されます.注意pageオブジェクトからhtml属性を取得するmarkdownソースコードはhtmlに変換されます.次のラウンド:テンプレートの追加
flaskはjinja 2テンプレートエンジンを使用して、ページを飾るテンプレートを作成します.まず、プロジェクトルートディレクトリの下に
templates
フォルダを作成します.$ mkdir templates
templates/base.html
で基本レイアウトを作成します.<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My site</title>
</head>
<body>
<h1><a href="{{ url_for("index") }}">My site</a></h1>
{% block content %}
<p>Default content to be displayed</p>
{% endblock content %}
</body>
</html>
url_for()
というテンプレート関数に注意してください.これはFlaskとJinjia 2を使用してurlを生成する方法です.page.html
テンプレートを使用して、ページの内容のレイアウトを入力します.{% extends "base.html" %}
{% block content %}
<h2>{{ page.title }}</h2>
{{ page.html|safe }}
{% endblock content %}
私たちのアプリケーションは次のようになります.
from flask import Flask, render_template
from flask_flatpages import FlatPages
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
app = Flask(__name__)
app.config.from_object(__name__)
pages = FlatPages(app)
@app.route('/')
def index():
return "Hello World"
@app.route('/<path:path>/')
def page(path):
page = pages.get_or_404(path)
return render_template('page.html', page=page)
if __name__ == '__main__':
app.run(port=8000)
幽霊に会って、私たちはさっき何をしましたか?
render_template
関数を使用してページをページテンプレートで装飾します.次のラウンド:ホームページにページリストを表示
今、私たちのホームページは弱く爆発しました.存在するすべてのページをリストします.
templates/index.html
を作成します.{% extends "base.html" %}
{% block content %}
<h2>List of stuff</h2>
<ul>
{% for page in pages %}
<li>
<a href="{{ url_for("page", path=page.path) }}">{{ page.title }}</a>
</li>
{% else %}
<li>No stuff.</li>
{% endfor %}
</ul>
{% endblock content %}
hello-world.md
を作成するように、pages/
ディレクトリにファイルを保存し、.md
拡張子を使用して、より純粋なテキストページを任意に作成します.アプリケーションの
index()
パスは、次のようになります.ホームページをリロードすると、ページリストが表示されます.ほんとうの彼×簡単です.
次のラウンド:ページにメタデータを追加
Flask-FiltPagesでは、
hello-world.md
のタイトルと日付のようにメタデータを追加し、page.meta
を通じてアクセスし、愚かに見えるpython辞書を得ることができます.驚きましたね.ページにラベルを追加したいと仮定すると、
hello-world.md
は次のようになります.title: Hello World
date: 2012-03-04
tags: [general, awesome, stuff]
**Hello World**, from a *page*!
メタデータはYAMLで記述されているので、文字列、ブール、整数、浮動小数点、リスト、辞書を使用して、Pythonに対応する内在的な等価物に変換することができます.
共有セクションを含む2つの異なるテンプレートを使用して、一般ページとラベルページをリストします.
index.html
は次のようになります.{% extends "base.html" %}
{% block content %}
<h2>List of stuff</h2>
{% with pages=pages %}
{% include "_list.html" %}
{% endwith %}
{% endblock content %}
tag.html
テンプレートを作成し、ラベルページのリストを表示します.{% extends "base.html" %}
{% block content %}
<h2>List of stuff tagged <em>{{ tag }}</em></h2>
{% with pages=pages %}
{% include "_list.html" %}
{% endwith %}
{% endblock content %}
新しい
_list.html
テンプレートには、次のものが含まれます.<ul>
{% for page in pages %}
<li>
<a href="{{ url_for("page", path=page.path) }}">{{ page.title }}</a>
{% if page.meta.tags|length %}
| Tagged:
{% for page_tag in page.meta.tags %}
<a href="{{ url_for("tag", tag=page_tag) }}">{{ page_tag }}</a>
{% endfor %}
{% endif %}
</li>
{% else %}
<li>No page.</li>
{% endfor %}
</ul>
アプリケーションに新しいtagパスを追加し、新しい
tag.html
テンプレートを使用します.@app.route('/tag/<string:tag>/')
def tag(tag):
tagged = [p for p in pages if tag in p.meta.get('tags', [])]
return render_template('tag.html', pages=tagged, tag=tag)
注意:pythonのリスト導出式が好きでなかったら、今できます.
≪静的ページの生成|Generate Static Page|emdw≫
はい、今、ファイルシステムに格納されている純粋なテキストページにサービスを提供する動的なWebサイトがあります.くだらない話です.しかし、私たちはもちろんFlaskアプリケーションではなく、静的なファイルの山であり、アプリケーションサーバを省くことができます.
Frozen-Flaskに入ります.その使用は本当に××単純:
import sys
from flask import Flask, render_template
from flask_flatpages import FlatPages
from flask_frozen import Freezer
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = '.md'
app = Flask(__name__)
app.config.from_object(__name__)
pages = FlatPages(app)
freezer = Freezer(app)
@app.route('/')
def index():
return render_template('index.html', pages=pages)
@app.route('/tag/<string:tag>/')
def tag(tag):
tagged = [p for p in pages if tag in p.meta.get('tags', [])]
return render_template('tag.html', pages=tagged, tag=tag)
@app.route('/<path:path>/')
def page(path):
page = pages.get_or_404(path)
return render_template('page.html', page=page)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "build":
freezer.freeze()
else:
app.run(port=8000)
次に、次のように実行します.
$ python sitebuilder.py build
コンストラクションフォルダを開くには、次のコマンドを入力します.
$ tree
.
├── hello-world
│ └── index.html
├── index.html
└── tag
├── awesome
│ └── index.html
├── general
│ └── index.html
└── stuff
└── index.html
5 directories, 5 files
考え方:びっくり
build
ディレクトリの下の任意のファイルを静的ファイルを管理できる場所に配置することができますが、34行のPythonコードだけで完成しました......いいでしょう?もちろん、今のバージョンは弱くて爆発して、あなたは少しずつそれのためにいろいろな特性を追加することができます.