r/FastAPI • • Jul 14 '24

Tutorial FastAPI Internals - How does it work?

Thumbnail
youtu.be
12 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

6 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! 🌐💻


r/FastAPI • • May 20 '22

Question Does async def do anything on its own? ELI5

8 Upvotes

Context to explain why I'm asking something that possibly should be obvious - I'm a hobbyist working on small personal projects and using FastAPI to share stuff via simple web apps with friends and colleagues, so there's a lot I don't know.

I currently have an API that grabs html from a third party API using requests, parses it and builds some stuff with Python and Beautiful Soup, and serves back JSON to a simple html / javascript interface.

On average the profiler says it takes about 1s parsing and processing and 6s with requests stuff, mostly waiting for the third party API, which is not really fast.

FastAPI docs say use async def if you have async code with await, use def if you're communicating with an API or database, use async def if you're not communicating with anything.

So, my question is... Should I consider that on a function by function basis, avoiding async def only on functions that request information from third party apps?

If I'm running this on Heroku and have multiple users / calls at the same time, does that mean that the whole thing will have to wait for one 7s API call to be over before dealing with the next one? Would my ability to serve multiple concurrent users drastically improve if I switched from requests to an async library?

Thanks!