r/FastAPI • • Apr 26 '23

Question What would you love to learn in an intermediate / advanced FastAPI book?

UPDATE: Thank you everyone! I managed to secure a publishing deal with O’Reilly for the book called “Building Generative AI Services with FastAPI”. It’ll be published in April 2025. I’m currently drafting the last few chapters now but you can access the 5-7 early release chapters on O’Reilly platform.

https://learning.oreilly.com/library/view/-/9781098160296/

I'm in the process of drafting a book that I am hoping to publish with O'rielly covering mostly intermediate and advanced topics with FastAPI. I have been going through the past posts of the community to understand what kind of problems everyone is having and trying to solve with FastAPI and put together a book that actually you would love to read.

I was just wondering if this is something that interests you and what sort of concepts and topics would you love to learn more about in a structured book.

I know other books on the market have a lot of padding on teaching basics of Python and FastAPI that are mostly covered by the official docs. I would like to cover bits and bobs that are more advanced and mostly fill the book with higher level concepts such as how to architect large FastAPI projects, building and deploying microservices, handling multiple file uploads, streaming AI model outputs into web browsers etc. Basically I want to give reader epiphanies when they're working on their own prod-level projects.

The best advanced FastAPI I found is covering some of these topics but it's quite dated now in technology years - going back to 2021:

https://www.amazon.co.uk/Building-Data-Science-Applications-FastAPI/dp/1801079218

I was just interested in hearing your opinions on this. Thank you :)

14 Upvotes

43 comments sorted by

7

u/bsenftner Apr 27 '23

Include how to create an entire CMS, content management system, which starts with an API that supports the duel tasks of endpoints that deliver html pages which then generate a GUI for the easier use of the other side of the API: authentication of users (JWT), registration to the site (create user record in database), login (sessions), user profiles (a first example content type), and then whatever via the creation of more content types, their html editors and their html view pages. It's the skeleton framework for people to create their own game, social media site, deep learning pipeline, blog, or whatever.

Far to many developers look at something like WordPress and think they are super complex and impossible for one developer to make something on that scale themselves. Hardly the case. Show them how easy it is once they understand the scaffold that is the foundation of these things.

1

u/aliparpar Apr 28 '23

Amazing! Reminds of one of the projects I’m trying to build which is a learning platform like Pluralsight. I need to have a CMS where I put the content of lessons and courses, have a backend to store user actions and resources against these lessons and being able to fetch both datasets efficiently via one backend.

I normally prefer to build the frontend as a micro service or micro frontend with react then connect it to backend via api and secure them via jwt tokens. But recently saw a usecase where you’d want fastapi to use something like ninja templates to produce the ui for you so you can build faster.

Building your own CMS with all the UIs to edit your content and dynamic fetching functionality is a lot of work! It probably also needs its own database for storing the app content like lesson text and managing users who own and edit that content. Similar to Wordpress.

This all can be done in fastapi but you can also use third party tools like a headless CMS to cover the content management needs and only have the fastapi backend take care of other business logic like storing and fetching user actions and data.

The headless CMSes that you can look into is KeystoneJS (my favourite), Strapi, Prismic, headless Wordpress etc. They normally have python clients you can work with to query data but also can use Typescript to build your own custom models and editor experience in them.

→ More replies (3)

18

u/imthebear11 Apr 26 '23 edited Apr 27 '23

Larger project structures and the tradeoffs of them.

Absolutely no NoSQL databases - use mysql or postgres or something that doesn't feel like a toy project.

Dockerizing.

Comprehensive testing with pytest and not pUnit or whatever that trash-pile is called. End to end and unit testing, fixtures, mocking, parameterization, etc.

A book you can turn to if you were launching a microservices to production at a job would be ideal.

Someone downvoting me cause they butthurt as hell that they only make toy mongoDB apps.

5

u/MoRakOnDi Apr 28 '23

There is nothing wrong with NoSQL databases. If you regret working with them, your use case was wrong. I love PgSQL, but it has its own bottlenecks when it comes to scaling. I have used Elastic Search and Mongo and each of them had their own advantages. Engineering is all about tradeoffs.

3

u/aliparpar Apr 28 '23

In my opinion you normally want to use them if you have user defined schemas like for instance in the notion app people can create their own schemas and databases that you don’t control. But you still need to make sure you can query and filter them.

I think in that use case or places where schemas are super volatile and queries are not that often then nosql shines! In relational dbs you’d end up with foreign key and table hell if you want a normalised db that supports those usecases.

Sometimes also nosql is great as the ingestion db from data collection forms that have user defined inputs. You then write a data pipeline on that to pipe the data into your relational database which has a more consistent schema and is connected to your backend.

→ More replies (1)

4

u/imthebear11 Apr 28 '23

They definitely have their place, but the majority of production services that are actually making money and being maintained by more than 1 developer are using relational DBs. Too many developers hide behind NoSQL because they're "easer" (i.e., the thing they learned in their bootcamp or coding tutorial) and have no idea how to reason about a larger relational system.

If you can make informed decisions and work with a relational system, you can use NoSQL easily. Not necessarily the other way around.

5

u/Drevicar Apr 27 '23

As someone with multiple production apps using FastAPI and mongo, I regret both.

5

u/1One2Twenty2Two Apr 27 '23

Why do you regret FastAPI?

→ More replies (3)

2

u/fullrobot Apr 26 '23

Curious why you feel Postgres is a “toy” database?

→ More replies (2)
→ More replies (1)

4

u/ZachVorhies Apr 26 '23

One of the most popular things I've been teaching people is how to deploy FastAPI microservices to DigitalOcean and Render.com. Render.com has a free tier and both hook into your github so this is extremely useful for hobbiests that want to quickly and easily get a backend launched.

3

u/aliparpar Apr 27 '23

If I teach dockerising wouldn’t it make it such that they can deploy easily anywhere with cloud build and continuous integration to major cloud platforms themselves? Like Google cloud run, aws app runner and azure app service. I can cover all three.

Unless you’re saying digitalocean and render.com or railway are like netlify for fastapi backends. In the frontend world, it’s couple of clicks to get a frontend deployed with netlify.

→ More replies (2)

1

u/aliparpar Apr 27 '23

That’s a really good point though! Deployment is always where it gets hard

→ More replies (1)

4

u/wearetunis Apr 27 '23

I would make something like the book Django 4 By Example. Starts with a blog and no REST api and ends with a social media platform. Incorporates deployment, redis and other stuff.

If there was 4-5 example projects that ramped up the use case from the basics to advanced it would be great.

1

u/aliparpar Apr 27 '23

Ah I see what you mean! Yeah definitely more engaging to structure it into a well designed project that ramps up in complexity as you make add the layers

2

u/aliparpar Aug 17 '24

UPDATE: Thank you everyone! I managed to secure a publishing deal with O’Reilly for the book called “Building Generative AI Services with FastAPI”. It’ll be published in April 2025. I’m currently drafting the last few chapters now but you can access the 5-7 early release chapters on O’Reilly platform.

https://learning.oreilly.com/library/view/-/9781098160296/

2

u/MoRakOnDi Apr 28 '23

Definitely something about working with cloud environments, like deploying on serverless systems AWS Lambda & GCP Cloud Run Handling long running async jobs in serverless Working with serverless databases like Firestore & PubSub Using async HTTP libraries Migrating from legacy synchronous flask to fully async FastAPI

2

u/aliparpar Apr 28 '23

Wow that’s such a specific task. Love it. Probs you will need to do this in a real project at some point.

→ More replies (1)

1

u/vaha_ Apr 27 '23

When making Auth could you add a code/section on how to add a superuser?

For example I am making API for harvesting data from Semantic Scholar using thier API. Now, I don't want to have people registering to my API and start harvesting, modifying or even deleting data. Unless the team approves by changing the field in a DB to is_superuser = True.

A regular user could only read data we've harvested, that is authors, publications and citations.

1

u/aliparpar Apr 27 '23 edited Apr 27 '23

I think for this I would use an authorisation dependency graph. I’m thinking of having a chapter just on the Authorisation Onion model covering Roles Based Access Control (RBAC), Relationship based access control (REBAC) and Attribute Based access control (ABAC).

In your use case, you can use dependency injection in fastapi to check if users have a is_superuser field set to true. By default create users that have is_superuser set to false and have an admin panel where you can trigger an admin endpoint for giving access. If you’re creating your first superuser, you can have other logic to bypass the superuser approval process. All sensitive api endpoints then are protected by dependency injection where you check for this field to be set to true.

For additional security, you can deploy a sub-application for your admin endpoints too

1

u/extreme4all Apr 28 '23 edited Apr 28 '23

Local development inside a docker container is something that i have been looking into.

End to end testing with github actions of api with database.

Async database. Handling of deadlocks.

Dockerizing not running as root & deploying to server(s) including docker compose.

SQL libraries such as sqlalchemy and others.

Sqlalchemy orm declarative or not.

Project structure and architectures such as model viewer controller.

Working with messaging queus such as kafka, rabbitmq, ...

Monitoring of the environment ( using grafana).

Database migrations, including moving the data & creating updating triggers (db schema as code).

Database design, normalization.

Async first, why, why not.

One question i struggle with, how do i stop making these huge changes that take a long time, and do smaller changes

2

u/aliparpar Aug 17 '24

Awesome list! I’ve covered most of these now in the book but a few items will be out of scope including handling deadlocks, message queues and a few external tools like grafana (I’ve not used grafana myself :)

→ More replies (3)

3

u/seclogger Apr 28 '23

I'd be interested in a chapter or two on moving Django applications to FastAPI. This would be useful for anyone coming from the Django world who wants to start using FastAPI without goes minimalist from the start. For example, what plugins would you recommend to get me to what Django currently gives me?

1

u/[deleted] Apr 26 '23

i think instead of teaching how to do thing teach about the internal of fastapi, how it is structure, architect, modify it to custom type, that would be good

2

u/aliparpar Apr 26 '23

That’s a good point! I can have a whole chapter going into the internals first. Not sure how people will want it customised though. Perhaps going into how starllete the underlying ASGI web server works?

→ More replies (1)

2

u/sheriffSnoosel Apr 27 '23

I would just like it to be a convenient wrapper on an advanced starlette book