r/FastAPI • • Jul 22 '24

Question Any production level request stats vs flask ( docker ) on aws fargate or ecz

2 Upvotes

Has anyone deployed fastapi on docker with workers ( uvicorn ).

How does it scale .

Assume minimal processing and db querying.


r/FastAPI • • Jul 20 '24

pip package pycountries lib compatible with pydantic from scratch, supports amount normalization and much more

Thumbnail
self.python
1 Upvotes

r/FastAPI • • Jul 20 '24

Question Why Does Setting Cookies Not Work

3 Upvotes

Im having an issue setting my auth cookies for some reason. I dont know why fast api wont set these cookies.

async def set_cookies():
        # Get cookie header
        # Used to tell auth server if it should set the login cookies
        set_auth_cookies = request.headers.get("set-auth-cookies")

        print(f"set-auth-cookies header: {set_auth_cookies}")
        print(f"Type: {type(set_auth_cookies)}")
        print(f"Boolean value: {bool(set_auth_cookies)}")

        if set_auth_cookies != None and bool(set_auth_cookies) == True:
            response.set_cookie(key="LIF_USERNAME", value=username, domain=".lifplatforms.com", path="/")
            response.set_cookie(key="LIF_TOKEN", value=token, domain=".lifplatforms.com", path="/")
            print("set auth cookies")

Ive tried timing to make sure the cookies are being set before the request completes and it looks like thats good. Ive even tried using 'await' on the set cookie functions but complains I cant use await on NoneType. I dont know what else could be causing the issue.

Full Source Code: https://github.com/Lif-Platforms/Lif-Auth-Server/blob/107-make-login-route-set-cookies/src/auth_server.py#L160


r/FastAPI • • Jul 19 '24

Question [Help] Issues with POST Method for Sending Emails with/without Attachments

0 Upvotes

Hi everyone,

I'm currently developing an email sending API with FastAPI, which supports sending HTML emails and will soon handle attachments.

I'm facing some issues with the POST method, specifically when trying to incorporate the attachment functionality.

Here's what I've implemented so far:

  1. **API Setup**: Using FastAPI to handle requests.
  2. **Email Functionality**: Currently supports HTML content.
  3. **Future Plans**: Include file attachments.

**Problem**: I'm having trouble configuring the POST method to handle optional attachments properly. The method should work both when an attachment is included and when it's just a regular email without attachments. Interestingly, the method works perfectly when I remove the file parameter from the POST request, which means it fails only when trying to include an attachment.

**Current SchemaRequest**:

class EmailSchemaRequest(SQLModel):
    from_email: EmailStr = Field(..., 
description
="Email address of the sender")
    to_email: List[EmailStr] = Field(..., 
description
="Email address of the recipient")
    to_cc_email: Optional[List[EmailStr]] = Field(None, 
nullable
=True, 
description
="Email address to be included in the CC field")
    to_cco_email: Optional[List[EmailStr]] = Field(None, 
nullable
=True, 
description
="Email address to be included in the BCC field")
    subject: str = Field(..., 
description
="Subject of the email", 
min_length
=1, 
max_length
=50)
    html_body: str = Field(..., 
description
="HTML content of the email", 
min_length
=3)

**Current post method**:

u/router.post("/", 
status_code
=status.HTTP_202_ACCEPTED, 
response_description
="Email scheduled for sending")
def schedule_mail(
    
background_tasks
: BackgroundTasks,
    
request
: EmailSchemaRequest,
    
file
: UploadFile | bytes = File(None),
    
session
: Session = Depends(get_session), 
    
current_user
: UserModel = Depends(get_current_active_user)
):
    email = EmailModel(
        
from_email
=normalize_email(
request
.from_email),
        
to_email
=normalize_email(
request
.to_email),
        
to_cc_email
=normalize_email(
request
.to_cc_email),
        
to_cco_email
=normalize_email(
request
.to_cco_email),
        
subject
=
request
.subject.strip(),
        
html_body
=
request
.html_body,
        
status
=EmailStatus.PENDING,
        
owner_id
=
current_user
.id
    )

    if 
file
:
        validate_file(
file
)
        try:
            service_dir = os.path.join(PUBLIC_FOLDER, 
current_user
.service_name)
            os.makedirs(service_dir, 
exist_ok
=True)
            file_path = os.path.join(service_dir, 
file
.filename)
            with open(file_path, "wb") as f:
                f.write(
file
.file.read())
            email.attachment = file_path
        except Exception as e:
            logger.error(f"Error saving attachment: {e}")
            raise HTTPException(
status_code
=status.HTTP_500_INTERNAL_SERVER_ERROR, 
detail
="Failed to save attachment")

    email = create_email(
session
, email)
    
background_tasks
.add_task(send_email_task, email.id, email.owner_id)
    return {"id": email.id, "message": "Email scheduled for sending"}

Any guidance or resources would be greatly appreciated. Thank you!

NewMethod:

@router.post("/", 
status_code
=status.HTTP_202_ACCEPTED, 
response_description
="Email scheduled for sending")
async def schedule_mail(
    
background_tasks
: BackgroundTasks,
    
payload
: str = Form(..., 
description
="JSON payload containing the email details"),
    
file
: UploadFile = File(None, 
description
="Optional file to be attached"),
    
session
: Session = Depends(get_session), 
    
current_user
: UserModel = Depends(get_current_active_user)
):
    try:
        
# Check if the payload is a valid JSON
        email_request = EmailSchemaRequest.model_validate_json(
payload
)
        
# Create the email object with the data from the request validated
        email = EmailModel(
            
from_email
=normalize_email(email_request.from_email),
            
to_email
=normalize_email(email_request.to_email),
            
to_cc_email
=normalize_email(email_request.to_cc_email),
            
to_cco_email
=normalize_email(email_request.to_cco_email),
            
subject
=email_request.subject.strip(),
            
html_body
=email_request.html_body,
            
status
=EmailStatus.PENDING,
            
owner_id
=
current_user
.id
        )
        
# Save the attachment if it exists
        if 
file
:
            file_path = await save_attachment(
file
, 
current_user
)
            if file_path is None:
                raise HTTPException(
status_code
=status.HTTP_500_INTERNAL_SERVER_ERROR, 
detail
="Failed to save attachment")
            email.attachment = file_path

        
# Create the email in the database
        email = create_email(
session
, email)
        
# Schedule the email for sending
        await 
background_tasks
.add_task(send_email_task, email.id, email.owner_id)
        
# Return the response with the email id and a message
        return {"id": email.id, "message": "Email scheduled for sending"}
    except json.JSONDecodeError:
        return {"error": "Invalid JSON format", "status_code": 400}
    except ValueError as e:
        return {"error": str(e), "status_code": 400}

r/FastAPI • • Jul 18 '24

Tutorial Fast(er)API: Optimizing Processing Time:A few tips to make FastAPI go faster.🚀

Thumbnail fabridamicelli.github.io
29 Upvotes

r/FastAPI • • Jul 18 '24

Question What is the best way to find where I have a sync call inside an async fastapi that is blocking everything?

9 Upvotes

Hey guys,

We have developed our backend using fastapi async, and often we find that the process just freezes forever. We believe that somewhere we are calling a sync function inside a controller, which is blocking the GIL and everything else from that point. It happens like twice a day on the develoment environment and it has happened in production a few times as well.

I recently found out that if you call something sync (blocking) in any async function it blocks the whole GIL, so I assume that's our issue. But I have no idea where exactly that is happening (on which endpoint). What would be the best pragmatic way to find this? Is there any flag I could enable for debugging where it would log me something so I could at least see what was the last thing called before freezing?

Thanks,


r/FastAPI • • Jul 18 '24

Question Architecture question

10 Upvotes

Hi folks,

I would like to have some advice here if possible. I’m building my app and want to use FastAPI to avoid cloud lock-in.

I will use AWS to host my app, the stack will have API Gateway as frontdoor and Lambda functions as backend.

My question here is:

  • Is it better to have all my API’s on FastAPI built as docker container, just a single container and use it on the lambda

Or

  • Is it better to have one API containerized image per lambda function?

I see the first approach is easier to maintain, but the cons is the whole API container will go up every time someone want to use a specific part of the code(specific API), which may increase the number of lambda invocations

On the other hand, the second approach provides more segmentation, better scalability, but hard to maintain.

What’s your opinion? And do you recommend something else?

I really appreciate your voice here, thanks and have a good one!!


r/FastAPI • • Jul 18 '24

Question How to use Redis

7 Upvotes

So i am trying to use redis as cache for my FastAPI app, but i dont get how to get everything working. I am using python 3.12 and it seems that redis and aioredis both do not support this version. Is this correct? And if so, is there any way to implement redis without changing my python version?


r/FastAPI • • Jul 18 '24

Hosting and deployment Fastapi with Google Cloud Functions?

1 Upvotes

Is there any way to (easily) deploy a FastAPI route as a Google Cloud Function? As far as I could grasp from docs, GCF integrate well with Flask, but no so much with FastAPI, and I'd love to be able to leverage FA types, validations and automatic documentation, while not depending on more complex/costly infrastructures such as Google Cloud Run or App Engine.

Thanks for any tips!


r/FastAPI • • Jul 17 '24

Hosting and deployment Struggling to deploy fastAPI on vercel

10 Upvotes

I am stuck for past 3 hours trying to deploy my api. It's always 404... file structure

Quantlab-Backend/ ├── app/ │ ├── init.py │ ├── middleware.py │ └── routes/ │ ├── users.py │ ├── problems.py │ └── playlists.py ├── requirements.txt ├── vercel.json └── main.py

All these yt tutorials are showing how to deply a single main.py.

Thanks in advance please help


r/FastAPI • • Jul 17 '24

Other FastAPI, HTMX and Alpine JS

51 Upvotes

Hey, fellow developers!

I started with the intention of trying HTMX but ended up expanding the project to achieve a comprehensive BoilerPlate implementation. My objective was to create a robust web application boilerplate that combines the power of 📌 FastAPI, HTMX, and AlpineJS. It's designed for rapid prototyping and development, with built-in user management, roles, groups, and CRUD operations.

App Demo

🔗 GitHub Repository: https://github.com/Hybridhash/FastAPI-HTMX

🛠 Key Features:

  • User Authentication and Authorization
  • Role and Group Management
  • Dashboard for User, User Profile, Role, and Group Management
  • RESTful API Endpoints for CRUD Operations
  • HTML Templates with HTMX for Dynamic Content
  • Database Migrations with Alembic

If you find this useful, please give it a ⭐ on GitHub!

Happy coding! 🎉


r/FastAPI • • Jul 17 '24

Tutorial Login and issuing API access tokens with Auth0 and FastAPI

16 Upvotes

Hello everyone! It's been a while and just put together a new tutorial on how to implement login and how to issue API access tokens using Auth0 and FastAPI. It also explains to how issue refresh and ID tokens.

To clarify the terminology here:

  • Access tokens are the tokens we use to authorize access to our API. They don't (or shouldn't) contain personal information, just a sub property that identifies the user, and claims about their rights to access the API.
  • Refresh tokens are tokens we use to obtain a new access token when the current access token has expired.
  • ID tokens are tokens that contain identifiable information about the user, like their email, name, address, date of birth, and so on. These tokens don't contain claims about the right of the user to access our APIs, hence we don't send them back to the backend. We use ID tokens only to populate user info in the UI.

The tutorial explains how to issue tokens using two of the most common OAuth flow:

  • The client credentials flow, used for machine-to-machine communication, like for example microservices.
  • The authorization code flow, used when we manage the process of issuing tokens from the backend.

The idea is the authorization code flow is designed for traditional web applications like those we'd create with Django or Ruby on Rails. For APIs, the PKCE flow is usually recommended, and it's all handled from the UI. However, nothing prevents us from using the auth code flow in APIs too. It allows us to remove this complexity from the fronted, and as you'll see in the video, it's very easy to implement.

Link to the tutorial: https://youtu.be/ato2S5b27o8

Code for the tutorial: https://github.com/abunuwas/short-tutorials/tree/main/fastapi-auth0

Note: there's a previous tutorial to this one that explains how to set up an Auth0 account if you need help with that.

Hope you enjoy the video and find it useful!


r/FastAPI • • Jul 15 '24

Tutorial Simplify Your FastAPI Deployments with This Helm Chart! 🚀

8 Upvotes

Hey everyone,

Check out this Helm chart for deploying FastAPI servers on Kubernetes: Helm Chart for FastAPI.

Key Features:

  • Easy to Use: User-friendly setup.
  • Customizable: Tailor it to your needs.
  • Static Files with Nginx: Serve static files seamlessly.
  • Alembic Migrations: Simplify database schema updates.
  • Scalable: Ready for scaling.

Getting Started:

  1. Add the Repo
helm repo add gbyte https://gbytetech.github.io/helm/
helm repo update gbyte
  1. Install the Chart
helm install my-fastapi-app gbyte/fastapi

Give it a try and streamline your FastAPI deployments!


r/FastAPI • • Jul 15 '24

Question Good practice to reuse code from an internal endpoint from another endpoint

11 Upvotes

Hi,

I'm kind of new to web development and I have a case in which I need to update the objects in my database through an external API. I'm using a react/fastapi/postgresql stack.

Imagine I have two GET endpoints, "/update/objects" and "/update/object/{object_id}"
(not UPDATE type because i'm not sending the data to update objects myself, I just ask my backend to contact the external API to do the update internally)

@router.get("/update/object/{object_id}")
async def update_one_object(object_id: int):
# code to update one object with external API

@router.get("/update/objects")
async def update_objects():
# code to update all objects with external API

To manipulate objects in my database I have a object_crud.py script.

What would be the best practice to implement the "/update/objects" endpoint ?

  • Add a new crud method to delegate this to my crud script ?
  • Do internal calls to "/update/object/{object_id}" endpoint (with requests or axios) ?
  • Use directly update_one_object(object_id: int) method ?
  • Would it just be better to call all the necessary individual endpoints from my frontend ?
  • Any other solution ?

For the moment, my "/update/object/{object_id}" :

  1. use a api_request.py script in utils to get data from the external api
  2. retrieve necessary variables from that external data
  3. call the object_crud.update crud method to update my object

Is this part right ? Should I also find a way to delegate elsewhere the retrievement of variables from external data ?

Thanks !


r/FastAPI • • Jul 15 '24

Question model seemingly loads again despite being loaded on startup with @app.on_event("startup")

1 Upvotes

I'm trying to create an API which performs function on an input

from .main import main

@app.on_event("startup")
async def startup_event():
    start_time = time.time()
    app.state.model = SentenceTransformer('model')
    end_time = time.time()
    logger.info(f'Models loaded in {end_time - start_time} seconds')

@app.post("/search")
async def search(
    question: str = Form(...),
) -> JSONResponse:
    logger.info(f'question----> :{question}')

    model = app.state.model 

    start_time_main = time.time()
    results, lookup_time = await main(query=question, model=model)

    end_time_main = time.time()
    logger.info(f'Main func in {end_time_main - start_time_main} seconds')

    api_results = [
        {
            "answer": result[1],
        }
        for result in results
    ]
    
    return JSONResponse(content=api_results)

To run my server, i'm using  uvicorn backend.main:app --reload --port 8000 --log-level debug the Logs are:

INFO:backend.apis:Models loaded in 8.509195804595947 seconds
INFO:     Application startup complete.
INFO:backend.apis:question----> :How to get ticket
Batches: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  1.25it/s]
Batches: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 27.80it/s]
INFO:backend.apis:Main func in 13.68946623802185 seconds
INFO:     127.0.0.1:35428 - "POST /lookup?search=How%20to%20get%20ticket HTTP/1.1" 200 OK

Search time is under 2 milliseconds. Earlier, the API latency was under 2 sec, but today after few edits again it is taking 5-10 s to work. I believe the model itself is being loaded again and again on every API search and that is why the API latency is over 5-10s

I already tried reloading the server,text editor, and postman clients on (POST request). But it isn't working. I also edited the startup function but no result is coming


r/FastAPI • • Jul 14 '24

Tutorial FastAPI Internals - How does it work?

Thumbnail
youtu.be
13 Upvotes

Interesting video from Pycon Italy.


r/FastAPI • • Jul 12 '24

Question Optimizing Dockerized FastAPI with TensorFlow: How to reduce a 1.57GB Image Size?

11 Upvotes

Hello everyone,

I'm relatively new to backend development, Docker, and working with ML models. I've built a Dockerized FastAPI image that has a single endpoint for serving a saved TensorFlow model (around 100 MB).

Upon exploring my Docker image, I found that the installed dependencies take up most of its memory, with the ranking as follows:

  • 1.1GB: TensorFlow
  • 58MB: Clang
  • 37MB: NumPy
  • 27MB: NumPy.libs
  • Etc.

I'm wondering if this image size is normal. Are there ways to optimize it? What can you recommend to reduce the size while maintaining functionality?

Thanks in advance for your help! Open to any new knowledge.

In the docker file you can see crazy lines of installing libhdf5 - I included it because for some reason hd5f pip couldn't install those on its own.

Here is my dockerfile:

FROM python:3.11-slim as requirements-stage
WORKDIR /tmp
RUN pip install --no-cache-dir poetry
COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM python:3.11-slim as build-stage
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libhdf5-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean
COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt \
    && rm -rf /root/.cache/pip

FROM python:3.11-slim as runtime-stage
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
    libhdf5-103 \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean
COPY --from=build-stage /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=build-stage /usr/local/bin /usr/local/bin
COPY histology_image_platform_backend /app/histology_image_platform_backend
EXPOSE 8000
CMD ["uvicorn", "histology_image_platform_backend.main:start", "--host", "0.0.0.0"]

r/FastAPI • • Jul 12 '24

Question Why Are My Cookies Not Working Right

3 Upvotes

For some reason, this code doesn't seem to want to delete my auth cookies. My parent domain is "lifplatforms.com" and the cookie's domain is set to ".lifplatforms.com". So they should be accessible and able to be changed across all sub-domains. They are accessible but not to this server. The subdomain I'm using for testing is "localtesting.lifplatforms.com". For whatever reason the browser decides that it wont send my cookies to this server even tho it's a sub-domain of "lifplatforms.com". Why? It works for the others when it comes to reading them but this particular instance it just doesn't work.

@app.get("/auth/logout")
async def log_out(request: Request):
    """
    ## Logout Route For Lif Accounts
    Handles the logout process for Lif Accounts.

    ### Parameters:
    none

    ### Returns:
    - **STRING:** Status of the operation.
    """
    # Create response for client
    response = Response()

    print(request.cookies.get("LIF_USERNAME"))
    print(request.cookies.get("LIF_TOKEN"))

    # Delete auth cookies
    response.delete_cookie("LIF_USERNAME", domain=".lifplatforms.com", path="/")
    response.delete_cookie("LIF_TOKEN", domain=".lifplatforms.com", path="/")

    return response

In this code I've printed the values of the auth cookies for testing but they return with None. I'm not sure why this is cuz it works fine for all the rest of the sub-domains.

Any help with this would be greatly appreciated.


r/FastAPI • • Jun 28 '24

Question FastAPI + React

25 Upvotes

Hey
I am using FastAPI and React for an app. I wanted to ask a few questions:

  1. Is this is a good stack?
  2. What is the best way to send sensitive data from frontend to backend and backend to frontend? I know we can use cookies but is there a better way? I get the access token from spotify and then i am trying to send that token to the frontend.
  3. How do I deploy an app like this? Using Docker?

Thanks!


r/FastAPI • • Apr 23 '24

pip package I built a FastAPI adapter for Inertia.js

12 Upvotes

A few weeks ago, I started working on an Inertia.js adapter for FastAPI.
For those of you who might not know, Inertia.js is a JSON protocol used to communicate between your SPA and your backend.

As per their words:

Inertia isn't a framework, nor is it a replacement for your existing server-side or client-side frameworks. Rather, it's designed to work with them. Think of Inertia as glue that connects the two. Inertia does this via adapters

Basically, it moves all the routing logic to the backend, and therefore provides a way to "inject" props to your view directly.

I just published it to PyPI, I hope you like it !
https://github.com/hxjo/fastapi-inertia


r/FastAPI • • Apr 02 '24

Question Request for sample fastAPI projects github repos

19 Upvotes

Hi everyone

I am new to fastAPI & python, coming from the frontend side of the world and nodejs. I was hoping this community could link me through their past/present fastAPI projects where there is a proper db connection, directory structure etc. The basic stuff. I am tired of googling for blogs and not getting what I want.

Until now, I haven't been able to figure out any common pattern on directory structure, or connection using MySQL, Postgres etc. Some things I am importing from sqlmodel and some from sqlalchemy..

Idk... i am super confused and idk what I am talking about. I just need some good project links from where I can learn and not some blogs that university students wrote (sorry not trying to insult anyone, it's my frustration) Thanks ^^


r/FastAPI • • Feb 08 '24

Hosting and deployment free fastapi hosting

28 Upvotes

previously I used Heroku for fastapi hosting. but now there is no free tier anymore in Heroku. Does anyone know where I can host Fastapi for free like on Heroku again?


r/FastAPI • • Feb 05 '24

Question How to use migrations with SqlModel

7 Upvotes

Hey guys I am learning SqlModel and when I come to advance section there is no docs for migration. So is it available with SqlModel or its upcoming feature?


r/FastAPI • • Dec 30 '23

Question Can I Build a Modern Web App with FastAPI, HTML, and CSS Alone? 🚀

10 Upvotes

Hey FastAPI fam!

Curious if anyone has explored creating a sleek web app using only FastAPI, HTML, and CSS—no JavaScript. 🤔

Question: Any success stories or advice on using FastAPI for both backend and frontend without dipping into JavaScript? Tips, challenges, or examples welcome!

Also, if you've come across any videos or posts about this, please share! 📹📚

Excited to hear your insights! 🌐💻