Add ui.status_code() to set HTTP status codes from page builders#5810
Open
evnchn wants to merge 1 commit intozauberzeug:mainfrom
Open
Add ui.status_code() to set HTTP status codes from page builders#5810evnchn wants to merge 1 commit intozauberzeug:mainfrom
ui.status_code() to set HTTP status codes from page builders#5810evnchn wants to merge 1 commit intozauberzeug:mainfrom
Conversation
Adds a new public API function `ui.status_code(code)` that allows setting the HTTP response status code from within a page builder function. Uses it in main.py to return 404 for unknown documentation pages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 19, 2026
Contributor
|
Quick clarification:
Undefined routes already return 404. Or am I missing something? @ui.page('/')
def page():
ui.label('Hello, world!') |
Contributor
|
Ah! You're focusing on scenario 2: The distinction is between two different scenarios:
|
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.
Motivation
Page builder functions currently have no way to control the HTTP status code of the response. This is needed for proper SEO (returning 404 for not-found pages) and for any scenario where a page needs to signal a non-200 status.
Split out from #5767 per review feedback — this is a public API addition that deserves focused review.
Implementation
nicegui/functions/status_code.py(new):ui.status_code(code)setscontext.client.status_codenicegui/client.py: Addsstatus_codefield toClient, uses it inbuild_response()when setnicegui/ui.py: Exportsstatus_codein__all__and importsmain.py: Usesui.status_code(404)for unknown documentation pagestests/test_page.py: Tests for custom and default status codesOpen questions for review
codebe validated (e.g. must be in 100-599 range)?Progress
🤖 Generated with Claude Code