How to Run Flask on Local Network

Follow these steps to run a Flask app on your local network and allow other devices to access it via the IP address and port:

1. Modify the Flask App Code

In your Flask app, update the app.run() method to specify the host as 0.0.0.0. This allows Flask to listen for requests from any IP address.

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)  # You can change the port if needed
  • host='0.0.0.0' allows Flask to be accessed by any device on the network.
  • port=5000 specifies the port (default is 5000).

2. Find Your Local IP Address

You'll need the local IP address of the computer running the Flask app to access it from other devices.

  • Windows:
    1. Open Command Prompt.
    2. Type ipconfig and press Enter.
    3. Look for the "IPv4 Address" under your network connection.
  • Mac/Linux:
    1. Open Terminal.
    2. Type ifconfig (or ip addr on some systems) and press Enter.
    3. Find the "inet" value under your active network interface (like eth0 or wlan0).

The local IP address will look something like 192.168.x.x.

3. Run the Flask App

In your terminal or command prompt, navigate to the directory of your Flask app and run the following command:

python your_flask_app.py

4. Access the Flask App from Other Devices

On any device connected to the same network, open a web browser and go to:

http://<YOUR_LOCAL_IP>:5000

For example, if your local IP is 192.168.1.100, type the following in the browser:

http://192.168.1.100:5000

5. Firewall Considerations (Optional)

If you can't access the Flask app from another device, check your firewall settings. You may need to allow traffic on port 5000:

  • Windows:
    1. Open the "Windows Defender Firewall" settings.
    2. Click on "Advanced settings."
    3. Add a new inbound rule to allow connections on port 5000.
  • Linux:

    If you're using ufw, run the following command:

    sudo ufw allow 5000/tcp
  • MacOS:

    Go to "System Preferences" > "Security & Privacy" > "Firewall" and ensure that traffic on port 5000 is allowed.

Once this is done, your Flask app should be accessible from any device on the local network!