Answer the question
In order to leave comments, you need to log in
How can you architect a really big Flask application?
I'm developing a large Flask application and there was a problem in its architecture. In general, I liked the approach in https://github.com/sean-/flask-skeleton that you can use Blueprint (there are plenty of similar examples and guides).
However, the application is great in that it is not just divided into several files for convenience, but serves several sites (hosts / subdomains - will be set in the config) that have an intersection of functionality (therefore, there is a common code for this) and a common session is maintained between them user and the database itself with them (and some other data).
In general, the architecture is now built in such a way that there is a certain core where common models and classes for all sites are created, as well as a Flask application is created, the Flask-SQLAlchemy extension is connected. Support for host matching was also included and the indication of the host in the Blueprint was implemented. The functional blocks themselves are stored in individual packages, where Blueprints are created for this matter, forms, models are defined (they import the created object of the SQLAlchemy class from the extension from the core), views. In general, the idea is the same as in the specified skeleton.
It would seem that everything is fine, register Blueprints in the Flask application and rejoice. But the problem is that Blueprint actually just registers the same imported views under different routes in the routing table (it assumes registering one Blueprint many times - Flask allows this). And this is not even the root of the problem: the model in the functional block remains the same and works with the same data. In fact, at different addresses there will be the same thing, which nullifies the whole point in the application.
At first I tried to solve the problem with models, but I began to think: was the application architecture built correctly? Isn't it too much to demand complete isolation from Blueprint and python packages? Perhaps we need to move towards creating specific instances of models from the abstract one and slip it into the view through the DI pattern? Perhaps it’s worth it to resort to the application factory pattern right now and create a Flask application for each site (but then you need to pass the session and other commonalities between them)?
Briefly and on the fingers, then I would like to build an architecture where let there be site1.ru, site2.ru, site3.ru. User bases and registration are common, session too. There is a common code in the form of blocks, which let them implement the functionality of the same news, articles and forums. And the config would indicate that site1.ru needs news on the main page (prefix /), site2.ru needs two article directories (prefixes /articles_1/ and /articles_2/), site3.ru needs news (prefix /news/ ) and forum (/forum/ prefix). And, of course, these, so to speak, blocks of code must operate with different content.
Well, I'll answer some possible questions:
Answer the question
In order to leave comments, you need to log in
If you want to build a large application, then you already immediately have a big problem
Share
You don’t have to have everything in one database and on one site
Make accounts on one domain - one application
And do everything else through deployment on a new domain
A about flask, I haven't seen success stories
Dynamic table selection for the same model in django is usually solved something like this:
def get_model(db_table):
class MyClassMetaclass(models.base.ModelBase):
def __new__(cls, name, bases, attrs):
name += db_table
return models.base.ModelBase.__new__(cls, name, bases, attrs)
class MyClass(models.Model):
__metaclass__ = MyClassMetaclass
class Meta:
db_table = db_table
return MyClass
my_model = get_model('29345794_table')
my_model.objects.filter( ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question