Skip to content

Commit b83aa83

Browse files
committed
Release v1.0.45 - Flask - Checklist for Production
1 parent c2ccaa9 commit b83aa83

File tree

2 files changed

+142
-75
lines changed

2 files changed

+142
-75
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [1.0.45] 2023-06-22
4+
### Changes
5+
6+
- `NEW` Tutorial: [Flask - Checklist for Production](https://docs.appseed.us/technologies/flask/production-checklist/)
7+
38
## [1.0.44] 2023-06-22
49
### Changes
510

docs/technologies/flask/production-checklist.mdx

Lines changed: 137 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: Learn how to prepare Flask for production
33
---
44

5-
# Flask production Checklist
5+
# Flask Checklist for Production
66

77
import SubHeading from "@site/src/components/SubHeading";
88

@@ -18,38 +18,51 @@ import SubHeading from "@site/src/components/SubHeading";
1818

1919
We will be making reference to [Flask Berry dashboard](https://github.com/app-generator/flask-berry-dashboard) for this tutorial.
2020

21-
## Setting up Flask application
22-
- Clone the repository to your machine
21+
<br />
22+
23+
## Setting up Flask Application
24+
25+
> Clone the repository to your machine
26+
2327
```bash
2428
$ git clone https://github.com/app-generator/flask-berry-dashboard.git
2529
$ cd flask-berry-dashboard
2630
```
27-
- Create a virtual environment and activate it
31+
> Create a virtual environment and activate it
2832
2933
On windows
30-
```bash
31-
flask-berry-dashboard$ virtualenv venv
32-
flask-berry-dashboard$ .\venv\Scripts\activate
33-
(venv) flask-berry-dashboard$
34+
35+
```bash
36+
$ virtualenv venv
37+
$ .\venv\Scripts\activate
38+
(venv) $
3439
```
3540
On Linux/Mac
41+
3642
```bash
37-
flask-berry-dashboard$ virtualenv venv
38-
flask-berry-dashboard$ source venv/bin/activate
39-
(venv) flask-berry-dashboard$
43+
$ virtualenv venv
44+
$ source venv/bin/activate
45+
(venv) $
4046
```
4147

42-
- Install the packages using pip
48+
> Install the packages using pip
49+
4350
```bash
44-
(venv) flask-berry-dashboard$ pip install -r requirements.txt
51+
(venv) $ pip install -r requirements.txt
4552
```
4653

4754
Once you have your application set up, we would start making changes to make the app fit for deployment.
4855

49-
## Preparing Flask application for deployment
56+
<br />
57+
58+
## Codebase `Update for Deployment`
59+
5060
We will be going through some general setups for deployment and then some specific setups for deployment on different platforms.
5161

52-
### Configuring application environmental variables
62+
<br />
63+
64+
### `Update Environment` Variables
65+
5366
- Create a file named `.env` in the root folder of your project, this file will hold environmental variables and their values.
5467

5568
```bash
@@ -61,17 +74,22 @@ DB_HOST=<db-host-name-or-ip>
6174
DB_NAME=<database-name>
6275
DEBUG=False
6376
```
64-
`SECRET_KEY` is a random string that is used to protect your Flask application from attack. It is used to encrypt cookies and other sensitive data. If an attacker can guess your secret key, they could potentially steal your users' session data or other sensitive information.
77+
78+
`SECRET_KEY` is a random string that is used to protect your Flask application from attack.
79+
It is used to encrypt cookies and other sensitive data. If an attacker can guess your secret key, they could potentially steal your users' session data or other sensitive information.
6580

6681
Using the `python-dotenv` package, the contents of the `.env` file are loaded into environmental variables when the application starts.
6782

68-
### Enabling database migration for application
83+
<br />
84+
85+
## Handle `DataBase Migration`
86+
6987
Migrations are a way to track changes to your database schema over time. This makes it easy to roll back changes if something goes wrong or to deploy your application to a new environment.
7088
`Flask-Migrate` is a Flask extension that provides support for database migrations.
7189

7290
- From the terminal install flask-migrate
7391
```bash
74-
(venv) flask-berry-dashboard$ pip install flask-migrate
92+
(venv) $ pip install flask-migrate
7593
```
7694

7795
- In the entry point of your application (`run.py`) add the following line of code
@@ -88,21 +106,27 @@ Migrate(app, db)
88106
- Next step is to create the migration directory and apply the migration to the current database schema
89107

90108
```bash
91-
(venv) flask-berry-dashboard$ flask db init
92-
(venv) flask-berry-dashboard$ flask db migrate --message "initial db migration"
109+
(venv) $ flask db init
110+
(venv) $ flask db migrate --message "initial db migration"
93111
```
94112

95113
By setting up `flask-migrate` you can be able to move scale your application database and alter its schema without losing data.
96114

97-
### Minifying application files using `Flask-Minify`
98-
`Flask-Minify` is a Flask extension that can be used to minify HTML, CSS, and JavaScript files. Minifying files can help to improve the performance of your Flask application by reducing the size of the files that need to be downloaded by the user.
115+
<br />
116+
117+
## Compressing Assets via `Flask-Minify`
118+
119+
`Flask-Minify` is a Flask extension that can be used to minify HTML, CSS, and JavaScript files.
120+
Minifying files can help to improve the performance of your Flask application by reducing the size of the files that need to be downloaded by the user.
121+
122+
> Install flask-minify from the terminal with the command
99123
100-
- Install flask-minify from the terminal with the command
101124
```bash
102-
(venv) flask-berry-dashboard$ pip install flask-minify
125+
(venv) $ pip install flask-minify
103126
```
104127

105-
- Add the following to `run.py` to enable the application to use `flask-minify`
128+
> Add the following to `run.py` to enable the application to use `flask-minify`
129+
106130
```py
107131
# run.py
108132
...
@@ -112,15 +136,19 @@ from flask_minify import Minify
112136
Minify(app=app, html=True, js=False, cssless=True)
113137
...
114138
```
115-
Test the minification of your files to make sure that it does not break your application. Minification can sometimes introduce errors in your files, so it is important to test them thoroughly before you deploy your application.
116139

117-
### Increasing Application security using `Flask-Talisman`
140+
Test the minification of your files to make sure that it does not break your application.
141+
Minification can sometimes introduce errors in your files, so it is important to test them thoroughly before you deploy your application.
142+
143+
<br />
144+
145+
## Increasing Security using `Flask-Talisman`
118146

119147
`Flask-Talisman` is a Flask extension that helps to secure your Flask application by setting HTTP security headers. These headers can help to protect your application from a variety of common web attacks, such as cross-site scripting (XSS), clickjacking, and session hijacking.
120148

121149
- Install `Flask-Talisman` from the terminal
122150
```bash
123-
(venv) flask-berry-dashboard$ pip install flask-talisman
151+
(venv) $ pip install flask-talisman
124152
```
125153

126154
- Setup flask-talisman by adding this line of code in `run.py`
@@ -134,31 +162,35 @@ talisman = Talisman(app)
134162
```
135163
With this, we have added a new layer of security to our application in a production environment.
136164

137-
### Serve static files using CDN
138-
A content delivery network (CDN) is a network of servers that are distributed around the world. CDNs can be used to improve the performance of web applications by delivering static content, such as images and JavaScript files, from the server that is closest to the user.
165+
<br />
166+
167+
## Serve Static Files via `CDN`
168+
169+
A content delivery network (CDN) is a network of servers that are distributed around the world.
170+
CDNs can be used to improve the performance of web applications by delivering static content, such as images and JavaScript files, from the server that is closest to the user.
139171

140172
To deploy a Flask application using a CDN, you will need to:
141173

142174
- Choose a CDN provider. There are many CDN providers available, such as Amazon CloudFront, Cloudflare, and Akamai.
143-
144175
- Create an account with the CDN provider.
145-
146176
- Upload your static content to the CDN provider in a compressed format. This will reduce the size of the files and improve the performance of your application.
147-
148177
- To configure your Flask application to use the CDN, add the following to the `.env` file
178+
149179
```
150180
# .env
151181
# CDN Support
152182
# Note: Used only when DEBUG=False (production mode)
153183
# CDN_DOMAIN="YYY.ZZZ.com/berry-bootstrap5-v101"
154184
```
155185

156-
- Install `Flask-cdn` from the terminal
186+
> Install `Flask-cdn` from the terminal
187+
157188
```
158-
(venv) flask-berry-dashboard$ pip install Flask-CDN
189+
(venv) $ pip install Flask-CDN
159190
```
160191

161-
- Adding `flask-cdn` to the flask application, `apps/__init__.py` contain a function for creating a flask object, that is where we will be initializing our CDN
192+
> Adding `flask-cdn` to the flask application, `apps/__init__.py` contain a function for creating a flask object, that is where we will be initializing our CDN
193+
162194
```py
163195
# apps/__init__.py
164196
...
@@ -175,44 +207,62 @@ def create_app():
175207
return app
176208
```
177209

178-
- Add the CDN domain to your python flask configuration.
210+
> Add the CDN domain to your python flask configuration.
179211
180-
* Using a configuration file `apps/config.py`
181-
```py
182-
# apps/config.py
183-
...
184-
class Config:
185-
...
186-
CDN_DOMAIN = os.getenv('CDN_DOMAIN' , None)
187-
...
188-
...
189-
```
190-
* From the flask object "app" in run.py
191-
```py
192-
# run.py
212+
* Using a configuration file `apps/config.py`
213+
214+
```py
215+
# apps/config.py
216+
...
217+
class Config:
193218
...
194-
app.config['CDN_DOMAIN'] = os.getenv('CDN_DOMAIN' , None)
219+
CDN_DOMAIN = os.getenv('CDN_DOMAIN' , None)
195220
...
196-
```
221+
...
222+
```
223+
224+
* From the flask object "app" in run.py
225+
226+
```py
227+
# run.py
228+
...
229+
app.config['CDN_DOMAIN'] = os.getenv('CDN_DOMAIN' , None)
230+
...
231+
```
197232

198233
With this done, our application is ready to serve static files over a CDN.
199234

235+
<br />
236+
200237
## Deploying Flask Application
201-
Deploying a Flask application is a crucial step in turning your Python web application into a fully functional and accessible website. It involves the process of making your Flask application available to users on a live server, ensuring its stability, security, and optimal performance. Without proper deployment, your Flask application may not reach its intended audience or function as expected.
238+
239+
Deploying a Flask application is a crucial step in turning your Python web application into a fully functional and accessible website.
240+
It involves the process of making your Flask application available to users on a live server, ensuring its stability, security, and optimal performance.
241+
Without proper deployment, your Flask application may not reach its intended audience or function as expected.
202242

203243
Deploying a Flask application goes beyond simply running it on your local development environment. It requires careful planning, configuration, and implementation of various steps to ensure a successful deployment.
204244

205-
Before you start the process of deploying your application, execute the command below on your terminal to create an updated `requirements.txt` file
245+
Before you start the process of deploying your application, execute the command below on your terminal to create an updated `requirements.txt` file.
246+
206247
```
207-
(venv) flask-berry-dashboard$ pip freeze > requirements.txt
248+
(venv) $ pip freeze > requirements.txt
208249
```
209250

251+
<br />
252+
210253
### Deploy using Docker
211-
Docker is a popular containerization platform that allows you to package and distribute applications along with their dependencies in a portable and isolated environment. It provides a consistent and reproducible way to build, ship, and run applications, making it easier to deploy applications across different environments without worrying about compatibility issues.
254+
255+
Docker is a popular containerization platform that allows you to package and distribute applications along with their dependencies in a portable and isolated environment.
256+
It provides a consistent and reproducible way to build, ship, and run applications, making it easier to deploy applications across different environments without worrying about compatibility issues.
212257

213258
For this tutorial, we will be using `Nginx` as the reverse proxy server and `Gunicorn to serve the application.
214259

215-
A reverse proxy is a server that sits between the internet and a web application. It can be used to improve the performance, security, and reliability of the web application. Gunicorn is an application server that can be used to run web applications. It is designed to be used with a reverse proxy. If Gunicorn is exposed directly to the internet, it can be vulnerable to denial-of-service (DoS) attacks. A DoS attack is an attempt to make a computer or network resource unavailable to its intended users. In the case of Gunicorn, a DoS attack could be performed by creating a load that trickles data to the servers. This type of attack is known as a Slowloris attack.
260+
A reverse proxy is a server that sits between the internet and a web application. It can be used to improve the performance, security, and reliability of the web application.
261+
Gunicorn is an application server that can be used to run web applications. It is designed to be used with a reverse proxy.
262+
If Gunicorn is exposed directly to the internet, it can be vulnerable to denial-of-service (DoS) attacks.
263+
A DoS attack is an attempt to make a computer or network resource unavailable to its intended users. In the case of Gunicorn, a DoS attack could be performed by creating a load that trickles data to the servers.
264+
265+
This type of attack is known as a Slowloris attack.
216266

217267
We will start by configuring our docker container.
218268

@@ -314,12 +364,17 @@ CMD ["gunicorn", "--config", "gunicorn-cfg.py", "run:app"]
314364
```
315365

316366
- Execute the command to build the docker image and deploy the application on docker.
367+
317368
```bash
318-
(venv) flask-berry-dashboard$ docker-compose up --build
369+
(venv) $ docker-compose up --build
319370
```
371+
320372
Now your application is running on docker. If you are running the application on your machine, visit [`http://localhost:5085`](http://localhost:5085) to access your application.
321373

322-
### Deploy on Render
374+
<br />
375+
376+
### `Deploy on Render`
377+
323378
Deploying Flask application on render is straightforward
324379

325380
- Create an account on [render](https://render.com)
@@ -333,43 +388,53 @@ Deploying Flask application on render is straightforward
333388
* **Runtime** - Python
334389
* **Build Command** - `pip install requirements.txt`
335390
* **Start Command** - `gunicorn run:run`
391+
336392
Create a web service and check it out with your browser on `https://<service-name>.onrender.com`
337393

338-
### Deploy on Heroku
339-
- Create an account with [heroku](https://signup.heroku.com/login)
394+
<br />
340395

341-
- After account creation, download and install Heroku cli [here](https://signup.heroku.com/login)
396+
### `Deploy on HEROKU`
342397

398+
- Create an account with [heroku](https://signup.heroku.com/login)
399+
- After account creation, download and install Heroku cli [here](https://signup.heroku.com/login)
343400
- Once the installation is complete, you have to log in using the command
401+
344402
```bash
345-
(venv) flask-berry-dashboard$ heroku login
403+
(venv) $ heroku login
346404
```
347405

348406
- Create a file `Procfile` with the command for starting the application by executing the command
407+
349408
```
350-
(venv) flask-berry-dashboard$ echo "web: gunicorn run:app" > Procfile
409+
(venv) $ echo "web: gunicorn run:app" > Procfile
351410
```
352411

353412
- Stage and commit the changes with git
413+
354414
```bash
355-
(venv) flask-berry-dashboard$ git add .
356-
(venv) flask-berry-dashboard$ git commit -m "Changes for deployment"
415+
(venv) $ git add .
416+
(venv) $ git commit -m "Changes for deployment"
357417
```
358418

359419
- Create the Heroku application by executing the command and push the code base to the Heroku repository that the application will be hosted from by executing the commands below
420+
360421
```
361-
(venv) flask-berry-dashboard$ heroku create <webapp-name>
362-
(venv) flask-berry-dashboard$ git push heroku master
422+
(venv) $ heroku create <webapp-name>
423+
(venv) $ git push heroku master
363424
```
364425
This will set up the application and start the Gunicorn server.
365426

366427
- Open the application by executing the command
367428
```
368-
(venv) flask-berry-dashboard$ heroku open
429+
(venv) $ heroku open
369430
```
370431

432+
<br />
433+
371434
## Conclusion
372-
In this article, we have covered the steps involved in setting up, preparing, and deploying a Flask application. We have also discussed some of the best practices for securing and optimizing your application.
435+
436+
In this article, we have covered the steps involved in setting up, preparing, and deploying a Flask application.
437+
We have also discussed some of the best practices for securing and optimizing your application.
373438

374439
Here are some of the key takeaways from this article:
375440

@@ -378,14 +443,11 @@ Here are some of the key takeaways from this article:
378443
- You can improve the security of your application by using Flask-Talisman and serving static files using a CDN.
379444
- There are a variety of deployment options available for Flask applications, including Docker, Render, and Heroku.
380445

381-
I hope this article has been helpful. For more information on Flask, please visit the official website: https://flask.palletsprojects.com/en/2.1.x/
446+
We hope this article has been helpful. For more information on Flask, please visit the official website: https://flask.palletsprojects.com/en/2.1.x/
382447

383448
<br />
384449

385450
## Resources
386-
- 👉 [Flask-Migrate](https://flask-migrate.readthedocs.io/en/latest/) documentation
387-
- 👉 [Flask-Minify](https://github.com/mrf345/flask_minify/) Homepage
388-
- 👉 [Flask-Talisman](https://github.com/wntrblm/flask-talisman) Homepage
389-
- 👉 [Replit](https://replit.com/) - the official website
451+
390452
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
391453
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts

0 commit comments

Comments
 (0)