Skip to content

Streaming output renderers: Response.stream(), extras, row_stream, CSV as a default renderer - #3069

Merged
simonw merged 11 commits into
mainfrom
claude/funny-ramanujan-ehkqn7
Nov 15, 2026
Merged

simonw merged 11 commits into
mainfrom
claude/funny-ramanujan-ehkqn7

Conversation

@simonw

@simonw simonw commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

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 a Response does, including register_routes() views.

"extras" and page. A renderer can declare the ?_extra= extras it needs, and receives them in a new page argument:

def render_summary(rows, page):
    return Response.text(
        "Showing {} of {} rows".format(len(rows), page.get("count"))
    )

@hookimpl
def register_output_renderer(datasette):
    return {
        "extension": "summary",
        "render": render_summary,
        "extras": ["count"],
    }

page holds everything the JSON for the page returns alongside its rows: declared extras, extras requested with ?_extra=, and the keys that page type always has (next and next_url for tables). Extras that do not exist for the current page type are omitted.

row_stream. A render function 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:

def render_ndjson(columns, row_stream):
    async def body():
        async for row in row_stream:
            yield json.dumps(dict(zip(columns, row))) + "\n"
    return Response.stream(body(), content_type="application/x-ndjson")

What changed inside Datasette

  • CSV and TSV now live in 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.
  • The built-in JSON renderer no longer takes data or args. It builds its output from rows, columns and page.
  • The three copies of the renderer dispatch code are one function, render_with_output_renderer() in datasette/views/base.py. It also passes the path and full_path arguments from path and full_path arguments for output renderers, closes #1179 #2987.
  • The top-level error handler in datasette/app.py re-raises if a response has already started, instead of trying to send an error page on top of it.
  • Setting aliases. New SETTING_ALIASES mechanism: a renamed setting keeps working under its previous name in config files, the settings= parameter, --setting, 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.
  • allow_csv_stream is renamed to allow_stream and max_csv_mb to max_stream_mb, using that mechanism.
  • Removed OBSOLETE_SETTINGS, LimitedWriter, EscapeHtmlWriter and WriteLimitExceeded, none of which were used any more. AsgiStream stays because plugins use it.
  • Fixed a TypeError when the same setting was in both the config settings block and the settings= parameter. This predates the PR.

Behaviour changes

  • Failed exports drop the connection. An "include all rows" export that errors part way through, or reaches max_stream_mb, no longer writes the error as the last line of the file. The download fails instead.
  • view_name is 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" by render and "database" by can_render. All of them now get "query". There is an upgrade guide entry for this.
  • Output renderers must return a Response (Remove old register_output_renderer dict mechanism in Datasette 1.0 #954). A render function that returns a dictionary of body, content_type, status_code and headers, deprecated since 0.49, is now a 500 error naming the format and saying to return a Response. 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= and args= are deprecated. A render function that accepts either now triggers a DeprecationWarning when it is registered, reported against the render function's own file and line. Both are still passed.
  • max_stream_mb and allow_stream apply to every streaming format, not only CSV.
  • ?_dl=1 adds content-disposition: attachment for every format, unless the renderer set its own.
  • csv and tsv appear in ?_extra=renderers. New ?_extra=expanded_columns extra.
  • Custom SQL CSV no longer runs the query twice.
  • Row JSON, and query JSON with ?_extra=columns, list their keys in a different order (rows now comes after the other keys). Contents are unchanged.
  • transform_value() from column types is applied inside the JSON renderer for non-HTML table requests.
  • Error messages: "CSV streaming is disabled" is now "Streaming is disabled", and "_next not allowed for CSV streaming" is now "_next not allowed when streaming".
  • The docs anchors setting_allow_csv_stream and setting_max_csv_mb are gone, replaced by setting_allow_stream and setting_max_stream_mb.

Differences from the design comment

  • ?_stream=1 is 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.
  • The _trace HTML wrapper stayed in the CSV renderer.
  • ?_stream=1 on a SQL query still returns at most max_returned_rows.

Not in this PR

  • Removing data= and args=. They warn but are still passed to plugin renderers. Nothing built in uses them.
  • Streaming JSON.

Testing

  • Full Python suite: 3205 passed, on the current head.
  • Chromium browser suite: 177 passed after merging main, run locally against an older Chromium build than the one this project's Playwright version expects. Not re-run since the view_name commit.
  • Against a real datasette server 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 to tests/test_internals_response.py and tests/test_plugins.py. The old xfail test_max_csv_mb, which asserted the error-as-last-line behaviour, is replaced.

🤖 Generated with Claude Code

https://claude.ai/code/session_016BvfqCn4No2Nib8F28Zt5J

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

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 266 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (e9d45fe) to head (f928f3c).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
datasette/csv_renderer.py 0.00% 71 Missing ⚠️
datasette/app.py 0.00% 52 Missing ⚠️
datasette/views/base.py 0.00% 51 Missing ⚠️
datasette/renderer.py 0.00% 32 Missing ⚠️
datasette/utils/asgi.py 0.00% 20 Missing ⚠️
datasette/views/table.py 0.00% 19 Missing ⚠️
datasette/views/row.py 0.00% 8 Missing ⚠️
datasette/views/table_extras.py 0.00% 8 Missing ⚠️
datasette/views/database.py 0.00% 4 Missing ⚠️
datasette/cli.py 0.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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
…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
@simonw
simonw merged commit b98e1a2 into main Nov 15, 2026
13 checks passed
@simonw
simonw deleted the claude/funny-ramanujan-ehkqn7 branch November 15, 2026 04:54
@simonw simonw added this to the Datasette 1.0 stable milestone Nov 15, 2026
@simonw simonw added the plugins label Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove old register_output_renderer dict mechanism in Datasette 1.0

2 participants