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:
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).You'll need the local IP address of the computer running the Flask app to access it from other devices.
ipconfig and press Enter.ifconfig (or ip addr on some systems) and press Enter.eth0 or wlan0).The local IP address will look something like 192.168.x.x.
In your terminal or command prompt, navigate to the directory of your Flask app and run the following command:
python your_flask_app.py
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
If you can't access the Flask app from another device, check your firewall settings. You may need to allow traffic on port 5000:
5000.If you're using ufw, run the following command:
sudo ufw allow 5000/tcp
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!