3

I'm trying (failing) to set up a simple FastAPI project and run it with uvicorn. This is my code:

from fastapi import FastAPI

app = FastAPI()

app.get('/')

def hello_world():
    return{'hello':'world'}

app.get('/abc')

def abc_test():
    return{'hello':'abc'}

This is what I run from the terminal:

PS C:\Users\admin\Desktop\Self pace study\Python\Dev\day 14> uvicorn server2:app   
INFO:     Started server process [3808]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:60391 - "GET / HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:60391 - "GET /favicon.ico HTTP/1.1" 404 Not Found

As you see, I get a 404 Not found. What could be the reason? Some network-related stuff, possibly firewall/vpn blocking this connection or something else? I'm new to this. Thanks in advance!

Janet
  • 101
  • 1
  • 1
  • 9
  • 1
    You need *decorate* the router using ***`@app.get(...)`***, not just `app.get()` – JPG Jan 06 '21 at 16:53

2 Answers2

5

By now you'd probably have figured it out. In order to get MWE running, you'd use the microservice's endpoint decorators before each function definition. The following snippet should get your issue solved. It assumes that you have the following structure:

.
+-- main.py
+-- static
|   +-- favicon.ico
+-- templates
|   +-- index.html
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

templates = Jinja2Templates(directory="templates")

@app.get('/')
def hello_world():
    return{'hello':'world'}

@app.get('/favicon.ico')
async def favicon():
    file_name = "favicon.ico"
    file_path = os.path.join(app.root_path, "static", file_name)
    return FileResponse(path=file_path, headers={"Content-Disposition": "attachment; filename=" + file_name})

@app.get('/abc')
def abc_test():
    return{'hello':'abc'}

So you'd be all set to run your first app using the FastAPI default ASGI server.

(env)$: uvicorn main:app --reload --host 0.0.0.0 --port ${PORT}

Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
Miguel Rueda
  • 478
  • 1
  • 6
  • 13
4

You need to use a decorator like this: @app.get('/'). Take a look at the FastAPI Docs.

Additionally, take a look at how decorators work in general to get a better idea of how things work behind the scenes.

Some resources for you:

python docs

one of many articles I was able to find

another SO question

istrupin
  • 1,423
  • 16
  • 32
  • Thanks. That was the issue. – Janet Jan 07 '21 at 11:53
  • Something else, so that I don't open another question - if you happen to know - I am asked every time after I restart the terminal to install fastAPI, uvicorn and pipenv again, otherwise it behaves like they don't exist....? Why would that be? Visual Studio Code, Python 3.8 – Janet Jan 07 '21 at 14:17
  • That kind of sounds like a virtual environment issue to me, where VSCode or your terminal are not picking it up correctly. Are you using the VSCode integrated terminal? Maybe look into setting up VSCode for a python environment [here](https://code.visualstudio.com/docs/python/environments). I think that should at least get you in a decent spot to do some more research, after which I think another question would be appropriate if you aren't able to find anything. Since you're using pipenv, VSCode should integrate with that nicely and see everything that you've installed via `pipenv install`. – istrupin Jan 07 '21 at 15:41
  • Yep this is exactly how VSCode is set up to use Python (because I set it up a few weeks ago using the same guide as the link you pasted) and I use the VSCode terminal. Anyway thanks for your help mate I'll keep digging and post if I find out what the issue was. Cheers. – Janet Jan 07 '21 at 19:10