In our previous post, we introduced the Flask and built a most simple flask application. So, now we want to run this application we created in this video.

How to Run Flask App?

There are two possible ways exporting an

  1. Environment variable
  2. Programmatically

First. let’s take a look at how we can run it by exporting the environment variable.

1: Environment Variable

Flask applications include a development web server that can be started with the flask run command. This command looks for the name of the Python script that contains the application instance in the FLASK_APP environment variable. 

Create A Virtual Environment:

To start our application, first, make sure the virtual environment you created earlier is activated then define the environment variable as export FLASK_APP=app.py make sure that you are inside the right directory. If you are on windows you can export that variable by using the command as:

set FLASK_APP=app.py    

Once the server starts up, it goes into a loop that accepts requests and services them. This loop continues until you stop the application by pressing Ctrl+C. With the server running, open your web browser and type:

http://localhost:5000/

You can see our application is working successfully, If you type anything else after the base URL, the application will not know how to handle it and will return an error code 404 to the browser the familiar error that you get when you navigate to a web page that does not exist.

2: Programmatically:

Now, let’s try another way to run this application, The Flask development web server can also be started programmatically by invoking the app. run() method. Older versions of Flask that did not have the flask command required the server to be started by running the application’s main script, which had to include the main section as if name == main then inside this section we have to call app. run(), now if we run this file as python app.py you can see our application is working in the same way as it did by using the environment variable. While the flask run command makes this practice unnecessary, the app. run() method can still be useful on certain occasions, such as unit testing.

Dynamic Routing:

You may have seen URLs where we pass some parameters and get the result accordingly. These kinds of URLs are called Dynamic Routes in the context of the flask. So, let me define another route in our application.

@app.route('/')
def print_name(name):
    return 'Hi , {}' .format(name)

To test the dynamic route, make sure the server is running and then navigate to 
http://localhost:5000/<YOUR_NAME>

I will pass Abdul, The application will respond with the personalized greeting using the name dynamic argument. Try using different names in the URL to see how the view function always generates the response based on the name given. There’s a lot more in routing because it’s an important part of APIs implementation.

Debugging in Flask:

 Flask applications can optionally be executed in debug mode. In this mode, two very convenient modules of the development server called the Reloader and the Debugger are enabled by default. 

Reloader:

When the reloader is enabled, Flask watches all the source code files of your project and automatically restarts the server when any of the files are modified. Having a server running with the reloader enabled is extremely useful during development, because every time you modify and save a source file, the server automatically restarts and picks up the change.

Debugger:

A debugger is a web-based tool that appears in your browser when your application raises an unhandled exception. The web browser window transforms into an interactive stack trace that allows you to inspect source code and evaluate expressions in any place in the call stack. 

So, we can enable this mode according to the method we choose to run our application if you are running your application by exporting the FLAS_APP variable, we have to export another variable as FLASK_DEBUG=1 but if you are running your application programmatically, we can pass debug=True in our app. run call.

If we run our application, it’s inside the debug mode and if I make a change in my code it will restart the server picks the change automatically by using the reloader module and if I make a mistake it throughs an unhandled exception it will provide an interactive stack trace to trace the error by using the debugger module. 

Command-line options:

The flask command supports a number of options. To see what’s available, you can run

flask --help

or just write flask without any arguments: you can see it brings all the commands, options, examples, and descriptions.

The flask shell command is used to start a Python shell session in the context of this application. You can use this session to run maintenance tasks or tests or to debug issues.

The --host argument is particularly useful because it tells the web server what network interface to listen to for connections from clients. By default, Flask’s development web server listens for connections on localhost, so only connections originating from the computer running the server are accepted.

But if we run the command as

flask run --host

and pass an IP address.

The web server should now be accessible from any computer in the network at that particular IP address. The --reload, --no-reload, --debugger, and --no-debugger options provide a greater degree of control on top of the debug mode setting. For example, if debug mode is enabled, the --no-debugger can be used to turn off the debugger while keeping debug mode and the reloader enabled. 

Posted 
Dec. 29, 2021
 in 
Web Development
 category

More from 

Web Development

 category

View All