Typical Project Setup
Now that you have installed the required software, it's time to create a basic FastAPI project. In this section, we'll guide you through creating a new project, configuring your development tools, and setting up a basic API structure.
Because FastAPI apps can be more complex (and Health Universe is primarily a deployment, not a development environment), it is best to have a local development environment that is linked to your GitHub repository.
Step 1: Create a GitHub Repository
Go to GitHub and create a new public repository. At this time, the repo must be public in order for Health Universe to be able to deploy your project.
Step 2: Clone Your Repository or Use Our Template
Clone Your Repository
git clone https://github.com/<your_account>/<your_repo_name>.git
Use Our Template
Start with our FastAPI Template, designed to work seamlessly with the Health Universe Navigator interface. It’s highly recommended, as it’s easy to customize and already optimized for deployment.
Click the green “Use this template” button (top right).
Select “Create a new repository”.
Give your new repository a name (e.g.,
tool-bmi-calculator
).Click “Create repository from template”.
Once it's created, clone your new repo to your local machine OR modify the script within Github.
Step 3: Set Up a Virtual Environment
Setting up a virtual environment for your Health Universe projects is required as it creates the requirements.txt file needed to specify your application's dependencies. To create a virtual environment, follow these steps:
Open a terminal and navigate to your desired project directory.
Create a new virtual environment with the following command:
python -m venv .env
Activate the virtual environment:
Windows:
.env\Scripts\activate
macOS and Linux:
source .env/bin/activate
Once activated, your terminal should display the environment name, e.g., (venv)
.
Step 4: Install FastAPI, Uvicorn, etc.
FastAPI is a high-performance framework for building APIs. Uvicorn is the ASGI server used to run FastAPI apps.. To install them, run the following command in your terminal:
pip install fastapi uvicorn pydantic python-multipart httpx
Wait for the installation to complete. FastAPI, Uvicorn, etc is now installed on your system and ready for use in your Health Universe projects.
Step 5: Edit App.py
Let's build a simple "Hello, World!" style API with input validation using Pydantic.
Open main.py
in your editor and add:
from fastapi import FastAPI
from pydantic import BaseModel
# Input schema using Pydantic
class GreetRequest(BaseModel):
name: str
# Create the app
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Welcome to Health Universe!"}
@app.post("/greet")
def greet_user(request: GreetRequest):
return {"message": f"Hello, {request.name}!"}
Save the main.py
file.
Step 6: Run the FastAPI App
To run your "Hello, World!" FastAPI app, open the terminal, navigate to your project directory, and run the following command:
uvicorn main:app --reload
Then open your browser and go to:
http://127.0.0.1:8000 to view the root endpoint.
http://127.0.0.1:8000/docs to test your API using Swagger UI.
To stop the app, press
Ctrl+C
in the terminal.
Step 7: Run pip freeze
From your terminal, run:
pip freeze > requirements.txt
This command will save all your project's dependencies into a requirements.txt file. This file is read by the Health Universe cloud to know what libraries your app will require.
Step 8: Commit Your Code and Push to GitHub
Step 9: Deploy Your App on Health Universe
Last updated
Was this helpful?