Skip to content

Commit v0.7

kwmccabe edited this page Apr 17, 2018 · 9 revisions

v0.7 - Blueprint modules: main, item


Files changed (6)

File web/app/init.py MODIFIED

  • Register main_blueprint and item_blueprint Blueprint objects with the application.
     init_logging(app)
 
+    from .main import main as main_blueprint
+    app.register_blueprint(main_blueprint)
+
+    from .item import item as item_blueprint
+    app.register_blueprint(item_blueprint)
+
     return app 

File web/app/item/init.py ADDED

  • Create Blueprint object for the item module.
  • The Blueprint is registered during the app.create_app() process.
+from flask import Blueprint
+
+item = Blueprint('item', __name__, template_folder='templates')
+
+from . import views

File web/app/item/views.py ADDED

  • Create initial route /item/ to test module.
+import logging
+
+from flask import current_app, escape
+from . import item
+
+
+@item.route('/item/')
+def hello_item():
+    logging.info("hello_item()")
+    return 'Hello FlaskApp : Item Module'

File web/app/main/init.py ADDED

  • Create Blueprint object for the main module.
  • The Blueprint is registered during the app.create_app() process.
+from flask import Blueprint
+
+main = Blueprint('main', __name__, template_folder='templates')
+
+from . import views

File web/app/main/views.py ADDED

  • Create initial route /main/ to test module.
  • Relocate routes /info/date and /info/config from flaskapp.py
  • Add route /info/url_map.
+import datetime
+import logging
+import os
+
+from flask import current_app, escape
+from . import main
+
+
+@main.route('/main/')
+def hello_main():
+    logging.info("hello_main()")
+    return 'Hello FlaskApp : Main Module'
+
+
+@main.route('/info/date')
+def info_date():
+    ts = datetime.datetime.now().strftime("%Y/%m/%d @ %H:%M:%S")
+    return "Current Datetime : %s" % ts
+
+
+@main.route('/info/config')
+def info_config():
+    cnf = dict(current_app.config)
+    return "'%s' Config : %s" % (os.getenv('FLASK_CONFIG'),cnf)
+
+
+@main.route('/info/url_map')
+def info_url_map():
+    return "current_app.url_map:<pre> %s </pre>" % escape(current_app.url_map)

File web/flaskapp.py MODIFIED

  • Relocate routes /info/date and /info/config to main module.
-
-@app.route('/info/date')
-def info_date():
-    ts = datetime.datetime.now().strftime("%Y/%m/%d @ %H:%M:%S")
-    return "Current Datetime : %s" % ts
-
-
-@app.route('/info/config')
-def app_config():
-    cnf = dict(app.config)
-    return "'%s' Config : %s" % (os.getenv('FLASK_CONFIG'),cnf)
-

Commit-v0.6 | Commit-v0.7 | Commit-v0.8

Clone this wiki locally