Forums

One web app but two applications

Hello, I have one web app but would like to run two flask applications 1) bookshop_front.py handles base url/bookshop and generates the HTML 2) books_api.py handles base url/api/books and provides the api crud How would i set this up in the WSGI configuration file?

You can use the werkzeug dispatcher middleware: https://werkzeug.palletsprojects.com/en/3.0.x/middleware/dispatcher/

Thank you for your link it has helped me a lot. However I think I must be missing something When just using mono application then book_api_v1_0 was working fine I have tried to adapt it to use DispatcherMiddleware

in /home/rdent/mysite I have book_api_v1_0.py & book_front.py

From postman https://rdent.pythonanywhere.com/api/v1_0/book I get error <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>

Here is the modified WSGI config file, could you give me a pointer as to what is wrong:

import sys

from werkzeug.middleware.dispatcher import DispatcherMiddleware

Add your project directories to sys.path

project_home = '/home/rdent/mysite'

if project_home not in sys.path:

sys.path = [project_home] + sys.path

print("sys.path:", sys.path)

from book_api_v1_0 import app as book_api_v1_0_app

from book_front import app as book_front_app

Create a DispatcherMiddleware to route requests based on path

application = DispatcherMiddleware(book_front_app, {

'/api/v1_0/book': book_api_v1_0_app,
enter code here

})

Ok finally got it to work. I didn't realize in the mount part of application definition when you use /bookshop to route

you have to change the code in the flask to @app.route('/') instead of @app.route('/bookshop')

Here is the final config file for anyone who is interested. Many thanks

import sys

from werkzeug.middleware.dispatcher import DispatcherMiddleware

project_home = '/home/rdent/mysite'

if project_home not in sys.path:

sys.path = [project_home] + sys.path
enter code here

print("sys.path:", sys.path)

from book_api_v1_0 import app as book_api_v1_0_app

from book_front import app as book_front_app

application = DispatcherMiddleware(book_api_v1_0_app, {

'/bookshop': book_front_app,
enter code here

})