本文整理匯總了Python中quart.Quart方法的典型用法代碼示例。如果您正苦於以下問題:Python quart.Quart方法的具體用法?Python quart.Quart怎麽用?Python quart.Quart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類quart
的用法示例。
在下文中一共展示了quart.Quart方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_view_decorators
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_view_decorators(app: Quart) -> None:
def decorate_status_code(func: Callable) -> Callable:
async def wrapper(*args: Any, **kwargs: Any) -> ResponseReturnValue:
response = await func(*args, **kwargs)
return response, 201
return wrapper
class Views(View):
decorators = [decorate_status_code]
methods = ["GET"]
async def dispatch_request(self, *args: Any, **kwargs: Any) -> ResponseReturnValue:
return request.method
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert response.status_code == 201
示例2: app
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def app() -> Quart:
app = Quart(__name__)
app.config["SERVER_NAME"] = SERVER_NAME
app.secret_key = "secret"
@app.route("/")
async def index() -> str:
return ""
async def index_post() -> str:
return ""
app.add_url_rule("/post", view_func=index_post, methods=["POST"], endpoint="index_post")
@app.route("/resource/<int:id>")
async def resource(id: int) -> str:
return str(id)
return app
示例3: test_websocket_accept_connection
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_websocket_accept_connection(
scope: dict, headers: Headers, subprotocol: Optional[str], has_headers: bool
) -> None:
connection = ASGIWebsocketConnection(Quart(__name__), scope)
mock_send = CoroutineMock()
await connection.accept_connection(mock_send, headers, subprotocol)
if has_headers:
mock_send.assert_called_with(
{
"subprotocol": subprotocol,
"type": "websocket.accept",
"headers": _encode_headers(headers),
}
)
else:
mock_send.assert_called_with({"subprotocol": subprotocol, "type": "websocket.accept"})
示例4: test_template_globals
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_template_globals(app: Quart, blueprint: Blueprint) -> None:
@blueprint.app_template_global()
def blueprint_global(value: str) -> str:
return value.upper()
@app.template_global()
def app_global(value: str) -> str:
return value.lower()
app.register_blueprint(blueprint)
async with app.app_context():
rendered = await render_template_string(
"{{ app_global('BAR') }} {{ blueprint_global('foo') }}"
)
assert rendered == "bar FOO"
示例5: test_template_filters
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_template_filters(app: Quart, blueprint: Blueprint) -> None:
@blueprint.app_template_filter()
def blueprint_filter(value: str) -> str:
return value.upper()
@app.template_filter()
def app_filter(value: str) -> str:
return value.lower()
app.register_blueprint(blueprint)
async with app.app_context():
rendered = await render_template_string("{{ 'App' | app_filter }}")
assert rendered == "app"
async with app.test_request_context("/"):
rendered = await render_template_string("{{ 'App' | blueprint_filter }}")
assert rendered == "APP"
示例6: test_template_tests
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_template_tests(app: Quart, blueprint: Blueprint) -> None:
@blueprint.app_template_test()
def blueprint_test(value: int) -> bool:
return value == 5
@app.template_test()
def app_test(value: int) -> bool:
return value == 3
app.register_blueprint(blueprint)
async with app.app_context():
rendered = await render_template_string("{% if 3 is app_test %}foo{% endif %}")
assert rendered == "foo"
async with app.test_request_context("/"):
rendered = await render_template_string("{% if 5 is blueprint_test %}bar{% endif %}")
assert rendered == "bar"
示例7: test_stream_with_context
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_stream_with_context() -> None:
app = Quart(__name__)
@app.route("/")
async def index() -> AsyncGenerator[bytes, None]:
@stream_with_context
async def generator() -> AsyncGenerator[bytes, None]:
yield request.method.encode()
yield b" "
yield request.path.encode()
return generator()
test_client = app.test_client()
response = await test_client.get("/")
result = await response.get_data(raw=True)
assert result == b"GET /" # type: ignore
示例8: test_cookie_jar
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_cookie_jar() -> None:
app = Quart(__name__)
app.secret_key = "secret"
@app.route("/", methods=["GET"])
async def echo() -> Response:
foo = session.get("foo")
bar = request.cookies.get("bar")
session["foo"] = "bar"
response = jsonify({"foo": foo, "bar": bar})
response.set_cookie("bar", "foo")
return response
client = Client(app)
response = await client.get("/")
assert (await response.get_json()) == {"foo": None, "bar": None}
response = await client.get("/")
assert (await response.get_json()) == {"foo": "bar", "bar": "foo"}
示例9: test_session_transactions
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_session_transactions() -> None:
app = Quart(__name__)
app.secret_key = "secret"
@app.route("/")
async def index() -> str:
return str(session["foo"])
test_client = app.test_client()
async with test_client.session_transaction() as local_session:
assert len(local_session) == 0
local_session["foo"] = [42]
assert len(local_session) == 1
response = await test_client.get("/")
assert (await response.get_data()) == b"[42]" # type: ignore
async with test_client.session_transaction() as local_session:
assert len(local_session) == 1
assert local_session["foo"] == [42]
示例10: create_app
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def create_app():
app = Quart(__name__)
app.config.from_mapping(get_config())
jwt = JWTManager(app)
app.before_serving(connect_logger)
app.before_serving(create_db)
app.before_request(add_uuid)
app.before_request(connect_zmq)
app.teardown_request(close_zmq)
@app.route("/")
async def root():
return ""
app.register_blueprint(query_endpoints_blueprint, url_prefix="/api/0")
app.register_blueprint(geography_blueprint, url_prefix="/api/0")
app.register_blueprint(spec_blueprint, url_prefix="/api/0/spec")
register_logging_callbacks(jwt)
jwt.user_loader_callback_loader(user_loader_callback)
return app
示例11: create_app
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def create_app(test_config=None):
app = Quart(__name__, instance_relative_config=True)
app.config.from_mapping(SECRET_KEY='dev')
app.config.from_pyfile('config.py', silent=True)
if test_config is not None:
app.config.from_mapping(test_config)
app.register_blueprint(view.bp)
app.azavea = azavea.Client(app.config['AZAVEA_TOKEN'])
@app.after_serving
async def teardown(*args):
await app.azavea.teardown()
return app
示例12: app
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def app() -> Quart:
app = Quart(__name__)
return app
示例13: test_view
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_view(app: Quart) -> None:
class Views(View):
methods = ["GET", "POST"]
async def dispatch_request(self, *args: Any, **kwargs: Any) -> ResponseReturnValue:
return request.method
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert "GET" == (await response.get_data(raw=False))
response = await test_client.put("/")
assert response.status_code == 405
示例14: test_method_view
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_method_view(app: Quart) -> None:
class Views(MethodView):
async def get(self) -> ResponseReturnValue:
return "GET"
async def post(self) -> ResponseReturnValue:
return "POST"
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert "GET" == (await response.get_data(raw=False))
response = await test_client.post("/")
assert "POST" == (await response.get_data(raw=False))
示例15: test_http_1_0_host_header
# 需要導入模塊: import quart [as 別名]
# 或者: from quart import Quart [as 別名]
def test_http_1_0_host_header(headers: list, expected: str) -> None:
app = Quart(__name__)
scope = {
"headers": headers,
"http_version": "1.0",
"method": "GET",
"scheme": "https",
"path": "/",
"query_string": b"",
}
connection = ASGIHTTPConnection(app, scope)
request = connection._create_request_from_scope(lambda: None)
assert request.headers["host"] == expected