- I took a couple of good things from Django's manage.py by using Flask-Script. It looks like 
- I'm not sure if there's a better way of doing this, but I really want to be able to swap out configurations based on which environment I'm using. I think I've forced some Django structure onto Flask, but that said, I dynamically load in a `settings` object and use that to configure my `app` object. I use something like . Just looking over it again I think I have some extraneous code in there but I'm tired. It works pretty well, so far.
- Flask-SQLAlchemy has some good stuff too but I'm not entirely sold on using it. I'm using it on one project, but not the other. I don't miss it when I don't have it.
As far as configuration based on environment goes, I really dislike environment based configuration in my project repository. So I use Flask's instance folder functionality:
This file can be used to configure the app based on its environment wether it be local development or my production environment. The shared factory method attempts to load this file and fails silently if it does not exist.
Regarding your SQLAlchemy comment, well...thats just not really something to add to the article now, is it? ;)
One thing I've done differently on my last couple of projects is to put the core parts of the app into their own packages at the top level and to make a 'web' package that just contains the web interface and the config. That way I make sure that the core application code isn't bound to the web framework at all. Then within my web blueprints they import the bits of the core library they need to do their work. I can then use the main bit of the library myself without having anything to do with the flask app.
The web app itself is responsible for creating a db connection and then injecting that into the core when needed. I've made a simple gateway class that wraps the db connection and deals with persistence.
The data layer is the bit I'm least happy with. Mostly that's because I went with a nosql solution for the db which ultimately has made the data model more complex than it needs to be. Tomorrow I'm going to experiment with switching to SQLAlchemy again to see how that effects things.
Definitely not saying it's the right way to do things but it's another take on how you can structure things and it's been working really well for me. Feels like you're writing an application first and a website second - which changes how you think about the code.
It'd be great to see Flask-Security gain more prominence (after you change the default away from plaintext of course :)
This was just a test project of mine so I used the settings like this (in my app.py file):
SECURITY_PASSWORD_HASH = 'pbkdf2_sha512'
app = Flask(__name__)
Question: I have a web app built in flask, but I also require a worker process in the background that is actively scraping websites and inserting/ updating data into the DB via the ORM. How would you go about that in the cleanest way?
It works, and it's easier for me to organize projects according to a common standard.
I would be fascinated to read your rationale for just using Django. Specifically I'm curious what use-cases you see as Django having an advantage in? (Specific examples make for a much more interesting dialogue.) In relation to this, do you think there are contrasting use-cases where Flask might excel as a more suitable option? Personally, I do not favor Flask or Django or <insert web application suite here> exclusively; instead I evaluate a use-case and then determine which tool best fits my needs. However, it would be amazing if you could somehow show that "just use django" is axiomatic to web development in Python...
Since then I've yet to run into a case where I want a lot more than what Flask offers. However, If there's something better that I just don't know about, I'd love to hear about it!
The three big things I think Django has over Flask:
- A stronger ecosystem
(But of course, none of these are entirely absent from Flask -- its not as if its particularly difficult to plug in SQLAlchemy, and I believe there's a Flask-admin package nowadays.)
My general advice is that if you feel like the thing you're developing is going to turn into a CRUD app, use Django.
As you say, it really depends on your use-case. I wrote a number of crud apps in Django in a previous life (when Django was still fairly young). I found that out of the box you could get something set up quickly but trying to bend it to your (or the clients) will sometimes required a lot of work. I remember at one point having to duplicate a huge chunk of code into one of my models. That's the tradeoff you'll always run into. You'll get a really complete system for free but it won't be as flexible as you may one day need it to be.
Flask does have an admin plugin but it's nowhere near as good, polished, or documented as Django's.
Django does have the advantage of a bigger mind share, users and ecosystems.
sqlalchemy is more powerful than django orm.
> I believe Flask actually has the advantage in the ORM since you aren't tied to Django's strictly RDBMS structure and Flask will allow you to easily use NoSQL solutions if that's your thing
easy there. you can as well use nosql with django (orm is useless then, by definition) and you are in the same exact situation as using nosql engines with flask. if you get rid of the orm in django (which is doable), you are just bringing your own batteries here, just as you are bringing them to flask.
Agreed - we normally use flask alongside psycopg2 and postgres but recently we've been using it with NEO4J for fun
Edit: on a serious note, use it when all the dependencies it comes with matches what you exactly need. Once you're down the path of swapping something out, all the third-party apps/packages (one of the key features) become useless. This becomes problematic when you start to use something like MongoDB and end up having to write a lot of code on your own.
or when the client is dictating the tech stack. :)
My primary use case involves having a webapp that actually works
And I was being sarcastic.
Flask is literally a 5-minute ad hoc content management system with all the power to take on massive tasks. The "micro-framework" label is an understatement.
I also think Armin's style is quite elegant and I've learned a lot more about the language just from reading Flask source.