Sunday, December 29, 2024
Google search engine
HomeLanguagesFlask Development Server

Flask Development Server

What is Flask?

Flask is a micro-web-framework based on python. Micro-framework is normally a framework with little to no external dependencies on libraries. Though being a micro-framework Flask is as effective as any other web framework because of its wide range of available python libraries like SQLAlchemy, Flask-Migrate, etc. In this article, we will be discussing what a development server is and why is it used.

What is a development server?

A development server is a server that is used in the development, testing of programs, websites, software, or applications by developers. It provides a runtime environment as well as all hardware/software utilities that are required for program debugging and development.

You can use a development server to check whether your web application is working as expected or not. In flask when debug settings are set to true, you can also use the development server to debug your application.

In this article, we will create a single-page flask-based web application and explain the different methods by which you can run your development server.

Creating Flask Web Application –

Module Installation: To install flask using pip(package installer for python) run the following command:

pip install flask

Example: Following is the code for a simple flask application that has a single page and we will use the development server to check whether the page is served in the application as expected.

Filename: app.py

python




from flask import Flask, render_template
  
app = Flask(__name__)
  
# Debug setting set to true
app.debug = True
  
@app.route('/')
def index():
    return "Greetings from Lazyroar"
  
if __name__ == '__main__':
    app.run()


Starting development server: Starting a development server in the flask using the following command.

python <name>.py

Here <name> is the name of the file where the instance of the flask app has been created and the app.run() function exists. By convention, this file is mostly named app, thus the command will be shown below.

python app.py

The development server will open up on http://127.0.0.1:5000/ and you will see the following output on your browser screen.

Greetings from Lazyroar

Lazy Loading Or Eager Loading – When using the development server it will continue to run even if you introduce syntax errors or other initialization errors into the code. Accessing the site with the errors will show the interactive debugger for the error, rather than crashing the server. This feature is called lazy-loading. In simpler words in lazy-loading, the server does not crash even if something goes wrong in your application rather an informative debugger page loads up.

To activate lazy loading you can modify the command as follows:

python app.py --lazy-loading

Now, in eager loading, if an error is present in the application code or some probable section of the application, then rather than loading the interactive debugger the development server fails with a detailed traceback of the error. 

To activate eager loading you can modify the command as follows:

python app.py --eager-loading

Reference: https://flask.palletsprojects.com/en/2.0.x/

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments