Skip to content

Comments

Add ui.status_code() to set HTTP status codes from page builders#5810

Open
evnchn wants to merge 1 commit intozauberzeug:mainfrom
evnchn:ui-status-code
Open

Add ui.status_code() to set HTTP status codes from page builders#5810
evnchn wants to merge 1 commit intozauberzeug:mainfrom
evnchn:ui-status-code

Conversation

@evnchn
Copy link
Collaborator

@evnchn evnchn commented Feb 19, 2026

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) sets context.client.status_code
  • nicegui/client.py: Adds status_code field to Client, uses it in build_response() when set
  • nicegui/ui.py: Exports status_code in __all__ and imports
  • main.py: Uses ui.status_code(404) for unknown documentation pages
  • tests/test_page.py: Tests for custom and default status codes

Open questions for review

  • Should code be validated (e.g. must be in 100-599 range)?
  • Should it return the previous code?
  • Does it need documentation on the website?

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
  • If this PR addresses a security issue, it has been coordinated via the security advisory process.
  • Pytests have been added (or are not necessary).
  • Documentation has been added (or is not necessary).

🤖 Generated with Claude Code

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>
@evnchn evnchn added the feature Type/scope: New or intentionally changed behavior label Feb 19, 2026
@falkoschindler
Copy link
Contributor

Quick clarification:

This is needed for proper SEO (returning 404 for not-found pages)

Undefined routes already return 404. Or am I missing something?

@ui.page('/')
def page():
    ui.label('Hello, world!')
$ curl -I http://127.0.0.1:1234/foo
HTTP/1.1 404 Not Found
...

@falkoschindler
Copy link
Contributor

Ah! You're focusing on scenario 2:


The distinction is between two different scenarios:

  1. No matching route (e.g., visiting /totally-random-url): NiceGUI/FastAPI already returns HTTP 404 — you're right about this.

  2. Route matches but content doesn't exist (e.g., /documentation/{name} where name is nonsense): The {name} wildcard catches the request, the page builder runs, renders the "not found" label, and returns HTTP 200 — because from the framework's perspective, the page builder succeeded.

@falkoschindler falkoschindler added the review Status: PR is open and needs review label Feb 19, 2026
@falkoschindler falkoschindler added this to the 3.10 milestone Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Type/scope: New or intentionally changed behavior review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants