How to use the sanic.response.html function in sanic

To help you get started, we’ve selected a few sanic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github defnngj / learning-API-test / chat_robot / chat_server.py View on Github external
async def index(request):
    """
    聊天页面
    """
    template = env.get_template('index.html')
    html_content = template.render(title='聊天机器人')
    return html(html_content)
github bosondata / chrome-prerender / prerender / app.py View on Github external
if _ENABLE_CB:
            user_agent = request.headers.get('user-agent', '')
            _os, browser = httpagentparser.simple_detect(user_agent)
            breaker = _BREAKERS[browser]
            data, status_code = await breaker.run(lambda: _render(request.app.prerender, url, format, proxy))
        else:
            data, status_code = await _render(request.app.prerender, url, format, proxy)
        headers.update({'X-Prerender-Cache': 'miss', 'Last-Modified': formatdate(usegmt=True)})
        logger.info('Got %d for %s in %dms',
                    status_code,
                    url,
                    int((time.time() - start_time) * 1000))
        if format == 'html':
            if 200 <= status_code < 300:
                executor.submit(_save_to_cache, url, data.encode('utf-8'), format)
            return response.html(
                apply_filters(data, HTML_FILTERS),
                headers=headers,
                status=status_code
            )
        if 200 <= status_code < 300:
            executor.submit(_save_to_cache, url, data, format)
        return response.raw(data, headers=headers, status=status_code)
    except (asyncio.TimeoutError, asyncio.CancelledError, TemporaryBrowserFailure, RetriesExhausted):
        logger.warning('Got 504 for %s in %dms',
                       url,
                       int((time.time() - start_time) * 1000))
        return response.text('Gateway timeout', status=504)
    except TooManyResponseError:
        logger.warning('Too many response error for %s in %dms',
                       url,
                       int((time.time() - start_time) * 1000))
github simonw / json-head / json_head.py View on Github external
head_infos = await asyncio.gather(*[
                head(session, url) for url in urls
            ])
            if callback and is_valid_callback(callback):
                return response.text(
                    '{}({})'.format(callback, json.dumps(head_infos, indent=2)),
                    content_type='application/javascript',
                    headers={'Access-Control-Allow-Origin': '*'},
                )
            else:
                return response.json(
                    head_infos,
                    headers={'Access-Control-Allow-Origin': '*'},
                )
    else:
        return response.html(INDEX)
github its-a-feature / Apfell / apfell-docker / app / routes / api_routes.py View on Github external
async def apiui_command_help(request, user):
    template = env.get_template('apiui_command_help.html')
    if use_ssl:
        content = template.render(links=await respect_pivot(links, request), name=user['username'], http="https", ws="wss", config=user['ui_config'])
    else:
        content = template.render(links=await respect_pivot(links, request), name=user['username'], http="http", ws="ws", config=user['ui_config'])
    return response.html(content)
github pyx / sanic-auth / examples / note.py View on Github external
async def login(request):
    message = ''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        # for demonstration purpose only, you should use more robust method
        if username == 'demo' and password == '1234':
            # use User proxy in sanic_auth, this should be some ORM model
            # object in production, the default implementation of
            # auth.login_user expects User.id and User.name available
            user = User(id=1, name=username)
            auth.login_user(request, user)
            return response.redirect('/')
        message = 'invalid username or password'
    return response.html(LOGIN_FORM.format(message))
github nexmo-community / audiosocket-demo / server.py View on Github external
async def index_handler(request):
    if CONFIG.fully_configured:
        return response.html(jinja.get_template("index.html").render(
            phone_number=format_number(CONFIG.phone_number),
            host=CONFIG.host))
    else:
        return response.html(jinja.get_template("env_errors.html").render(
            missing_envs=CONFIG.missing_keys))
github HackerDom / qctf-school-2018 / tasks / broadcast / service / main.py View on Github external
async def index(request, token):
    if token not in TOKENS:
        return response.text("Not Found! \nIncorrect token could be used!", 404)
    return response.html(INDEX_PAGE)
github PWZER / swagger-ui-py / swagger_ui / core.py View on Github external
async def swagger_blueprint_editor_handler(request):
                return response.html(self.editor_html)
github huge-success / sanic / sanic / handlers.py View on Github external
response_message = "Exception occurred while handling uri: %s"
        logger.exception(response_message, url)

        if issubclass(type(exception), SanicException):
            return text(
                "Error: {}".format(exception),
                status=getattr(exception, "status_code", 500),
                headers=getattr(exception, "headers", dict()),
            )
        elif self.debug:
            html_output = self._render_traceback_html(exception, request)

            return html(html_output, status=500)
        else:
            return html(INTERNAL_SERVER_ERROR_HTML, status=500)