Skip to content

Conversation

@pgjones
Copy link
Member

@pgjones pgjones commented Jun 9, 2022

Directly accept iterators/generators as returned from view functions

My expectation was that this was already possible to stream responses
and I can't think of a reason why this shouldn't be allowed. Quart
already does this without any issues I know of.

Add stream_template and stream_template_string functions

These helper functions make it easier to render a template piece by
piece with the correct context. Much like render_template and
render_template_string make it easy to render the entire template.

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@pgjones pgjones changed the title Generate Generate improvements Jun 9, 2022
@davidism davidism changed the title Generate improvements allow view to return generator Jun 9, 2022
@davidism davidism added this to the 2.2.0 milestone Jun 9, 2022
@pgjones pgjones force-pushed the generate branch 2 times, most recently from 2bcd250 to 2775892 Compare June 11, 2022 09:54
@davidism davidism force-pushed the generate branch 2 times, most recently from c20ed26 to a874f8a Compare June 18, 2022 19:16
@davidism
Copy link
Member

  • Instead of using yield from directly, use a closure so that we can wrap it with stream_with_context automatically if a request is active. This still returns a generator, but the function itself is not a generator.
  • Use isinstance(value, collections.abc.Iterator) instead of inspect.isgenerator(value). This allows returning iter(["a", "b", "c"]), as well as generators and generator expressions.
  • Update the types and type tests to allow Iterator, which also allows Generator.

@davidism davidism merged commit ab36542 into pallets:main Jun 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants