[Flask] Flask Structure

Jani Šumak jani.sumak at gmail.com
Thu Jul 14 06:13:57 EDT 2016


Hi John,

great to have you here! IMHO there is no "correct" Flask structure. You
could put it all in one file and it would still be good Flask app. The only
thing that caught my attention is your run.py, I'd recommend that you
replace app.run() with

if __name__ == "__main__":
    app.run()

so you could run your app with a WSGI server, like Gunicorn or uWSGI. More
on this here
<http://stackoverflow.com/questions/419163/what-does-if-name-main-do>.

But to address your question about structure: Miguel Grinberg
<http://blog.miguelgrinberg.com/index> had a great talk/workshop at PyCon
2016 titled "Flask at Scale" <https://www.youtube.com/watch?v=tdIIJuPh3SI>.
He starts his Flask app with just one file and gradually breaks it up into
modules. The talk is a bit long, but worth your while, because he explains
why breaking up your app into modules and using blueprints is a good thing.
If this talk is too long, I'd highly recommend his book *Flask Web
Development
<http://shop.oreilly.com/product/0636920031116.do?cmp=af-webplatform-books-videos-product_cj_9781449372620_%25zp>
*or some if his previous talks/tutorials
<http://blog.miguelgrinberg.com/post/flask-at-scale-tutorial-at-pycon-2016-in-portland>

*. *
That being said*, *you are free to chose and advocate a different
structure. If you look at some flask cookiecutter repos on Github, e.g.
cookiecutter-flask <https://github.com/sloria/cookiecutter-flask>, you
might see a different app layout, that still adheres to best practices.

Hope this helps and sorry for the late response.

Take care, Jani

On Wed, Jul 13, 2016 at 2:15 AM John Robson <John.Robson at usp.br> wrote:

> 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
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160714/9b091898/attachment-0001.html>


More information about the Flask mailing list