Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

Pass object argument through FastAPI

$
0
0

I have question about FastAPI, because I can't understand some stuff, and documentation is not clear for me.

I have example program:

import jsonimport argparsedef args():    parser = argparse.ArgumentParser("Some argumets")    parser.add_argument("--name", type=str)    parser.add_argument("--surname", type=str, nargs='?', default="Dan")    parser.add_argument("--birthday", type=str, nargs='?', default="1995")    args = parser.parse_args()    return argsclass Simple():    def create_df(self, name, surname, birthday):        x = {"name": name, "surname": surname, "bd": birthday}        x = json.dumps(x)        return xif __name__ == "__main__":    args_all = args()    s = Simple()    print(f"name: {args_all.name}, surname: {args_all.surname}, birthday:{args_all.birthday}")    print(s.create_df(args_all.name, args_all.surname, args_all.birthday))

I can run it by:

python app.py --name=Matt --surname=Dan  #And let's say that birthday is 1995 is default one.

And got working string:

Matt Dan 1995

And now I want that expose it by FAST API for users. It could create request like:

www.api_adress.domain/names?name=Anthony&birthday=33

expect result: run function with arguments:

name=Anthony, surname=Dan, birthday=33

So as you can see I need to have some default values but could be change by user.

My api_code:

from fastapi import FastAPIfrom app_code import *app = FastAPI()o = Simple()args = args()name = args.namesurname = args.surnamebirthday = args.birthdayprint(args)@app.get("/names")async def root(name=args.name, surname=args.surname, birthday = birthday.args.birthday):    print("In progress...")    result = Simple.create_df(name=name, surname=surname, birthday=birthday)    return result

And of course I tried many configurations (and still looking for solution), but still got error like:

←[33mWARNING←[0m:  The --reload flag should not be used in production on Windows.usage: Some argumets [-h] [--name NAME] [--surname [SURNAME]] [--birthday [BIRTHDAY]]Some argumets: error: unrecognized arguments: main_api:app --reload

I have some problems to understand that, and trying to figure it out. How to pass arguments like normal program with some default values? So if somebody could explain me it, I will be grateful!

I also tried changing my API to:

@app.get("/names")async def root(name: str, surname: str = "Dan", birthday: str = "2020"):    print("In progress...")    result = Simple.create_df(name=name, surname=surname, birthday=birthday)    return result

And got the following error:

←[32mINFO←[0m:     Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)←[32mINFO←[0m:     Started reloader process [←[36m←[1m20260←[0m] using ←[36m←[1mwatchgod←[0m←[33mWARNING←[0m:  The --reload flag should not be used in production on Windows.usage: Some argumets [-h] [--name NAME] [--surname [SURNAME]] [--birthday [BIRTHDAY]]Some argumets: error: unrecognized arguments: main_api:app --reload

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>