I've read a lot of love for Litestar (formerly Starlite), since it seems people prefer it over FastAPI, Flask, etc.
Or is the preferred web framework still Django?
I've read a lot of love for Litestar (formerly Starlite), since it seems people prefer it over FastAPI, Flask, etc.
Or is the preferred web framework still Django?
What do I mean by "cross cutting concerns"? I'm thinking about ORM/models, migrations, admin UI, caching, sessions, misc security middleware, user accounts, validation/forms, templating, RSS feeds, logging, testing infrastructure, etc. Django's pieces all fit together well, and any time I've done this with Flask/etc I've found myself spending so much time solving issues gluing these bits together and working around impedance mismatches between libraries.
We had a large Django site at my last place and would just suggest new starters worked through the Django tutorial, because our site basically still worked like that.
1. You're making something with 100 lines, which you're absolutely sure will stay 100 lines. Go with Flask.
2. Otherwise, Django.
Django is great at getting out of your way when you don't need its functionality, so you can just ignore most of it if you don't need it.
And I've done smaller things with Flask where it would make no sense to go with Django as well but those were things not really built around a db or CRUD API etc.
I used to like DRF (Django Rest Framework) and Django + DRF was a powerful combo to drive single-page-application sites. But DRF can get a little painful if you're not using some of the out-of-the-box classes. Painful as in... lots of code to right and very hard for someone to maintain without knowledge of how DRF magic is made under the hood.
I wish I had a hard and fast rule for the choice, but it's something like:
- Litestar if I know with certainty (a) the thing I'm building is and always will be a tiny service and (b) I probably won't have to evolve the backing data schema much. Wrap an ML model? Litestar. Have a small database I want to expose via API, read-mostly? Litestar.
- Django if I know (a) I'm going to be building on this over a longer timeframe, (b) I have complex user/group auth needs, (c) my data schema will likely go through substantial evolution, or (d) I will need an admin UI in the early goings, either for the managing team or for customer support.
I have plenty of SQLAlchemy+Alembic+Litestar backends but I vastly prefer the creature comforts of Django's ORM + migrations; it hits that 80% sweet spot so very well. SQLalchemy is both raw power and, often, far too much friction.
I used to choose between Django and Flask, but Litestar -- while still young -- seems to capture most of the key things I liked about core Flask but in a more modern package. (Maybe Quart, aka async Flask, will ultimately win the day; not sure.)
It's not targeted at the use case, but I wonder if Datasette will replace my need for the Django admin UI at least in some limited set of cases.
These days, most of my front-ends are React+Typescript+Vite.
(FWIW I personally avoid FastAPI entirely. I appreciate the impact it had getting the python community to think about (a) async (b) proper use of type hints in the web context and (c) the need for OpenAPI support, etc. but there are a lot of footguns and so many issues still opened that its (amazing, lovely) single maintainer probably won't get to. SQLModel -- aka the merging of Pydantic + SQLAlchemy ORM models -- is an even bigger trap, IMO.)
The only two things it lacks great support of is typing and async. However the async is actively being worked on and does work (with some quirks), and python's type support has only gotten half-mature just recently (coming from typescript at least).
I would personally be hesitant to try something new just because django is so battle-tested at this stage, unless you just absolutely require better async support.
I recently tried to implement it in a project using the new ORM a-methods (which AFAIK are not even async under the hood) and it resulted in an explosion of complexity bubbling up in many places in the codebase. I reverted everything to sync and re-implemented the hot functions with multi-threading... I think I'll keep to this approach until I see some good guides, codebase examples, and overall feature maturity.
> I reverted everything to sync and re-implemented the hot functions with multi-threading...
I'm curious what your workload is? If you need the requests themselves async then you normally have gunicorn/uwsgi handle that for you. Otherwise you'd send it to a work queue. Do you have a lot of long-running queries or realtime stuff?
Hopefully django's async support improves. I love the ORM so much I use it for non-server based projects.
If you discover a hot spot somewhere one can always spin that part off to its own service (which can very well be FastAPI to run it async) or rewrite that particular service in Go or Java. But I would not recommend premature optimization in most cases. First get your product out.
[1] https://lp.jetbrains.com/python-developers-survey-2022/#Fram...