示例#1
0
def global_init(template_folder: str, auto_reload=False, cache_init=True):
    global __templates, template_path

    if __templates and cache_init:
        return

    if not template_folder:
        msg = f'The template_folder must be specified.'
        raise FastAPIChameleonException(msg)

    if not os.path.isdir(template_folder):
        msg = f"The specified template folder must be a folder, it's not: {template_folder}"
        raise FastAPIChameleonException(msg)

    template_path = template_folder
    __templates = PageTemplateLoader(template_folder, auto_reload=auto_reload)
示例#2
0
def render(template_file: str, **template_data):
    if not __templates:
        raise FastAPIChameleonException(
            "You must call global_init() before rendering templates.")

    page: PageTemplate = __templates[template_file]
    return page.render(encoding='utf-8', **template_data)
示例#3
0
def __render_response(template_file, response_val, mimetype):
    # source skip: assign-if-exp
    if isinstance(response_val, fastapi.Response):
        return response_val

    if template_file and not isinstance(response_val, dict):
        msg = f"Invalid return type {type(response_val)}, we expected a dict or fastapi.Response as the return value."
        raise FastAPIChameleonException(msg)

    model = response_val

    html = render(template_file, **model)
    return fastapi.Response(content=html, media_type=mimetype)