Python
Preparing Your Python Code with FastAPI
Before uploading your FastAPI application, ensure it's properly prepared. Here’s how you can do this, following our platform's requirements.
Here is an example Python Sample
Selecting Your Language
Ensure your project is developed in Python.
Packaging Your Project
For a FastAPI application, follow these steps to package your project:
my_project/
│
├── main.py # Your main FastAPI application file.
│
├── requirements.txt # Lists all the dependencies for your project.
│
└── additional_files/ # Optional: Any additional files or directories.
├── models.py # Optional: Defines data models.
└── dependencies.py # Optional: Contains any dependency functions.
- Main File: Your main FastAPI file (e.g.,
main.py
) should be at the root of your package. This file contains your application instance and routes. - Dependencies: Include a
requirements.txt
file that lists FastAPI and any other dependencies. Yourrequirements.txt
might look something like this: - Compression: Compress your project directory into a
.zip
or.sqsh
file. Make sure the compression includes themain.py
file andrequirements.txt
.
Defining the Entry Point
For FastAPI projects, the entry point is crucial for our platform to know how to start your application. If your main file is main.py
:
- Entry Point: Your entry point would be
main:app
, wheremain
is the filename (without.py
) andapp
is the FastAPI application instance.
Example Code Explanation
Here is the explanation of your code snippet:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
- FastAPI Instance:
app = FastAPI()
creates an instance of a FastAPI application. - Route Definition:
@app.get("/")
defines a route that listens for GET requests at the root URL (/
). - Async Function:
async def root():
is an asynchronous function that handles requests to the root URL. It returns a JSON response with a message. - JSON Response:
{"message": "Hello World"}
is the JSON response that clients will receive when they access the root URL.
Uploading Your Code
After preparing your code, follow the platform's upload process:
- Navigate to Code Upload: Go to the code upload section by creating a new function on our platform.
- Choose File: Select your
.zip
or.sqsh
file containing the FastAPI project. - Select Language: Choose Python as the language.
- Specify Entry Point: Enter
main:app
as the entry point. - Upload: Complete the upload process.
By following these steps and ensuring your FastAPI application is correctly prepared, you'll facilitate a seamless integration.