How to Run Multiple Flask Applications from the Same Server

Route to multiple WSGI applications at different URL paths

Do you have multiple flask applications that you would like to run from the same domain? Do you want to be able to access them by using a URL prefix instead of by accessing a different port? Application dispatching is the solution.

Why I Wanted to Run Multiple Flask Applications

As a part of my data analyst learning journey I decided to set up a portfolio site. I decided to learn the Flask framework to deploy and showcase projects.

Since I was developing the applications at separate times in separate environments, I wanted to be able to keep them separate in deployment instead of cobbling them into the same large application. Several of the potential solutions I found just ended up placing the different applications on different ports. I wanted a solution that allowed for a modular website that could be accessed through paths on the same domain.

Enter application dispatching. With this you can combine multiple Flask applications at the WSGI level. This also allows you to combine any WSGI application. So if you have separate Flask, Django, or Dash applications you can run them in the same interpreter side by side if you want.

How to Configure Flask Application Dispatching

Setting this up is relatively straightforward when you take a modular approach.

  1. Set up the separate applications
  2. Combine the applications
  3. Invoke a development server

Set up the Separate Applications

Here are 2 Flask applications:

flask_app_1

# /flask_1/__init__.py
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
return '<h1>Hello, World! I am Flask App 1.</h1> \
Please go visit <a href="/flask_app_2">Flask App 2</a>'

flask_app_2

# /flask_2/__init__.py
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
return '<h1>Hello, World! I am Flask App 2.</h1> \
Please go visit <a href="/">Flask App 1</a>'

Combine the Applications

You will utilize the Werkzeug Dispatcher Middleware. flask_app_1 will run on “/” and flask_app_2 will run on “/flask_app_2”.

# app.pyfrom werkzeug.middleware.dispatcher import DispatcherMiddleware # use to combine each Flask app into a larger one that is dispatched based on prefix
from flask_1 import app as flask_app_1
from flask_2 import app as flask_app_2
application = DispatcherMiddleware(flask_app_1, {
'/flask_app_2': flask_app_2
})

Invoke a Development Server

Here is a small file that calls the Werkzeug Run Simple server that is suitable for development. For deployment, you’ll need to configure a suitable WSGI server and remove the debug options from the app files.

# run.pyfrom werkzeug.serving import run_simple # werkzeug development server
from app import application
if __name__ == '__main__':
run_simple('localhost', 5000, application, use_reloader=True, use_debugger=True, use_evalex=True)

Conclusion

As stated before, this is a simple setup that only requires a modular approach and a few lines of code. I wanted to figure out how to set up this framework first before I built my site and then had to restructure files and folders to get it to work.

You can find the complete code for this on my github. You’re welcome to fork it and clone it to get a working example.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store