參考 CKAN Extensions Tutorial(v2.9):一個空的 CKAN extension,建立 ckanext-example_flask_iblueprint 擴充套件。
建立:ckanext-example_flask_iblueprint
- 進入Python Virtual Environment:
. /usr/lib/ckan/default/bin/activate - 切換目錄
cd /usr/lib/ckan/default/src - 執行 ckan generate extension,輸入:ckanext-example_flask_iblueprint。
撰寫 plugin 類別:ExampleFlaskIBlueprintPlugin
修改 ckanext-example_flask_iblueprint/ckanext/example_flask_iblueprint/plugin.py。參考完整程式,主要內容如下:
class ExampleFlaskIBlueprintPlugin(p.SingletonPlugin):
u'''
An example IBlueprint plugin to demonstrate Flask routing from an
extension.
'''
p.implements(p.IBlueprint)
p.implements(p.IConfigurer)
# IConfigurer
def update_config(self, config):
# Add extension templates directory
p.toolkit.add_template_directory(config, u'templates')
def get_blueprint(self):
u'''Return a Flask Blueprint object to be registered by the app.'''
# Create Blueprint for plugin
blueprint = Blueprint(self.name, self.__module__)
blueprint.template_folder = u'templates'
# Add plugin url rules to Blueprint object
rules = [
(u'/hello_plugin', u'hello_plugin', hello_plugin),
(u'/', u'home', override_flask_home),
(u'/helper_not_here', u'helper_not_here', helper_not_here),
(u'/helper', u'helper_here', helper_here),
(u'/flask_request', u'flask_request', flask_request),
]
for rule in rules:
blueprint.add_url_rule(*rule)
return blueprint
修改 setup.py:設定 entry_points
修改 ckanext-example_flask_iblueprint/setup.py 的 entry_points:
entry_points='''
[ckan.plugins]
example_flask_iblueprint=ckanext.example_flask_iblueprint.plugin:ExampleFlaskIBlueprintPlugin
修改 ckan.ini:啟用 plugin
修改 /etc/ckan/default/ckan.ini。CKAN 的 extension plugins 必須加到 CKAN 設定檔的 ckan.plugins 中,CKAN 才會呼叫。
ckan.plugins = stats text_view 略... example_flask_iblueprint
安裝
- 進入Python Virtual Environment:
. /usr/lib/ckan/default/bin/activate - 切換目錄:
cd /usr/lib/ckan/default/src/example_flask_iblueprint - 安裝 Plugin:
python setup.py develop - 重啟 CKAN 服務:
sudo supervisorctl restart ckan-uwsgi:*
測試
瀏覽 http://[CKAN web site] 下的 /, /hello_plugin, /helper, /helper_not_here, /flask_request 可看到對應的程式內容。
沒有留言:
張貼留言