I’m just getting started with flask and I’ve hit a snag. I’m trying to write a small blog to get used to the framework so I made two packages, an “auth” and “posts”. I read through the Large Applications section in the Flask docs.
My directory looks like this.
>/root
>>run.py
>>/posts
>>>____init____.py
>>>views.py
>>>/templates
>>>/static
>>/auth
>>>____init____.py
>>>views.py
>>>/templates
>>>/static
the run.py looks like this:
from flask import Flask
from auth import auth_app
from posts import posts_app
auth_app.run()
posts_app.run()
/posts/__init__.py and /auth/__init__.py look like this:
from flask import Flask
auth_app = Flask(__name__)
import auth.views
and the views.py look like this:
from auth import auth_app
@auth_app.route('/auth/')
def index():
return "hello auth!"
But whenever I run the server, only the localhost/auth/ is available, and everything else gives a 404, som I’m assuming that the posts app isnt being run.
Can anyone help?
Your auth_app.run() method blocks your program from continuing to run. This is why the the posts_apps app doesn’t get run. The entire process of serving up pages happens within Flask’s run() method. Therefore, you can conclude that you can’t run two Flask apps in the same process.
If you wish to split up your application into two like this, the recommended way is to use blueprints. Rather than creating two apps (auth and posts), you create two blueprints. You then create one application like so…