Skip to main content

sanic-asyncjinja2

Jinja2 in async mode with Sanic [experimental]

Build Status Latest Version Python Versions Tests Coverage

It is based on sanic-jinja2. It provides two decorators @template and @stream_template. The main change is in using render_async and generate_async for the decorator functions.

@stream_template is just and experimental way to make HTTP response via sanic.response.stream It gives you ability to decrease time to the first byte on response and stream long response during the processing template with data.

Example code

import asyncio
from sanic import Sanic
from sanic_asyncjinja2 import SanicAsyncJinja2


app = Sanic(__name__)
jinja = SanicAsyncJinja2()
jinja.init_app(app)


@app.route('/', methods=["GET", ])
@jinja.template("hello.html")
def index(request):
    return {"name": "world"}


@app.route('/slow_query', methods=["GET", ])
@jinja.template("hello.html")
async def slow_handler(request):
    async def func1():
        await asyncio.sleep(2)
        return "name"

    async def func2():
        await asyncio.sleep(2)
        return "world"

    async def gfunc(k, v):
        key, value = await asyncio.gather(k, v)
        return {key: value}

    f1 = func1()
    f2 = func2()
    g = await gfunc(f1, f2)
    return g


# this is just a stupid idea
# to win the time to the first byte on response
# on one of our production servers
# it is helpful with big responses
@app.route('/simplestream', methods=["GET", ])
@jinja.stream_template("hello.html")
async def handler2(request):
    very_long_text = "Very long response here..." 
    return {"name": "world", "text": very_long_text}

Release files for sanic-asyncjinja2 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-asyncjinja2 0.1.2
File Size Uploaded
sanic-asyncjinja2-0.1.2.tar.gz 3.6 kB Details

Release files / sanic-asyncjinja2-0.1.2.tar.gz

Download URL sanic-asyncjinja2-0.1.2.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
57d74c5a2a2aeaf02532a8a57396d583778bc3a714482413e2c7d39090a4b281
BLAKE2b-256 checksum
How to use checksums
5b5532b5860b16ca904a6c9a911c4ca7fe748b82e6a3d21013b3cb6276777683
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page