[Flask] Flask Structure

John Robson John.Robson at usp.br
Tue Jul 12 19:04:21 EDT 2016


I created an structure that is working, but I wonder if this is correct 
or not: (summary)

~/MyApp
     |-- run.py
     |__ /app
          |-- __init__.py
          |-- file1.py
          |-- file2.py

~/MyApp/run.py
     from app import app
     app.run()

~/MyApp/app/__init__.py
     from flask import Flask, render_template, session, Blueprint, 
current_app
     app = Flask(__name__)
     sess = Session()
     sess.init_app(app)
     db = SQLAlchemy()
     db.init_app(app)
     db.app = app

~/MyApp/app/file1.py
     from app import app, db, render_template, session
     db.session.add(......)
     db.session.commit() # Works !!!

     @app.route('/test/', methods=["GET","POST"])
     def test():
         return render_template('works.html')

~/MyApp/app/file2.py (same as file1.py and everything works fine)

In file1.py and file2.py I don't import Flask objects, I import the 
objects that I Imported (render_template, session) or Created (app, db, 
sess) in __init__.py

My question is: "from app import app, db, sess" allows me to use all 
objects from "__init__.py" in every other file.

So, why use Blueprint? This approach is correct? Importing "app", "db", 
"sess", "render_template", etc from __init__.py ?

Thank you,
John
https://bpaste.net/show/9f7b889e9450



More information about the Flask mailing list