Forums

Is it better to develop my Flask App locally, then deploy, or to stick to PythonAnywhere's premade Flask App structure?

I am new to developing web applications, so I'm following tutorials... I've noticed that every tutorial's project layout (python package structure) is slightly different. Almost every time.

Now, I've already generated a flask_app.py through PythonAnywhere's app templates. I followed the PythonAnywhere tutorial for making a flask application (very cool, much wow), but I am confused about application structure.

If I develop an app locally (with the intention of deploying to PyAny), should I be using the project layout described in Flask's Documentation, or the project layout in PythonAnywhere's Flask tutorial? A secondary, related, question would be.. Would having my own project structure effect the security of my web application? Is there a convenient tutorial I can follow which describes how to keep my locally-developed application secure; before I deploy?

If it means that security measures remain intact, then I don't mind using the auto-generated application structure PythonAnywhere provides. I'm just feeling a bit stumped on how to expand beyond the Scratchpad tutorial... My web application is going to get pretty complex, have quite a few views, so any help or relevant information would be greatly appreciated. Thank you.

Flask's Project Layout: https://flask.palletsprojects.com/en/1.1.x/tutorial/layout/

PythonAnywhere Flask Tutorial: https://blog.pythonanywhere.com/121/

The great thing about Flask is how much flexibility it allows you in setting up your code's structure (unlike, for example, Django, which imposes a very rigid structure on you). But of course the downside to that is that the flexibility means that you have to decide on a structure, which is (as you say) quite confusing when you're getting started and don't know what the best one is!

Our tutorial goes with a really simple structure just to keep things simple; there's no particular advantage to sticking with that, it's just there to minimise the amount of stuff that people have to learn to get started.

The Flask project layout in their own tutorial is much more complicated -- which is why we avoided it in the tutorial -- but it's definitely a better layout for the longer term.

I don't think that either is better or worse from a security standpoint, though -- it's just that our tutorial's layout is easier to understand when you're getting started, while the Flask tutorial's structure is better for long-term maintenance and extensibility.

So, to cut a long story short -- if you've gone through our tutorial and got the benefits of understanding how the basics work from there, I'd definitely recommend moving to the Flask tutorial structure for your website going forward.

(Perhaps the migration from one structure to the other would make a good third part to our tutorial!)

Thank you, Giles! This answers my questions perfectly! Thank you, thank you, thank you!

A couple suggestions for additional flask tutorials would be:

  • "views.py" or "routes.py" to explain how to split your app views up into additional files

  • an explanation of PythonAnywhere's auto-generated Flask structure, with "Can" and "Can't" do

  • WTForms integration, "forms.py" file -> fill out db.model, then commit model to a MySQLAlchemy DB (builds on Part 2, and "views.py/routes.py" tutorial.

  • how to maintain security within a WSGI application on PythonAnywhere (Flask edition) (builds on this forum post https://www.pythonanywhere.com/forums/topic/2018/)

P.S. I love this platform :o

@jrr -- thanks for the feedback and suggestions, appreciated!