4

I like to generate routes for my Flask app hosted on Google App Engine. Currently I am using blueprints to generate routes. For instance

conference_routes = Blueprint('conference_routes', __name__)


@conference_routes.route('/home/Conferences/QCCC-Mappleton-2015')
def mappleton_conference():
    return render_template('Mappleton-2015.html')

And then I register the blueprint like so from the main.py

app.register_blueprint(conference_routes)

This is becoming really cumbersome, especially when we have over 100 routes. I wish to define routes in a database and dynamically construct them at runtime.

Is this possible ?

Vinay Joseph
  • 5,515
  • 10
  • 54
  • 94

1 Answers1

3

This answer shows the use of .add_url_rule:

for page in session.query(Page):
route = u'/{0}/{1}/{2}'.format(page.id1, page.id2, page.id3)
app.add_url_rule(
    route,
    page.name,  # this is the name used for url_for
    partial(actual_handler, page=page),
)

This answer has useful information for setting up those routes, and this answer also has several good examples.

It sounds like you'll want to add all of your routing right after setting up your app variable:

conference_routes = Flask(__name__)
# load info from database
# insert routes

If your files are all 1-1 like your example, then you don't even need that route loading method - just look for the file from the url:

@conference_routes.route('/<path:path>')
def search_file(path):
    # (in)validate path
    # return html template or 404
Community
  • 1
  • 1
Celeo
  • 5,583
  • 8
  • 39
  • 41