Running Django and Flask at same time

Question:

I want to run Django and Flask at same time. The Django app would run at ‘hello.com’ and the Flask app would run at ‘hello.com/flaskapp’. How can I do this?

Answers:

If you don’t need your django-y stuff in flask or your flask-y stuff in django (i.e. they are entirely standalone applications) then this is just a matter of setting up your web server to proxy requests to /flaskapp to a process running Flask and everything else under / to a process running Django.

I’ve never done this in production, mind, and never specifically with Django and Flask side by side, but have quite often done it with a NodeJS dev server and a Django (or Flask) backend API that proxies all requests to /api/ to whichever port on localhost I am running the Django dev server on.

An example configuration for nginx to accomplish this:

upstream django {
    # your usual django config using e.g. uwsgi or gunicorn
}

upstream flask {
    # your usual flask config
}

server {
    location / {
        proxy_pass http://django;
    }

    location /flaskapp {
        proxy_pass http://flask;
    }
}

@davidism makes a good point in the comments about letting Flask know it doesn’t “own” the site root. Settings script_root to /flaskapp should do the trick (see the relevant Flask docs).

Answered By: chucksmash
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.