loading...

Why I'm Leaving Flask

dbanty profile image Dylan Anthony ・2 min read

TL;DR
Not interested in the whole story? Just go check out FastAPI

Several months ago I started this series about how to make a REST API using Python and Flask. Then, abruptly, I stopped posting. Why? The purpose of the series was to show what I consider to be the best way to develop a REST API using Python, fueled by my experience using the Flask framework. Recently, as I have been given the opportunity to explore different technologies, I realized that Flask may not be the best choice anymore.

Let me explain- Flask is an excellent, modular, pluggable framework. Its greatest power comes from its flexibility and rich ecosystem, allowing you to build virtually anything on top of it. However, if you want to build a REST API with clear, static, inputs which are validated, you have to include several different packages from the community maintained by different groups of people that don't really play nice with each other. On top of that, you have to write a great deal of custom code to get it all working. The reality is, only developers who are really dedicated to correct, well documented, safe APIs will go through this effort. I am (and have), clearly, but most people are not.

This leads me to the real reason I'm leaving Flask: I found something else that fulfils my needs better. FastAPI is my new favorite web framework because it:

  1. Is built from the ground up to do everything I want- no hacks required.
  2. Does input declaration and validation with Python 3.6+ type hints, so the developer experience is naturally very good.
  3. Is async which means I don't have to fight with gevent/eventlet monkey patching.
  4. Is built on Starlette which has excellent GraphQL support. See my previous posts about GraphQL to see how confused I've been about why the experience is so bad on Python.
  5. Has awesome documentation meaning I won't have to write an entire blog series to teach people how to build a well-documented API with it.
  6. Is blazingly fast
  7. Is very pluggable using their "dependency injection" system
  8. Is written incredibly well- 100% test coverage including mypy checking. The kind of code quality I enforce at work. For real, check out the code

    GitHub logo tiangolo / fastapi

    FastAPI framework, high performance, easy to learn, fast to code, ready for production

    FastAPI

    FastAPI framework, high performance, easy to learn, fast to code, ready for production

    Test Coverage Package version Join the chat at https://gitter.im/tiangolo/fastapi


    Documentation: https://fastapi.tiangolo.com

    Source Code: https://github.com/tiangolo/fastapi


    FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

    The key features are:

    • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.

    • Fast to code: Increase the speed to develop features by about 200% to 300%. *

    • Fewer bugs: Reduce about 40% of human (developer) induced errors. *

    • Intuitive: Great editor support. Completion everywhere. Less time debugging.

    • Easy: Designed to be easy to use and learn. Less time reading docs.

    • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.

    • Robust: Get production-ready code. With automatic interactive documentation.

    • Standards-based: Based on (and fully compatible with)…

I'm still experimenting with FastAPI, I haven't put anything into production yet that uses it (though that is changing soon). I'm very hopeful, this is by far the best option I've found that covers most of what I want my APIs to do. I can not encourage you enough to go and try it out.

Discussion

pic
Editor guide
Collapse
wangonya profile image
Kinyanjui Wangonya

Thanks! I've also always been a strong advocate for using Flask. Didn't know about FastAPI. I'll definitely play around with it this weekend.

Collapse
rhymes profile image
rhymes

Thanks Dylan! I knew about the project but I've never actually tried it! Let us know :)

Collapse
gabrielviviani profile image
gabriel-viviani

OMG, I certainly will give this a try, and maybe put a RPA sys on production with that. Thanks for that!