Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: Using multiple packages in one app

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?

like image 620
Jeremy Spencer Avatar asked Jun 21 '12 15:06

Jeremy Spencer


People also ask

How to run multiple flask apps on one machine?

32 Flask development server by default listens on port 5000so when you run a Flask app without port number it will run on 5000. You can run a number of Flask app on the same machine but with the different port numbers. Let's say your scripts names are script1.pyand script2.py:

How do I deploy a flask application in production?

A common way of deploying a Flask web application in a production environment is to use an Apache server with the mod_wsgi module, which allows Apache to host any application that supports Python’s Web Server Gateway Interface (WSGI), making it quick and easy to get an application up and running.

Where is my flask app located?

My application is located in a directory named “chatbot”, which contains the main Flask app file (app.py), my virtual environment (chatvenv), and some helping files. So, in my local machine, I use the scp command to copy all my project's files to the remote server.

Can I run multiple apps in the same process group?

Of course, this will cause interference when multiple apps are handled by the same process group, so the safest thing to do is to put each application that relies on C extensions in its own process group. Assuming all of our applications fall into this category (e.g. they all use numpy), our final configuration fill might look like this:


2 Answers

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...

from flask import Flask
from auth import auth_blueprint
from posts import post_blueprint

app = Flask(__name__)
app.register_blueprint(auth_blueprint)
app.register_blueprint(post_blueprint)
app.run()
like image 92
Mark Hildreth Avatar answered Sep 28 '22 07:09

Mark Hildreth


Though it seems as though Mark's approach using blueprints fits your project well, if you want to use separate applications for each package you should look into werkzeug.wsgi.DispatcherMiddleware.

A single process cannot run a second app after you run the first (like in your question), but that's not a problem with DispatcherMiddleware. You can use it to define a main application as well as others based on URL prefixes.

The example on the docs distinguishes between two applications --frontend and backend-- which are run depending on the URL the user requests.

If you want to learn more, read Matt Wright's "How I Structure My Flask Applications" and look at Overholt, his sample project. He decides to use two apps: one for the main website (the frontend) and another for the API, and he creates the distinction between the two based on the URL prefix. From his code*:

    from werkzeug.serving import run_simple
    from werkzeug.wsgi import DispatcherMiddleware
    from overholt import api, frontend


    application = DispatcherMiddleware(frontend.create_app(), {
        '/api': api.create_app()
    })


    if __name__ == "__main__":
        run_simple('0.0.0.0', 5000, application, use_reloader=True, use_debugger=True)

This way, he creates two applications wherein each has its defined set of views, configurations, etc. and is able to run them from the same Python interpreter process.

*Note that run_simple() is only meant to be used for development --not production.

like image 22
aralar Avatar answered Sep 28 '22 08:09

aralar