Repository navigation
Streaming output renderers: Response.stream(), extras, row_stream, CSV as a default renderer - #3069
Merged
Merged
Conversation
Response.stream(body) sends strings or bytes yielded by an async iterator to the client a chunk at a time. It works anywhere a Response does, including views registered using register_routes(). If the iterator fails after the response has started Datasette can no longer send an error page, so the exception is re-raised and the server drops the connection. The client sees a failed request, not a truncated response that looks complete. The iterator is closed if the client disconnects. Refs #2936, #1101 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
register_output_renderer() dictionaries can now include an "extras" key listing the ?_extra= extras the format needs. Datasette resolves them, only for that format, and passes them to the render function as a new extras argument - a dictionary which also includes any extras requested in the URL. Extras that are not available for the current page (count on an arbitrary SQL query, for example) are omitted from the dictionary. Refs #2770, #2186, #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
The built-in JSON renderer now builds its output from rows, columns and extras, the same documented arguments available to plugin renderers. To make that possible the extras= dictionary now holds everything a page's JSON returns alongside its rows: declared and requested extras plus the keys that type of page always includes, such as next and next_url for tables. Applying transform_value() from column types moves into the JSON renderer for non-HTML table requests. Refs #2770, #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
The dictionary passed to output renderers holds everything a page's JSON returns alongside its rows - not only extras - so extras was a misleading name for it. The "extras" registration key is unchanged. Refs #2770, #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
Output renderers can accept a new row_stream argument: an async iterator over the rows on the current page, or over every row of the table or view - fetched a page at a time - when the URL has ?_stream=1. Combined with Response.stream() this lets any format export a whole table. CSV and TSV are now implemented this way in datasette/csv_renderer.py, replacing stream_csv() and the special cases for those formats in the table, query and row views. Plugins can replace either of them. The three copies of the renderer dispatch code are now a single render_with_output_renderer() function, which also handles: - ?_dl=1 for every format, not just CSV and TSV - the max_csv_mb size limit, for any streamed response A streaming export that fails part of the way through, or reaches max_csv_mb, now drops the connection instead of writing the error as the last line of an otherwise complete looking file. New ?_extra=expanded_columns extra, used by the CSV renderer. Refs #2936, #1101, #1062 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
New SETTING_ALIASES mechanism: a setting that has been renamed keeps working under its previous name in config files, the settings= parameter, --setting on the command line, datasette.setting() and the settings dictionary passed to templates. Previous names are not listed in --help-settings or /-/settings and are not documented. Setting both names at once is a startup error. Used to rename allow_csv_stream to allow_stream and max_csv_mb to max_stream_mb, since they now apply to every output format that streams. Also: - Removed OBSOLETE_SETTINGS, which nothing read. - Fixed a TypeError if the same setting was in both the config settings and the settings= parameter. Refs #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
These were only used by stream_csv(), which the CSV output renderer replaced. AsgiStream stays, since plugins use it. Refs #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
The path and full_path render arguments from #2987 are now passed by render_with_output_renderer(), replacing the three call sites that conflicted. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3069 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 79 80 +1
Lines 13960 14000 +40
=====================================
- Misses 13960 14000 +40 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Plugin hooks were told a query page was "database" - the same as the page listing a database's tables - except for output renderer render() functions, which were told "table" while can_render() for the same format was told "database". Arbitrary SQL and stored query pages now pass view_name="query" to template hooks, render() and can_render() alike. Refs #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
An output renderer whose render function accepts the undocumented data or args arguments now triggers a DeprecationWarning when it is registered. The warning is reported against the render function's own file and line. Both arguments are still passed. Use the page argument in place of data, and request.args in place of args. Refs #2770, #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
This was referenced Nov 15, 2026
Refactor .csv to be an output renderer - and teach register_output_renderer to stream all rows
#1062
Closed
…back A render function that returns a dictionary of body, content_type, status_code and headers - deprecated since Datasette 0.49 - is now an error naming the format and saying to return a Response. Any other return type gets the same error, replacing an assert. The "callback" key, the pre-0.49 name for "render", is no longer accepted. Registering a renderer without a render function is a startup error, which mentions the rename if "callback" was used. Closes #954, refs #2936 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J
This was referenced Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs:
Makes
register_output_renderer()able to stream, then moves CSV and TSV onto it so they are ordinary default renderers. Design notes are in this comment on #2936; the places this differs from that design are listed below.Claude Fable 5.2 Medium
What's new for plugins
Response.stream(body)sends an async iterator of strings or bytes a chunk at a time. It works anywhere aResponsedoes, includingregister_routes()views."extras"andpage. A renderer can declare the?_extra=extras it needs, and receives them in a newpageargument:pageholds everything the JSON for the page returns alongside its rows: declared extras, extras requested with?_extra=, and the keys that page type always has (nextandnext_urlfor tables). Extras that do not exist for the current page type are omitted.row_stream. Arenderfunction that accepts it gets an async iterator over the current page's rows, or over every row of the table or view when the URL has?_stream=1:What changed inside Datasette
datasette/csv_renderer.py.stream_csv(),is_csv_format()and the CSV branches in the table, query and row views are gone. A plugin can replace either format.dataorargs. It builds its output fromrows,columnsandpage.render_with_output_renderer()indatasette/views/base.py. It also passes thepathandfull_patharguments from path and full_path arguments for output renderers, closes #1179 #2987.datasette/app.pyre-raises if a response has already started, instead of trying to send an error page on top of it.SETTING_ALIASESmechanism: a renamed setting keeps working under its previous name in config files, thesettings=parameter,--setting,datasette.setting()and the settings dictionary passed to templates. Previous names are not listed in--help-settingsor/-/settingsand are not documented. Setting both names at once is a startup error.allow_csv_streamis renamed toallow_streamandmax_csv_mbtomax_stream_mb, using that mechanism.OBSOLETE_SETTINGS,LimitedWriter,EscapeHtmlWriterandWriteLimitExceeded, none of which were used any more.AsgiStreamstays because plugins use it.TypeErrorwhen the same setting was in both the configsettingsblock and thesettings=parameter. This predates the PR.Behaviour changes
max_stream_mb, no longer writes the error as the last line of the file. The download fails instead.view_nameis now"query"for SQL query pages, both arbitrary SQL and stored queries. Template hooks (extra_template_vars(),extra_css_urls(),extra_js_urls(),extra_body_script()) were previously passed"database"for those pages; output renderers were passed"table"byrenderand"database"bycan_render. All of them now get"query". There is an upgrade guide entry for this.Response(Remove old register_output_renderer dict mechanism in Datasette 1.0 #954). Arenderfunction that returns a dictionary ofbody,content_type,status_codeandheaders, deprecated since 0.49, is now a 500 error naming the format and saying to return aResponse. The"callback"key, the pre-0.49 name for"render", is a startup error that mentions the rename. There is an upgrade guide entry for this.data=andargs=are deprecated. Arenderfunction that accepts either now triggers aDeprecationWarningwhen it is registered, reported against the render function's own file and line. Both are still passed.max_stream_mbandallow_streamapply to every streaming format, not only CSV.?_dl=1addscontent-disposition: attachmentfor every format, unless the renderer set its own.csvandtsvappear in?_extra=renderers. New?_extra=expanded_columnsextra.?_extra=columns, list their keys in a different order (rowsnow comes after the other keys). Contents are unchanged.transform_value()from column types is applied inside the JSON renderer for non-HTML table requests.setting_allow_csv_streamandsetting_max_csv_mbare gone, replaced bysetting_allow_streamandsetting_max_stream_mb.Differences from the design comment
?_stream=1is ignored, not rejected with a 400, by formats that cannot stream. The export dialog's "Include all rows" checkbox submits to.tsv, so a plugin replacing TSV without streaming would otherwise get a 400 from Datasette's own dialog._traceHTML wrapper stayed in the CSV renderer.?_stream=1on a SQL query still returns at mostmax_returned_rows.Not in this PR
data=andargs=. They warn but are still passed to plugin renderers. Nothing built in uses them.Testing
main, run locally against an older Chromium build than the one this project's Playwright version expects. Not re-run since theview_namecommit.datasetteserver with the size limit set to 1MB, before the settings rename: a streamed CSV over the limit made curl exit with code 18 ("transfer closed with outstanding read data remaining"); one under the limit downloaded complete.New test files:
tests/test_renderer_extras.py,tests/test_renderer_streaming.py,tests/test_setting_aliases.py, plus additions totests/test_internals_response.pyandtests/test_plugins.py. The old xfailtest_max_csv_mb, which asserted the error-as-last-line behaviour, is replaced.🤖 Generated with Claude Code
https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J