Skip to content

Commit 3768334

Browse files
committed
Debugging Flask - Introduction for Developers
1 parent ff11ee1 commit 3768334

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
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+
## [UNRELEASED]
4+
### Changes
5+
6+
- [Debugging Flask](https://docs.appseed.us/technologies/flask/debugging/) `Guide for Developers`
7+
38
## [1.0.68] 2023-10-25
49
### Changes
510

docs/technologies/flask/debugging.mdx

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
1-
# Debugging
2-
Flask debug mode is defined as a module that ensures insights regarding debug pushed as an extension for Flask during development of Flask application. This capability is incorporated as a part of development tools in developing any Flask application in a development server and option to enable it in production environment as well. the interactive traceback is one of the many great insight which allows any developer to look back into the code and asses what went wrong in order to effectively improve and fix the code. One effective and often known to be a best practice is to use debugger in development environment although one can also choose to use it in production, but only use it temporarily!
1+
---
2+
title : Debugging Flask - Guide for Developers
3+
sidebar_label : Debugging
4+
---
35

4-
### Why do we need a flask to debug mode?
5-
In order to effectively find and eliminate errors in the application being developed we need the debug mode the most so that once the errors are fixed the application can qualify to be the most valuable product and bug free application increases its own value immensely. Not only that the debug mode helps to improve the code quality but also helps you reach to a point in the code where there might be a possible break of the code.
6+
# Debugging Flask
67

7-
Let us know about possible errors through a simple example. In our code there is a mathematical calculation where we divide 2 numbers. Let us say that in some situations the denominator be zero. In this case the integer datatype might be incapable of handling such scenarios. Now, in a code where there are multiple modules, if is nearly impossible to find where the error has been arising from and even at a higher level, what is the error. Now in case of Flask server, if the debug mode is not ON, we would just see an error code like 404 or in some cases be Internal Server error etc. Now, finding out the type of error and where the error has generated is a herculean task! Now, in case of running the code with debug mode ON in Flask we will be able to traceback where the error has occurred and also what the error is.
8+
<SubHeading>Debugging Flask, a short introduction for developers</SubHeading>
9+
10+
Flask debug mode is defined as a module that ensures insights regarding debug pushed as an extension for Flask during development of Flask application.
11+
This capability is incorporated as a part of development tools in developing any Flask application in a development server and option to enable it in production environment as well.
12+
13+
The interactive traceback is one of the many great insight which allows any developer to look back into the code and asses what went wrong in order to effectively improve and fix the code.
14+
15+
![Debugging Flask - Tutorial provided by AppSeed.](https://user-images.githubusercontent.com/51070104/268566349-c41e65a5-2ab9-4b54-8cbc-350ab6da746c.png)
16+
17+
## ✅ Why do we need a Flask to debug mode?
18+
19+
In order to effectively find and eliminate errors in the application being developed we need the debug mode the most so that once the errors are fixed the application
20+
can qualify to be the most valuable product and bug free application increases its own value immensely.
21+
22+
Not only that the debug mode helps to improve the code quality but also helps you reach to a point in the code where there might be a possible break of the code.
23+
24+
Let us know about possible errors through a simple example. In our code there is a mathematical calculation where we divide 2 numbers. Let us say that in some situations the denominator be zero.
25+
26+
In this case the integer datatype might be incapable of handling such scenarios. Now, in a code where there are multiple modules, if is nearly impossible to find where the error has been arising from and even at a higher level, what is the error. Now in case of Flask server, if the debug mode is not ON, we would just see an error code like 404 or in some cases be Internal Server error etc. Now, finding out the type of error and where the error has generated is a herculean task! Now, in case of running the code with debug mode ON in Flask we will be able to traceback where the error has occurred and also what the error is.
827

928
Flask server also provides an interactive tool to actually know what the value before the error was, has been encountered as it has the utility of executing python code from the browser and using that interactive tool one can easily get into the most granular level to find the issue and then fix it. Now, as a best practice we should never use debug mode in production. The reasons are as follows:
1029

1130
- The main and the foremost reason is performance. When one uses debugging mode, there are chances of significantly slowing down the execution speed as the RAM utilization increases.
1231
- The next reason is a security breach which might be possible in case of using debug mode although there is a PIN attached to tracebacks, but possibility of security breach does exist and that too in production data!
1332

33+
## ✅ How does Flask debug mode work?
1434

15-
16-
### How does Flask debug mode work?
1735
It is now time for us to look into the working of Flask debug mode as by this time we know the need of Flask debug mode. For the reference we will be using excerpts from built-in debugger of Flask application server i.e. Werkzeug development server. The in-built debugger is recommended to be used only in case of development stages. In production environment debugger allows execution of arbitrary python code and though protected by PIN can’t be relied on for security.
1836

1937
First, we would need to set the environment to development by setting the FLASK_ENV variable. this is done by executing:
2038

2139
```python
2240
set FLASK_ENV=development
2341
```
24-
### Advantages and disadvantages
42+
43+
## ✅ Advantages and disadvantages
44+
2545
Not every concept is perfect and hence consists of pros and cons. Let’s review the pros and cons of Flask debug mode here:
2646

2747
### Advantages
@@ -32,15 +52,17 @@ Not every concept is perfect and hence consists of pros and cons. Let’s review
3252
- In addition to the traceback, the text which an error points, enables easy interpretations!
3353

3454
### Disadvantages
35-
- Flask’s debug mode, if left switched on in production leads to performance issues.
55+
56+
- Flask`s debug mode, if left switched on in production leads to performance issues.
3657
- In production, usage of debug mode can lead to security issues, as one can run some arbitrary python codes to hack into sensitive “production” data.
3758
- Limited usage to development environment only!
3859

39-
### Examples
60+
## Examples
4061

4162
Here are the following examples mention below
4263

4364
### Example #1
65+
4466
Running with Environment as development:
4567

4668
**Syntax** (debugMode.py is a python code that contains a flask application)
@@ -56,6 +78,7 @@ python debugMode.py
5678
![de](https://github.com/app-generator/docs/assets/150575074/4ec54cbc-11df-4a04-a4c4-9d24a4ec62ea)
5779

5880
### Example #2
81+
5982
Running with Environment as production:
6083

6184
**Syntax** (debugMode.py is a python code that contains a flask application)
@@ -80,20 +103,39 @@ Syntax (debugMode.py is a python code that contains a flask application with del
80103
In python code:
81104

82105
```python
106+
83107
from flask import Flask
84108
appFlask = Flask(__name__)
85-
@appFlask.route('/home')
109+
110+
@appFlask.route('/')
86111
def home():
87-
result = 10/0
88-
return 'We are learning HTTPS @ EduCBA'
89-
if __name__ == "__main__":
90-
appFlask.run(debug=True)
112+
result = 10/0
113+
return 'We are learning how to debug Flask'
91114

115+
if __name__ == "__main__":
116+
appFlask.run(debug=True)
92117
```
93118

94119
**Output:**
95120

96121
![de2](https://github.com/app-generator/docs/assets/150575074/57c754c6-1922-4601-93a9-cb11eb88194c)
97122

98-
### Conclusion
99-
Herewith this article, we have got an essence of how a debugger works and its advantages and disadvantages owing to the usage of the debugger in the development server. We also looked at a deliberate error to understand the stack trace of the error, particularly pointing out the point of generation of error!
123+
## ✅ Conclusion
124+
125+
Herewith this article, we have got an essence of how a debugger works and its advantages and disadvantages owing to the usage of the debugger in the development server.
126+
We also looked at a deliberate error to understand the stack trace of the error, particularly pointing out the point of generation of error.
127+
128+
## ✅ Free Flask starters
129+
130+
To move forward from theory to practice, feel free to access a few Flaks Starters crafted and actively supported by AppSeed
131+
132+
- [Rocket Flask](https://appseed.us/product/rocket/flask/), our latest starter built on top of `Tailwind/Flowbite`
133+
- [Datta Able Flask](https://appseed.us/product/datta-able/flask/), a popular open-source Flask Dashboard styled with Boostrap
134+
135+
136+
## ✅ Resources
137+
138+
- 👉 Access [AppSeed](https://appseed.us/) for more starters and support
139+
- 👉 [Deploy Projects on Aws, Azure and DO](https://www.docs.deploypro.dev/) via **DeployPRO**
140+
- 👉 Create landing pages with [Simpllo, an open-source site builder](https://www.simpllo.com/)
141+
- 👉 Build apps with [Django App Generator](https://app-generator.dev/django/) (free service)

0 commit comments

Comments
 (0)