DEV Community

Cover image for How I Structure My Flask Apps

How I Structure My Flask Apps

Itachi Uchiha on June 23, 2019

This post previously published on my blog Hi. I have been developing Flask applications as a side project for 5 years. After all these years, I...
Collapse
 
dbanty profile image
Dylan Anthony

Thanks for the post! I have a few questions if you don’t mind:

  1. Any reason you choose to create and import the app directly instead of using the factory method?

  2. How do you usually run Flask apps in production? (e.g. gunicorn, waitress, etc.)

  3. How do you manage configs for different environments?

  4. How do you distribute the services to the server?

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Hi. Thanks for your questions :)

1-) Actually, this is a habit for me. I never used a factory method in my Flask apps. Also, I don't know what are the benefits of factory methods.

2-) I use gunicorn.

3-) I usually use dotenv. But I used class-based configs for this post :)

4-) I don't understand this question :/

Thanks :)

Collapse
 
adekoder profile image
adekoder

Benefit of using factory methods

Testing. You can have instances of the application with different settings to test every case.

Multiple instances. Imagine you want to run different versions of the same application. Of course you could have multiple instances with different configs set up in your webserver, but if you use factories, you can have multiple instances of the same application running in the same application process which can be handy.

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha

Hmm, can you show an example about factory methods for the newbies :)

Thread Thread
 
adekoder profile image
adekoder

Sure in a bit

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha

Thanks, I'll be waiting for your contribution. Thanks.

Collapse
 
costular profile image
Diego Francisco ConcepciΓ³n

The fourth question means:
How do you deploy your monolith/microservices to your server?

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha

Hmm. I'm using the EB CLI.

Firstly, I install it.

pip install awsebcli β€” upgrade

I also set secret keys for the elastic bean in the ~/.aws/config file.

[profile eb-cli]
aws_access_key_id = MY_KEY
aws_secret_access_key = MY_SECRET

and then I use these commands.

eb init -p python-3.6 my-flask-app β€” region WHICH_REGION_DO_YOU_USE

eb init

eb create flask-env

eb open

Did you mean this? :P I hope :/)

Thread Thread
 
dbanty profile image
Dylan Anthony

Yes, thank you. I’ve never used elastic beanstalk before, I’ll add it to the list of services to explore. Thanks for all your answers!

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha • Edited

Actually, I only used it twice times. I'll use gcloud or azure next times. (This is my plan). Thanks :)

Collapse
 
jayso_o1 profile image
Sowah Joseph Anyetei • Edited

I think the question four was referring to whether it's a microservice or monolithic app.

Collapse
 
cybermischa profile image
Mischa Spiegelmock

I run flask on AWS lambda. This is my application structure: github.com/jetbridge/sls-flask/

Collapse
 
jldohmann profile image
Jesse

Started using Flask recently, and this article is really helpful, thank you! Also enjoying the discussions in the comments.

Collapse
 
avelino profile image
Thiago Avelino

I like Flask Blueprint architecture flask.pocoo.org/docs/1.0/blueprints/

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Yes, I use blueprints :)

Collapse
 
perymerdeka profile image
perymerdeka

ask how to call app.config['some conf'] in views?

Collapse
 
ayushnita profile image
Ayushnita

I also trying to create a command-line project that creates this code automatically.
github.com/openuniquesolution/flas...

If you feel its good idea pls start raising pr