Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask : how to architect the project with multiple apps?

Tags:

python

flask

Lets say I want to build a project Facebook

I need a project structure like

facebook/
         __init__.py
         feed/
             __init__.py
             models.py
             business.py
             views.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         games/
             __init__.py
             models.py
             business.py
             views.py
         common/
             common.py

         runserver.py

How can I structure this well so that when I run

python facebook/runserver.py

It loads views from all my apps internally?
I want to keep this structure because extending the project further is more natural way

I am trying to follow their advice, but don't really understand where I need to write

from flask import Flask

app = Flask(__name__)

and how to import all views from all apps at one place, please help

If lets say I write the above code in facebook/__init__.py, then how in facebook/feed/views.py, I can do

from facebook import app
like image 516
daydreamer Avatar asked Mar 23 '13 05:03

daydreamer


People also ask

Can you run multiple Flask apps?

Application dispatching is the process of combining multiple Flask applications on the WSGI level. You can combine not only Flask applications but any WSGI application. This would allow you to run a Django and a Flask application in the same interpreter side by side if you want.

Can Flask be used for large projects?

Unlike most other web frameworks, Flask does not impose a specific organization for large projects; the way to structure the application is left entirely to the developer. In this chapter, a possible way to organize a large application in packages and modules is presented.

Can Flask handle large applications?

Absolutely, yes. In fact, Flask is arguably better for a truly large scale app compared to Django.


1 Answers

Use blueprints. Each one of your sub-applications should be a blueprint, and you load every one of them inside your main init file.

Answering your second question

from flask import Flask
app = Flask(__name__)

You should put this into facebook/__init__.py

BTW, my runserver.py and settings.py always resides one level under facebook/.

Like this:

facebook/
         __init__.py
         feed/
             __init__.py
             models.py
             business.py
             views.py
         chat/
             __init__.py
             models.py
             business.py
             views.py
         games/
             __init__.py
             models.py
             business.py
             views.py
         common/
             common.py

runserver.py
settings.py

Content of runserver.py:

from facebook import app
app.run()

I suppose the content of settings.py should not be explained.

Content of facebook/__init__.py:

from flask import Flask
app = Flask(__name__)
app.config.from_object('settings')
from blog.views import blog #blog is blueprint, I prefer to init them inside views.py file
app.register_blueprint(blog,url_prefix="/blog")
like image 96
Tigra Avatar answered Oct 22 '22 04:10

Tigra