FastAPI add additional argument to running Uvicorn
Asked Answered
B

2

8

I'm currently starting up the server with the following Uvicorn command:

main:app --host 0.0.0.0 --port 8003 --access-log

And I would like to add an extra argument --foo such that it works as argparse's "store_true" action so I can optionally execute a function during my startup.

With Python, using argparse I can achieve this executing the command main.py --migrate:

parser = argparse.ArgumentParser(description="Startup")
parser.add_argument("--foo", dest="run_foo", action="store_true")
args = parser.parse_args()

@app.on_event("startup")
async def startup_event():
    if args.run_foo:
        foo()

Where app is my FastAPI instance. However, I get an uvicorn: error: unrecognized arguments: main:app --host 0.0.0.0 --port 8003 --access-log when I try to execute it using Uvicorn. Is there a way to do this?

Brena answered 29/11, 2022 at 17:1 Comment(3)
Can you please explain more what you are asking? It sounds like you want to extend the Uvicorn CLI by using argparse...? Is that right?Fca
@Fca yes and no. I posted my Python solution which uses argparse, but I don't know if something similar could be done in Uvicorn. So yes, I imagine this would be considered expanding the CLI.Brena
I'm looking for the same thing... Using environment variables can be a workaroundCalling
W
4

You can do a trick by calling your service in python and using argparse as:

import uvicorn
from fastapi import FastAPI
import argparse
app: FastAPI = FastAPI()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--foo')
    args = parser.parse_args()
    foo(args.foo)
    #run server
    uvicorn.run(app)

Then run you server using normal python command and pass the argument:

python main.py --foo foo
Whinchat answered 14/8, 2023 at 16:21 Comment(1)
abvious not op want to do....Antagonism
B
1

Another option is to define the environment variables of uvicorn app and get them through Python. I recommend this amazing tutorial that will explain it better than me. But if you are lazy to read or prefer a TL;DR explanation, here it goes:

You can configure environment variables with ADMIN_EMAIL="[email protected]" APP_NAME="ChimichangApp" uvicorn main:app (for example). And then get the variable from your Python app using:

import os
name = os.getenv("ADMIN_EMAIL")
print(f"The email is {ADMIN_EMAIL}")

However, this are not best practices. First of all, it would be better to define the env variables with a *.env file, and use it with uvicorn main:app --env-file <my-filename>.env. This env file will look like this:

ADMIN_EMAIL="[email protected]"
APP_NAME="ChimichangApp"

Secondly, it would be better to use pydantic to get the env variables.

Baseline answered 27/3 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.