app-generator / docs

App Generator - The Official Documentation | AppSeed
https://docs.appseed.us
1 stars 1 forks source link

[Published] Flask Debugging #94

Open mahfujul-helios opened 1 month ago

mahfujul-helios commented 1 month ago

Flask Debugging

STATE: Published

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!

Why do we need a flask to debug mode?

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.

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.

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:

How does Flask debug mode work?

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.

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

set FLASK_ENV=development

Advantages and disadvantages

Not every concept is perfect and hence consists of pros and cons. Let’s review the pros and cons of Flask debug mode here:

Advantages

Disadvantages

Examples

Here are the following examples mention below

Example #1

Running with Environment as development:

Syntax (debugMode.py is a python code that contains a flask application)

In the command line:

set FLASK_ENV=development
python debugMode.py

Output:

de

Example #2

Running with Environment as production:

Syntax (debugMode.py is a python code that contains a flask application)

In the command line:

set FLASK_ENV=production
python debugMode.py

Output:

de1

Example #3

Running with error in code:

Syntax (debugMode.py is a python code that contains a flask application with deliberate error) In python code:

from flask import Flask
appFlask = Flask(__name__)
@appFlask.route('/home')
def home():
result = 10/0
return 'We are learning HTTPS @ EduCBA'
if __name__ == "__main__":
appFlask.run(debug=True)

Output:

de2

Conclusion

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!

app-generator commented 1 month ago

This is cool. TY @mahfujul-helios

app-generator commented 1 month ago

Please public this content under the Flask Node:

https://docs.appseed.us/technologies/flask/debugging/ [ new page ]

TY!