When you create a new Flask application, it comes with a built-in development server. Which is good because it helps you see errors and automatically reloads when you make changes. However, if you start your app using this server, you can only access it from your own computer, because it only listens on localhost (127.0.0.1). This means you can’t access this application from other devices on the same network

So the question is, can we change this to allow access the application from other devices on the same network using the IP address of your computer? Answer is “Yes”. This tutorial will help you to configure your Flask application to accessible on network.

Changing Host IP Address in Flask

By default, Flask’s development server is set to only work on your computer (localhost) for security reasons. The official Flask documentation says:

How to Configure Flask Application Visible on the Network Flask Network Python Python Framework
Flask official documentation on default host address

If you trust the other devices on your network or if you have disabled the debugger, you can change this.

The host variable decides which IP addresses the server listens to. Normally, host is set to 127.0.0.1 or localhost. To make your app accessible on the network, you can change this to 0.0.0.0. Setting host to 0.0.0.0 tells the operating system to listen on all available public IP addresses.

There are two ways to do this. You can choose anyone of them.

Method 1: Using the Flask Command

If you start your app with the flask run command, you can add the --host flag like this:


flask run --host=0.0.0.0

Then, on another device in the same network, open a web browser and type your computer’s IPv4 address followed by the port number like this:

  • http://[your-ipv4-address]:5000
  • This will let you see your app from another device.

    Method 2: Using the run() Method

    If you start your Flask application with python app.py, you can change the app.run() method to include the host='0.0.0.0' parameter, like this:

    
    if __name__ == '__main__':
        # run app in debug mode on port 5000
        app.run(debug=True, port=5000, host='0.0.0.0')
    
    

    Then, on another device in the same network, open a web browser and type your computer’s IPv4 address followed by the port number like this:

  • http://[your-ipv4-address]:5000
  • Now, you should be able to access your app from any device on your network.

    Conclusion

    By following these simple steps, you can make your Flask application accessible from other devices on your local network. This is especially useful for testing and demonstrating your app on different devices. Whether you use the flask run command or modify the app.run() method, setting the host to 0.0.0.0 will allow your app to listen on all network interfaces attached to your system that allows application access to public IP addresses as well. Just remember to use your computer’s IPv4 address followed by the port number to access the application from other devices.

    Hope this tutorial helps you to allow your flask application over the network.