Flask page doesn't open, or returns nothing

  • Flask
  • Raspberry Pi

You may see

TypeError: The view function for 'h' did not return a valid response. The function either returned None or ended without a return statement.
Address already in use
404 Not Found

Stay safe: Use it on your home network only, and switch off debug mode there.

Why it happens

  • host isn't set, so only this computer can open it.
  • Another copy is still running.
  • The function has no return.
  • The firewall blocks it.

How to fix it

  1. Use app.run(host="0.0.0.0", port=5000).
  2. Open http://<the computer's IP>:5000 from the phone on the same Wi-Fi.
  3. Stop the old copy with Ctrl+C, or use another port.
  4. End each view function with return.

It worked if: The terminal prints a line with 200 when you open the page.

Seen in a real run (some lines are close variants of it). The TypeError came from Flask 3.1.2 here; the others are Flask's usual wording. Sources: Flask: quickstart.

Didn't help? Search another error or ask us.