question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Choices Dropdown Menu in API Documentation

See original GitHub issue

Feature Request:

It would be nice to document in the API, which what choices are available to the user as a drop-down menu in the UI. It helps prevent such documentation in the description\help of the parameter.

Context flask_restplus library is able to show choices in its Swagger UI, and is able to handle if incorrect choice is provided as an input by the user.
Selection_005

Preferred Solution: Would like to see choices parameter added to possibly fastapi.Query class and possibly other classes that handle input from user. Would be awesome if it was something like this

@api.get('/get_countries')
def get_something(
    choice: str = Query('eu', choices=('eu,'us','cn,'ru'), description='foo bar')
):
    do_something(choice)

Would be nice to see this feature in FastAPI, as I/We would prefer to use FastAPI over flask_restplus.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

36reactions
euri10commented, May 22, 2019

after some digging it seems it works out of the box:

Imgur

import uvicorn

from fastapi import FastAPI, Query

app = FastAPI()


@app.get('/get_countries')
async def get_countries(_q: str = Query("eu", enum=["eu", "us", "cn", "ru"])):
    return {"selected": _q}


if __name__ == '__main__':
    uvicorn.run("test_swagger_dropdown:app", reload=True)
30reactions
tiangolocommented, Jan 19, 2020

Hey @alikhtag, just to let you know, this is available now in the latest versions (actually for some time now).

Here are docs for how to use enums in paths, but the same applies to queries: https://fastapi.tiangolo.com/tutorial/path-params/#predefined-values

Here’s your same example:

from fastapi import FastAPI
from enum import Enum


class Country(str, Enum):
    eu = "eu"
    us = "us"
    cn = "cn"
    ru = "ru"


app = FastAPI()


@app.get("/")
def get_something(country: Country = Country.eu):
    return {"country": country.value}
Read more comments on GitHub >

github_iconTop Results From Across the Web

API to Create or Update dropdown list - Typeform Community
I'd like to update a dropdown part of my form with a list of today+7 to today+30 every night. Currently it looks like....
Read more >
Dropdown | Dash for Python Documentation | Plotly
This is an example on how to update the options on the server depending on the search terms the user types. For example...
Read more >
Dropdown Component - Appian 21.2
Function. Displays a list of choices for the user to select one item and saves a value based on the selected choice. If...
Read more >
Dropdown Menu - Symphony Developers Documentation
A dropdown menu is a static list of items that appears whenever a piece of text or a button is clicked. This is...
Read more >
Define option lists - Retool Docs
Many components present a list of options, such as the dropdown in a Select or the menu of a Split Button. Each option...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found