flask : how to architect the project with multiple apps?
Question:
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
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")
I have tried blueprints and came up with a solution which works for me, let me know if you have other ideas.
Project Structure
facebook/
runserver.py
feed/
__init__.py
views.py
chat/
__init__.py
views.py
Code
# create blueprint in feed/__init__.py
from flask import Blueprint
feed = Blueprint('feed', __name__)
import views
# create blueprint in chat/__init__.py
from flask import Blueprint
chat = Blueprint('chat', __name__)
import views
# add views (endpoints) in feed/views.py
from . import feed
@feed.route('/feed')
def feed():
return 'feed'
# add views (endpoints) in chat/views.py
from . import chat
@chat.route('/chat')
def chat():
return 'chat'
# register blueprint and start flask app
from flask import Flask
from feed import feed
from chat import chat
app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)
In Action
* Running on http://127.0.0.1:5000/
# Hit Urls
http://127.0.0.1:5000/feed # output feed
http://127.0.0.1:5000/chat # output chat
I attempted the code sample provided with the feed and chat example. When I attempt to import views in each init.py file, I get a
ModuleNotFoundError: No module named ‘views’
runserver
runserver.py
chat
init.py
views.py
feed
init.py
views.py
runserver.py
# register blueprint and start flask app
from flask import Flask
from feed import feed
from chat import chat
app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)
feed/init.py (there are double underscore before and after the init)
from flask import Blueprint
feed = Blueprint('feed', __name__)
import views
feed/views.py
from facebook.feed import feed
@feed.route('/feed')
def feed():
return 'feed'
The error I receive is as follows
/usr/bin/python3.10 /home/.../facebook/runserver.py
Traceback (most recent call last):
File "/home/.../facebook/runserver.py", line 3, in <module>
from feed import feed
File "/home/.../facebook/feed/__init__.py", line 6, in <module>
import views
ModuleNotFoundError: No module named 'views'
Process finished with exit code 1
My understanding of python is that using import on a module (the file) within the same directory will import that module. This gives me the impression that the file/module is considered missing by python even though I see it there.
Is there something I am missing?
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
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")
I have tried blueprints and came up with a solution which works for me, let me know if you have other ideas.
Project Structure
facebook/
runserver.py
feed/
__init__.py
views.py
chat/
__init__.py
views.py
Code
# create blueprint in feed/__init__.py
from flask import Blueprint
feed = Blueprint('feed', __name__)
import views
# create blueprint in chat/__init__.py
from flask import Blueprint
chat = Blueprint('chat', __name__)
import views
# add views (endpoints) in feed/views.py
from . import feed
@feed.route('/feed')
def feed():
return 'feed'
# add views (endpoints) in chat/views.py
from . import chat
@chat.route('/chat')
def chat():
return 'chat'
# register blueprint and start flask app
from flask import Flask
from feed import feed
from chat import chat
app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)
In Action
* Running on http://127.0.0.1:5000/
# Hit Urls
http://127.0.0.1:5000/feed # output feed
http://127.0.0.1:5000/chat # output chat
I attempted the code sample provided with the feed and chat example. When I attempt to import views in each init.py file, I get a
ModuleNotFoundError: No module named ‘views’
runserver
runserver.py
chat
init.py
views.py
feed
init.py
views.py
runserver.py
# register blueprint and start flask app
from flask import Flask
from feed import feed
from chat import chat
app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)
feed/init.py (there are double underscore before and after the init)
from flask import Blueprint
feed = Blueprint('feed', __name__)
import views
feed/views.py
from facebook.feed import feed
@feed.route('/feed')
def feed():
return 'feed'
The error I receive is as follows
/usr/bin/python3.10 /home/.../facebook/runserver.py
Traceback (most recent call last):
File "/home/.../facebook/runserver.py", line 3, in <module>
from feed import feed
File "/home/.../facebook/feed/__init__.py", line 6, in <module>
import views
ModuleNotFoundError: No module named 'views'
Process finished with exit code 1
My understanding of python is that using import on a module (the file) within the same directory will import that module. This gives me the impression that the file/module is considered missing by python even though I see it there.
Is there something I am missing?