How to insert Sanic stream object to jinja asynchronous template
In Sanic (python async web framework) I can create stream object output to html with this:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write('<b>foo</b>')
await response.write('<b>bar</b>')
return stream(sample_streaming_fn, content_type='text/html')
Result:
foo bar
And with Jinja2 I can render asynchronously like so after turn on the async feature:
from sanic.response import html
@app.route('/')
async def test(request):
rendered_template = await template.render_async(
key='value')
return html(rendered_template)
I tried with this to output stream object to Jinja2 template:
@app.route('/')
async def test(request):
async def stream_template(response):
rendered_template = await template.render_async(
key="<b>value</b>")
await response.write(rendered_template)
return stream(stream_template, content_type='text/html') # I need it to return stream
but all I got was the downloaded template (html file).
I there any way to rig Jinja2 template.render_async to accept Sanic's response.write and return it in stream?
python python-3.x asynchronous jinja2 sanic
add a comment |
In Sanic (python async web framework) I can create stream object output to html with this:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write('<b>foo</b>')
await response.write('<b>bar</b>')
return stream(sample_streaming_fn, content_type='text/html')
Result:
foo bar
And with Jinja2 I can render asynchronously like so after turn on the async feature:
from sanic.response import html
@app.route('/')
async def test(request):
rendered_template = await template.render_async(
key='value')
return html(rendered_template)
I tried with this to output stream object to Jinja2 template:
@app.route('/')
async def test(request):
async def stream_template(response):
rendered_template = await template.render_async(
key="<b>value</b>")
await response.write(rendered_template)
return stream(stream_template, content_type='text/html') # I need it to return stream
but all I got was the downloaded template (html file).
I there any way to rig Jinja2 template.render_async to accept Sanic's response.write and return it in stream?
python python-3.x asynchronous jinja2 sanic
add a comment |
In Sanic (python async web framework) I can create stream object output to html with this:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write('<b>foo</b>')
await response.write('<b>bar</b>')
return stream(sample_streaming_fn, content_type='text/html')
Result:
foo bar
And with Jinja2 I can render asynchronously like so after turn on the async feature:
from sanic.response import html
@app.route('/')
async def test(request):
rendered_template = await template.render_async(
key='value')
return html(rendered_template)
I tried with this to output stream object to Jinja2 template:
@app.route('/')
async def test(request):
async def stream_template(response):
rendered_template = await template.render_async(
key="<b>value</b>")
await response.write(rendered_template)
return stream(stream_template, content_type='text/html') # I need it to return stream
but all I got was the downloaded template (html file).
I there any way to rig Jinja2 template.render_async to accept Sanic's response.write and return it in stream?
python python-3.x asynchronous jinja2 sanic
In Sanic (python async web framework) I can create stream object output to html with this:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write('<b>foo</b>')
await response.write('<b>bar</b>')
return stream(sample_streaming_fn, content_type='text/html')
Result:
foo bar
And with Jinja2 I can render asynchronously like so after turn on the async feature:
from sanic.response import html
@app.route('/')
async def test(request):
rendered_template = await template.render_async(
key='value')
return html(rendered_template)
I tried with this to output stream object to Jinja2 template:
@app.route('/')
async def test(request):
async def stream_template(response):
rendered_template = await template.render_async(
key="<b>value</b>")
await response.write(rendered_template)
return stream(stream_template, content_type='text/html') # I need it to return stream
but all I got was the downloaded template (html file).
I there any way to rig Jinja2 template.render_async to accept Sanic's response.write and return it in stream?
python python-3.x asynchronous jinja2 sanic
python python-3.x asynchronous jinja2 sanic
asked Nov 25 '18 at 12:53
ArdhiArdhi
694713
694713
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
After tweaking, here's what I came up with:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write(await template.render_async(key='<b>foo</b>'))
await asyncio.sleep(1) # just for checking if it's indeed streamed
await response.write(await template.render_async(key='<b>bar</b>'))
return stream(sample_streaming_fn, content_type='text/html')
And here's the Jinja2 template:
<p>{{ key|safe }}</p>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467634%2fhow-to-insert-sanic-stream-object-to-jinja-asynchronous-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
After tweaking, here's what I came up with:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write(await template.render_async(key='<b>foo</b>'))
await asyncio.sleep(1) # just for checking if it's indeed streamed
await response.write(await template.render_async(key='<b>bar</b>'))
return stream(sample_streaming_fn, content_type='text/html')
And here's the Jinja2 template:
<p>{{ key|safe }}</p>
add a comment |
After tweaking, here's what I came up with:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write(await template.render_async(key='<b>foo</b>'))
await asyncio.sleep(1) # just for checking if it's indeed streamed
await response.write(await template.render_async(key='<b>bar</b>'))
return stream(sample_streaming_fn, content_type='text/html')
And here's the Jinja2 template:
<p>{{ key|safe }}</p>
add a comment |
After tweaking, here's what I came up with:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write(await template.render_async(key='<b>foo</b>'))
await asyncio.sleep(1) # just for checking if it's indeed streamed
await response.write(await template.render_async(key='<b>bar</b>'))
return stream(sample_streaming_fn, content_type='text/html')
And here's the Jinja2 template:
<p>{{ key|safe }}</p>
After tweaking, here's what I came up with:
from sanic.response import stream
@app.route("/")
async def test(request):
async def sample_streaming_fn(response):
await response.write(await template.render_async(key='<b>foo</b>'))
await asyncio.sleep(1) # just for checking if it's indeed streamed
await response.write(await template.render_async(key='<b>bar</b>'))
return stream(sample_streaming_fn, content_type='text/html')
And here's the Jinja2 template:
<p>{{ key|safe }}</p>
answered Nov 25 '18 at 13:50
ArdhiArdhi
694713
694713
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467634%2fhow-to-insert-sanic-stream-object-to-jinja-asynchronous-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown