piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

Is there an example of how to integrate piccolo into an existing fastapi application?

Open ohmeow opened this issue 4 years ago • 10 comments

Is there an example of how to integrate piccolo into an existing fastapi application?

Something along the lines of the examples in the fastapi docs here and here.

I'm interested in trying the ORM out as it reminds me a lot of how EntityFramework operates in the .NET world, but right now it seems a lot of magic and black boxed and to require using one of the generators to put all the pieces into place. That's just my perception. Anyways, would love to see a clean and understandable example for how to put this into an existing fastapi app and use similarily to the ORMs already mentioned in the fastapi docs.

Thanks much - wg

ohmeow avatar Feb 09 '21 17:02 ohmeow

Here's an example:

https://github.com/sinisaos/headless-forum-fastapi

It uses FastAPIWrapper, which is a Piccolo thing, and lets you build a CRUD endpoint without writing much code.

However, that is totally optional - you can just write your own FastAPI endpoints manually.

I totally agree with your point though - more example projects are definitely needed. I'm working on a tutorial, but the more the better.

dantownsend avatar Feb 09 '21 17:02 dantownsend

Cool thanks.

Yah, I'm looking for something less magical (still getting my head around your library) ... something where I can define all my database models in a models.py, all my pydantic view models in a view_models.py, and a local database session object I can use via dependency injection wherever I like. The structure of my particular application is closer to the postgresql template available via fastapi.

Thanks again - wg

On Tue, Feb 9, 2021 at 9:24 AM Daniel Townsend [email protected] wrote:

Here's an example:

https://github.com/sinisaos/headless-forum-fastapi

It uses FastAPIWrapper, which is a Piccolo thing, and lets you build a CRUD endpoint without writing much code.

However, that is totally optional - you can just write your own FastAPI endpoints manually.

I totally agree with your point though - more example projects are definitely needed. I'm working on a tutorial, but the more the better.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/piccolo-orm/piccolo/issues/67#issuecomment-776103826, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAADNMBHF46JYWOAQJWBWN3S6FVUHANCNFSM4XLLLJMA .

ohmeow avatar Feb 09 '21 18:02 ohmeow

If I'm able to pass my hurdles in #112, I'll be happy to do a write-up of a tutorial and publish an example. Leaving this comment here so I'll remember to check back and hold myself somewhat accountable :)

heliumbrain avatar Jun 20 '21 19:06 heliumbrain

@heliumbrain Thanks, I'd really appreciate that. Let me know if I can help in any way.

dantownsend avatar Jun 20 '21 20:06 dantownsend

I've started setting this up here: https://github.com/heliumbrain/fastapi-piccolo

Won't have a lot of time for it this week, but I'll try to squeeze in a few hours.

heliumbrain avatar Jun 21 '21 14:06 heliumbrain

@heliumbrain Awesome, thanks for this!

dantownsend avatar Jun 21 '21 17:06 dantownsend

@dantownsend. So I managed to get quite a lot of it up.. I think I found a nice balance between Piccolo magic and raw Starlette/Pydantic/FastAPI. Please let me know.

Still struggling with the auth tho, can't really wrap my head around how to inject it as a Dependency (a la FastAPI). Have a look at https://github.com/heliumbrain/fastapi-piccolo/blob/main/piccolo-fastapi/product/routers.py. With that, I can login to /admin and get my cookie. It then (seemingly) correctly authenticates against that. Any feedback to improve that is appreciated :)

heliumbrain avatar Jun 22 '21 18:06 heliumbrain

@heliumbrain It's looking really good.

The way I use the Session auth is something like this:

app = FastAPI()
private_app = FastAPI()

@private_app.get('/')
def top_secret_endpoint(request: Request):
    user = request.scope.get('user').id  # request.user may also work
    return JSONResponse({'secrets': 'abc123'})


from functools import partial
auth_middleware = partial(
    AuthenticationMiddleware,
    backend=SessionsAuthBackend(...),
)

app.mount(path="/private-stuff", app=auth_middleware(private_app))

I'm not sure how it would work as a FastAPI dependency - I need to look into it.

dantownsend avatar Jun 24 '21 19:06 dantownsend

@dantownsend cool, thanks! I'll look in to it a bit more too

heliumbrain avatar Jun 25 '21 12:06 heliumbrain

@dantownsend I'll be working a bit more on this the next few days. Hopefully I'll have a fully working example-repo and a demo up before the weekend. I'll let you know, would love to get your feedback when it's done

heliumbrain avatar Jul 07 '21 09:07 heliumbrain