0

This is a React project but I want to just test the Flask environment for now.

My directories:

api
   |app
       |_py_cache
       | .flaskenv
       | api.py
   |venv

api.py

  import time
  from flask import Flask

  app = Flask(__name__)

  @app.route('/time')
  def get_current_time():
       return {'time': time.time()}

.flaskenv

 FLASK_APP=api.py
 FLASK_ENV=development

package.json

  "scripts": {
"start": "react-scripts start",
"start-api": "cd api && venv\\Scripts\\activate && cd app && flask run --no-debugger",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
 },
 "proxy": "http://127.0.0.1:5000/"

After using flask run and going to the url, I get this error: flask.cli.NoAppException: Failed to find Flask application or factory in module "api". Use "FLASK_APP=api:name to specify one.

ShridharK
  • 365
  • 2
  • 14

2 Answers2

1

You might need to add app.run() in api.py

Arnav Deo
  • 33
  • 1
  • 3
1

This is a common mistakes in beginners. You have to add the following code at the end of your code of api.py :

if __name__ == '__main__':
   app.run()

Or the best practice:

if __name__ == '__main__':
   app.run(debug=True)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Anandakrishnan
  • 349
  • 5
  • 10