Using url_for between two applications
I liked the many conventions of Overholt's example but ran into a specific problem.
I have two applications configured with the DispatcherMiddleware object from werkzeug.wsgi:
from werkzeug.wsgi import DispatcherMiddleware
from myapp import api, frontend
application = DispatcherMiddleware(frontend.create_app(), {
'/api': api.create_app()
})
This works great; the end points are all there. The check application.app.url_map
shows the mappings for the frontend, and it application.mounts['/api'].url_map
correctly renders the mappings for the api.
The problem I am running into is using url_for()
frontend in my templates for methods in api, but haven't found a way to make this work. Hardcoding the URL path works, but will cause problems later if I want to move things around.
source to share
What you can do is add a new route to your server, say /api/route-map
that spits out a map (dictionary / JSON) of routes (you can use url_for
to create a map) and push that route from your interface to get a dynamic route map that you can use through your front-end templates with your jinja2 custom function (which you can create as shown below).
def api_url_for(route_fn_string):
"""
This is just boilerplate code. Please do some checking here.
'"""
return route_map[route_fn_string]
app.jinja_env.globals.update(api_url_for=api_url_for)
Now you can do {{api_url_for('update')}}
in your jinja2 template to get its actual route.
If you are putting both applications on the same server, you can simply share the route map as global or using a getter function.
source to share