How to run fastapi from terminal?

Member

by jasen , in category: Javascript , 4 months ago

How to run fastapi from terminal?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , 4 months ago

@jasen 

To run a FastAPI application from the terminal, you can follow these steps:

  1. Make sure you have FastAPI installed in your Python environment. You can install FastAPI using pip:
1
pip install fastapi


  1. Create a Python script with your FastAPI application code. For example, create a file named app.py with the following code:
1
2
3
4
5
6
7
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}


  1. Now, you can run your FastAPI application by executing the following command in the terminal:
1
uvicorn app:app --reload


This command starts the Uvicorn server and loads your FastAPI application located in the app.py file. The --reload flag will automatically restart the server whenever you make changes to your code.

  1. After running the command, you should see output similar to the following:
1
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)


  1. You can now open a web browser and go to http://127.0.0.1:8000 to see your FastAPI application running.


That's it! You have successfully run a FastAPI application from the terminal.

Related Threads:

How to run a script on server using fastapi?
How to run fastapi on apache2?
How to run fastapi app on multiple ports?
How to clear the redis terminal?
How to change git terminal credentials in bitbucket?
How to prevent html injection in fastapi?