简化创建端点
项目描述
starlette-views – 使用 Starlette 使视图更快的辅助工具
Starlette-Views自动将视图结果转换为 starlette.responses.Response 对象。
需求
python >= 3.7
安装
starlette-views 应使用pip进行安装
pip install starlette-views
初始化
只需将您的 Starlette 应用程序包裹在视图中
from starlette_views import Views
app = Starlette(...)
app = Views(app)
或者您可以从模块中导入 Starlette (在这种情况下,您无需进行更多设置)
from starlette_views import Starlette
app = Starlette(...)
快速入门
from starlette.applications import Starlette
from starlette.routing import Route
async def hello(request):
return 'Hello from Views!'
# Create Starlette Application
app = Starlette(routes=[
Route('/', hello)
])
# Enable the quick views
app = Views(app)
然后运行应用程序…
$ uvicorn example:app
打开 http://127.0.0.1:8000 并在视图中找到hello。
使用方法
async def render_json1(request):
"""Just return dictionary to make a JSON response."""
return {'any': 'value'}
async def render_json2(request):
"""List also works well."""
return [{'any': 'value'}]
async def render_json3(request):
"""Return a tuple to set HTTP status."""
return 403, {'message': 'Authorization required'}
async def render_html1(request):
"""Return any string to make an HTML response."""
return "<h1>I would be rendered as HTML</h1>"
async def render_html2(request):
"""Return a tuple to set HTTP status."""
return 201, 'Record Created'
# Starlette Responses works as well too
from starlette.responses import HTMLResponse
async def render_normal(request):
"""Starlette Responses are returned as is."""
return HTMLResponse('Common behaviour', status_code=200)
错误跟踪器
如果您有任何建议、错误报告或烦恼,请向 https://github.com/klen/starlette-views/issues 的issue跟踪器报告。
贡献
许可协议
许可协议: MIT许可。
项目详情
关闭
starlette_views-0.0.4-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d8d3766c39f003434d648dc6335741118473c24a3dac4d20dac02c499071e3ea |
|
MD5 | 469506bdaafb17c34a317671f065345d |
|
BLAKE2b-256 | 5c0479597adb38b019e23d2452705af1dcac05ef1082870c2a14d5437d18c09e |