11 ms·
Flask Mega-Tutorial (2017)
- caspii 4y agoI learned Flask using this tutorial for a toy project. 4 years later that project has become my main source of income. Flask is fantastic and I would recommend it to anyone starting out developing webapps.
- Zababa 4y agoIs there anything like this but for express? Trying to piece together 10 libraries and 20 medium articles is always a bit of a pain.
- drcongo 4y agoI've recommended this tutorial for years to people wanting to learn Python for the web. It's really well done.
- Tyrannas 4y agoStep 1: pip install fastapi /s
- jyriand 4y agoCare to explain?
- trymas 4y agoNot OP, but I think OP meant about: https://fastapi.tiangolo.com/ https://fastapi.tiangolo.com/ FastAPI is async web-framework for Python comparable [0] to Flask. Read it as Flask is "old" and "slow" (i.e. synchronous and needs multiprocess deployment), whereas FastAPI is really fast, single threaded and async. [0] IMHO comparable only at "hello world" level as FastAPI is much more and uses a lot of goodies of newer Python versions (typing, asyncio, etc.)
- samwillis 4y agoAsync python is not inherently "faster" than sync, more scalable? maybe, easer to write parallel code? yes. But async does not magically make anything faster. > synchronous and needs multiprocess deployment Async python still has the issue of the GIL, you still need multiple processes to scale out with async. The only seriously useful thing async gives you is the ability to run multiple async IO operations in parallel while processing a single request. But most async request processing is still being written in a somewhat sync way, "awaiting" each IO operation within a view/request processing function. I tend to only use async for long running requests (web-sockets, SSE, long polling), or if I have a view that is calling lots of slow IO which doesn't depend on each other. Think views that are calling multiple REST APIs, and multiple DB requests, you can do them all at once. That is incredible rare though, usually requests do depend on each other and so have to be run sequentially.
- rhodysurf 4y agoThanks for this detailed comment. I think people really misunderstand async python on web servers. Async literally only helps when you are io bound otherwise it hurts
- trymas 4y ago> Async python is not inherently "faster" than sync, more scalable? I understand the pros/cons of async vs sync. Just did not want to get into multi-paragraph tirade to explain everything. EDIT: though FastAPI is still fast compared to other python frameworks and especially synchronous ones: https://www.techempower.com/benchmarks/#section=test&runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&hw=ph&test=query&l=v2qha7-7&a=2 https://www.techempower.com/benchmarks/#section=test&runid=7...
- atxbcp 4y agowhat you don't understand is that FastAPI is not a web framework, so it is neither fast nor slow, it's just a patchwork of libraries on top of Starlette.
- BayesStreet 4y agoI was a diehard Flask fan for years until I finally decided to try FastApi. Wish I switched sooner. Free swaggerdocs is pretty nice, and dependencies get rid of so much code duplication. A good starter to mess around in is this project https://github.com/tiangolo/full-stack-fastapi-postgresql https://github.com/tiangolo/full-stack-fastapi-postgresql
- wiktorcie 4y agofastapi is valuable and useful tech. But it's also very over hyped with all the it's fanatics trying to throw it at every problem. Simple admin dashboard for few dozen users? FASTAPI. Backend for a mobile app? FASTAPI. There is more useful and battle tested solutions out there for flask than fastapi (due to it's obvious time in the market, but still). It's also interesting to check number of github issues for both frameworks (14 vs >1k). Nonetheless fastapi is great.
- samwillis 4y agoI quite like the look of Django-Ninja (https://django-ninja.rest-framework.com https://django-ninja.rest-framework.com). It's inspired by FastAPI but plays nicely with Django.
- nagestri 4y agoAwesome. This is why I read this board.
- ryannevius 4y agoMiguel has updated this guide (available for purchase). In my opinion, it's worth every penny as a starting point into building larger Flask apps, but does include some material that isn't optimized for production-ready Flask apps (e.g. the chapters on authentication).
- divbzero 4y agoWhat part of the authentication material isn’t optimized for production and what adjustments would you recommend?
- marmakoide 4y agoI learned to use Flask with this tutorial, which complemented the already good Flask documentation. This allowed me to spin-up web interfaces to make monitors, remote control panels and so on very easily. Than you very much Miguel, it was a great gift.
- marban 4y agoI'm somewhat of a Flask pro but have struggled to find a good entry on how to use it as a backend for a native iOS app (for someone who completely missed the Swift train). I guess it starts with migrating to Fastapi?
- arinlen 4y agoSpeaking of backend development, recently I gave Jooby[1] a try after discovering it was one of the world's top performer in Tech Empower's web framework benchmark[2]. Surprisingly enough, it's terribly easy to put together a REST API with Jooby. I was expecting a lot of arcane tricks to set things up, but it's one of the less.verbose frameworks out there. I wonder why it's adoption rate is so low. [1] https://jooby.io/ https://jooby.io/ [2] https://www.techempower.com/benchmarks/ https://www.techempower.com/benchmarks/
- darkstar_16 4y agoWhy fastapi, though ? What's missing in Flask if you just want to learn iOS programming ?
- rhodysurf 4y agoWhat is flask missing? FastAPI has swagger and open api stuff but it’s not like it’s required or necessary most of the time. I use my basic flask server as the most dead simple rest API backend for my iOS apps without any issues
- mgraupner 4y agoAs others have said: What has one to do with the other? As a Flask pro you probably know that you can create great APIs with it. And iOS apps can consume them.
- neurostimulant 4y agoNot using Flask, but I've been using Django as backend for various iOS apps over the years. My recipe is just a REST API library + Django OAuth2 Provider. I imagine if I were to use Flask, I'll do the same thing: implementing the REST API and gate it using OAuth2. Plenty of library in iOS side that make hooking up with OAuth2 relatively easy.
- parham 4y agoThis was my go to for learning Flask, I wish I never found it. Flask has a very niche use case in my opinion and if you need something to scale (in terms of code complexity) Flask is the absolute worst decision you can make. Try Django instead. If you read my comment history you'll see that I learnt this the hard way.
- Zr40 4y agoIn my experience it's quite the opposite. We use Flask for a large-scale webapp in production quite successfully and it's enabled us to reduce application complexity significantly.
- nicbou 4y agoThat was also my experience. I find flask to fill a small gap between SimpleHTTPServer and Django.
- JodieBenitez 4y agoFor every case where I would have used Flask in the past, I now would use one of the numerous ASGI-native frameworks.
- nabla9 4y agoAfter reading your comment history and searching it, it seems to me that you never learned to use Flask. Jumping into new project and learning a framework at the same time means that you may have to restructure everything after you have learned it. That's my impression at least.
- parham 4y agoI learnt it quite well actually. Used it in a few projects one quite large. It worked ok for internal apis not open to customers, but even then Django was much better as just setting up the admin side provided a lot of value to the business (for free). All forgotten now but wasted a huge amount of time glueing things together that came for free with Django.
- 4y ago
- miguelgrinberg 4y agoFlask Mega-Tutorial author here. This was a surprise. :) Happy to see people continue to find my tutorial useful and relevant. If you have any questions, I'll be happy to answer.
- teruakohatu 4y agoIs flask-admin worth using? Or any other auto-admin type libraries?
- miguelgrinberg 4y agoThis is really a matter of preference. I do not use Flask-Admin on my projects, I have always preferred to design my own admin pages than being forced to accept the choices that are imposed by these big extensions. This allows me to have a consistent UI across admin and non-admin pages. One thing that people often complain is that it is hard and/or tedious to build tables with data, which is one of the building blocks most admin pages need. Last year I wrote a blog article where I show how I build tables in my Flask apps: https://blog.miguelgrinberg.com/post/beautiful-interactive-tables-for-your-flask-templates https://blog.miguelgrinberg.com/post/beautiful-interactive-t....
- zach_garwood 4y agoI think that Flask has many great extensions, but in my opinion, installing flask-admin is just a tacit admission that you really wanted to use Django instead.
- iknownothow 4y agoWhat do you think about the current state of Flask and Flask extensions? Are they being maintained and up to date? Or would it better to go with FastAPI and use its non-async features to have the same Flask like experience?
- miguelgrinberg 4y agoSomething that I think not everybody realizes is that Flask does not require extensions for most things. The extensions are nice because they integrate with the Flask configuration object, and with before/after request handlers when it makes sense, but none of that is required. If an extension stops being maintained, you can always find a newer general purpose Python package that does what you need and just use it in your Flask app. Now this is my personal opinion, but you cannot say the same about FastAPI. Even though the ecosystem of asyncio compatible libraries has grown, it is still several orders of magnitude smaller than what is available for standard non-async Python. So in my view, in terms of access to 3rd party libraries Flask is a better choice.
- sgt 4y agoFor the items covered in this tutorial, I would rather recommend Django. Better documented and arguably more suited to the task. For an API alone e.g. along with an SPA I would consider FastAPI.
- deleted 4y ago[deleted]
- sireat 4y agoI can second that Miguel's tutorial is quite nice and sufficient to build Flask apps. Thanks to Miguel (and also official Flask docs) I have some Flask Apps running non stop for a few years by now. Flask is extremely easy to write in regards that the mental model is easy to follow. Sure there is magic there but the abstractions seem "non-leaky". By contrast I can't get into Django because it seems too much magic. The worst part of using Python for back end was deployment. Setting up WSGI server to host Python apps was a bit of a pain. https://flask.palletsprojects.com/en/2.1.x/deploying/ https://flask.palletsprojects.com/en/2.1.x/deploying/ I think I went with Gunicorn but there was a bit of learning curve there. I am not even sure if that was the right choice
- mritchie712 4y agoIf you've used Google Cloud, you should be able to get this tutorial running in minutes: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-python-service https://cloud.google.com/run/docs/quickstarts/build-and-depl...
- gosukiwi 4y agoComing from Rails, I appreciate Django's less-magical approach. I admit it has some silly limitations though, like not being able to call methods with arguments in templates, and the whole mixin view generic classes, just to name a few That being said, Django is great, and so is Python :)
- godmode2019 4y agoFlask is easily the best python web framework. Many years ago a learnt flask from this tutorial I built a json ui for my flask apps the other day - Jsonify - https://github.com/xzava/jsonify https://github.com/xzava/jsonify While I am a flask fan, a lot of good things can be said about fastapi.
- pil0u 4y agoThis is how I started my journey with web development. I was a Data Scientist and I superficially used Flask to build an internal matching tool between internal products and competitors product (after an ML model). Then I started the Mega-Tutorial to build a small app to track our foosball matches and run a team tournament. Eventually, I wanted to accelerate my learning and joined a bootcamp (Le Wagon) where I learned Ruby on Rails from scratch. My Flask introduction with this tutorial was really helpful to understand things on almost all layers. I always recommend this tutorial to people who come from Python and want a sneak peak to web development. I am very grateful for Miguel's work and for sharing it.
- 4dregress 4y agoThis is gold! He also has a great tutorial explaining how to use celery with Flask!
- z3ugma 4y agoThe celery-Flask tutorial saved my bacon recently. It sometimes surprises me that the Ruby community rallied so hard around Rails and was able to integrate all these things like job queues (ActiveJob, Sidekiq, DelayedJob etc) right into the main web framework. "batteries included" indeed. Whereas Flask still has more of a pick-and-choose approach.
- nchudleigh 4y agoThis was a big part of my start into flask. Great tutorial.
- trashm3 4y agoMiguel is a great teacher; it's difficult to find proper (up to date!) text-based tutorials in this era. I will also give a shoutout to his recent React Mega-Tutorial, which I've been learning and using his tutorial as one of my sources for learning React. I did his flask tutorial a year + ago before I started learning react. I'd love to see the workflow for developing a backend Flask API first (without Jinja/render_template). It seems less straight-forward than FastAPI which I am using because I think it's simpler to develop APIs first (ala the name)
- aiibe 4y agoMy first web programming tutorial !