Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🧑‍💻 Add conditional printing API docs URLs in panel #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FlavienRx wants to merge 5 commits into fastapi:main
base: main
Choose a base branch
Loading
from FlavienRx:docs-conditional-panel

Conversation

Copy link
Contributor

@FlavienRx FlavienRx commented Nov 9, 2024
edited
Loading

Description:

This PR refactors the FastAPI server start-up logic to improve the display of the application’s documentation URLs in the console. It introduces a conditional check for app.openapi_url, ensuring that only defined documentation URLs are printed. The updated logic adds more flexibility to display either docs_url, redoc_url, or both, enhancing visibility for users when serving the API.

Changes:

  • Updated import handling with get_import_string_and_app to retrieve both import_string and app for improved compatibility with uvicorn.
  • Enhanced serving_str output formatting to conditionally display API documentation URLs based on availability.
  • Cleaned up conditional print logic to handle cases where only one or both of docs_url and redoc_url are available.

This adjustment improves user experience by making API documentation links visible and accessible only when they are set.

Examples:
image
image
image

Stargator and eden881 reacted with thumbs up emoji
Copy link

hi
i had the same issue with custom docs urls showing wrong url in the logs,
i think the latest version of the fastapi_cli code is changed from when you worked this on it,
cli module now uses ImportData to get import string and other app module data
i think it's better to add the fastapi app as a field to ImportData and use it on cli module instead of returning a tuple

Copy link
Contributor Author

Hi @sehraramiz

You right, I started to refactor my code but I run out of time to finish and push my code 😄

sehraramiz reacted with rocket emoji

Copy link
Contributor Author

@sehraramiz

I did the job, let me know what do you think.

@tiangolo, what do you think about this feature ?

Stargator reacted with thumbs up emoji

Copy link

A new label has to be added:

Label check failed: required 1 of 'breaking', 'security', 'feature', 'bug', 'refactor', 'upgrade', 'docs', 'lang-all', 'internal', but found 0.

Copy link

This resolves #162

Copy link

eden881 commented Aug 3, 2025

Would this work if I disable the URLs entirely?
In my case I depend on an env var:

app = FastAPI(
 lifespan=lifespan,
 docs_url="/docs" if DEV_MODE else None,
 redoc_url="/redoc" if DEV_MODE else None,
 openapi_url="/openapi.json" if DEV_MODE else None
)

Copy link
Contributor Author

@eden881 Yep, I had some tests to test this case.

eden881 reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /