本文整理汇总了Python中jinja2.PrefixLoader方法的典型用法代码示例。如果您正苦于以下问题:Python jinja2.PrefixLoader方法的具体用法?Python jinja2.PrefixLoader怎么用?Python jinja2.PrefixLoader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2
的用法示例。
在下文中一共展示了jinja2.PrefixLoader方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_jinja_env
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import PrefixLoader [as 别名]
def get_jinja_env():
"""Returns jinja2.Environment object that knows how to render templates."""
# TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
# the GAE SDK.
env = jinja2.Environment(
loader=jinja2.PrefixLoader({
prefix: jinja2.FileSystemLoader(path)
for prefix, path in _TEMPLATE_PATHS.items()
}),
autoescape=True,
extensions=['jinja2.ext.autoescape'],
trim_blocks=True,
undefined=jinja2.StrictUndefined)
env.filters.update(_GLOBAL_FILTERS)
env.globals.update(_GLOBAL_ENV)
return env
示例2: get_notebook_jinja2_loader
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import PrefixLoader [as 别名]
def get_notebook_jinja2_loader(nbapp):
"""Return the appropriate jinja2 template loader for the notebook app.
This is confusing but necessary to meet the following criteria:
- Templates in geonotebook/templates will override those in core notebook
templates
- Core notebook templates can still be referred to/extended by referring
to them as core@template.html
The ChoiceLoader tries each of the loaders in turn until one of them
provides the right template.
The PrefixLoader allows us to refer to core templates using the core@
prefix, this is necessary
because we want to extend templates of the same name while referring to
templates in a different loader (core).
The PackageLoader lets us put our templates ahead in priority of the
notebooks templates.
The core_loader is last which falls back to the original loader the
notebook uses.
This implementation is weird, but should be compatible/composable with
other notebook extensions and fairly future proof.
:param nbapp: NotebookApp instance
:returns: A jinja2 loader designed to work with our notebook templates
:rtype: jinja2.ChoiceLoader
"""
return ChoiceLoader([
PrefixLoader(
{'core': nbapp.web_app.settings['jinja2_env'].loader},
delimiter='@'
),
PackageLoader('geonotebook'),
nbapp.web_app.settings['jinja2_env'].loader])
示例3: init_plugins
# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import PrefixLoader [as 别名]
def init_plugins(app) -> Dict[str, AbstractTaskType]:
"""
Extracts modules (task types) from global configuration.
:param app: Current Flask application instance
:type app: flask.Flask
:return: Dictionary with instantiated TaskType objects
:rtype: dict[str, AbstractTaskType]
"""
task_types = {}
loaders = {}
enabled_tasks = app.config.get('ENABLED_TASKS', {})
files_to_watch = []
app.logger.info('Loading plugins: %s', list(enabled_tasks.keys()))
for plugin, task in enabled_tasks.items():
app.logger.debug('Started loading plugin <%s>.', plugin)
task_settings = import_string(
'{plugin_name}.settings'.format(plugin_name=plugin)
)
plugin_type = import_string(
'{plugin_name}'.format(plugin_name=plugin))
settings = plugin_type.configure(task_settings)
task_type = import_string(
'{plugin_name}.models.task_types.{task}'.format(
plugin_name=plugin, task=task)
)
loaders[task_type.type_name] = jinja2.PackageLoader(plugin)
task_types[task_type.type_name] = task_type(settings=settings)
default_static_path = plugin_type.__path__[0]
# if Flask-Collect is enabled - get files from collected dir
if 'COLLECT_STATIC_ROOT' in app.config:
# all plugin static goes stored in a dir may have prefixed name
# to prevent any collision e.g. plugin named 'images'
prefix = app.config.get('COLLECT_PLUGIN_DIR_PREFIX', '')
static_path = os.path.join(app.static_folder,
'{}{}'.format(prefix, plugin))
# else - use standard static folder
else:
static_path = default_static_path
files_to_watch.extend(_init_plugin_assets(
app=app,
task_type=task_type,
static_path=static_path))
app.logger.debug('Finished loading plugin <%s>.', plugin)
app.jinja_loader = jinja2.ChoiceLoader([
app.jinja_loader,
jinja2.PrefixLoader(loaders)])
app._plugin_files_to_watch = files_to_watch
return task_types