0

I've already read much about it on stack overflow but now I'm struggling with an issue and don't know how to fix it.

My main.py looks like the following:

from flask import Flask, jsonify, request

from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from flask_cors import CORS

app = Flask(__name__)


# connect to LiTags database
app.config["MONGO_URI"] = "mongodb://localhost:27017/LiTags"

mongo = PyMongo(app)

CORS(app)

if __name__ == "__main__":
    app.run(debug=True)

then I seperated some database requests (MongoDB) in another file(requests.py). I know that it's not necessary right now because there are not that much requests right now but there will be more after I fixed this problem.

So this is my requests.py:

from main import app, mongo
from flask import Flask, jsonify, request, Blueprint
from flask_pymongo import PyMongo
from bson.objectid import ObjectId


@app.route("/", methods=["GET"])
def getProjects():
    result = []
    projects = mongo.db.literature

    for field in projects.find():
        result.append(
            {"_id": str(field["_id"]), "project_name": field["project_name"]})

    return jsonify(result)

this is generally my frontend (ReactJS) request to flask:

// get project names from the backend
  getProjects() {
    axios.get(`http://127.0.0.1:5000/`)
      .then(res => {
        this.setState({ projects: res.data });
      })
  }

before I seperated the requests in requests.py from the main.py my program worked but now I get an error in the inspect tool in the browser which says:

"GET http://127.0.0.1:5000/ 404 (NOT FOUND)
Error: Request failed with status code 404
    at createError (createError.js:17)
    at settle (settle.js:19)
    at XMLHttpRequest.handleLoad (xhr.js:63)"
Till
  • 15
  • 4
  • BTW: most people and tutorials/documentations use name `views.py` instead of `requests.py` for file with code which use `@app.route`. – furas Apr 09 '20 at 16:41
  • please check: https://stackoverflow.com/questions/11994325/how-to-divide-flask-app-into-multiple-py-files – Yosua Apr 09 '20 at 16:43
  • you have to `import requests` in `main.py` - without import it never use code from `requests.py` so it doesn't use code `@app.route("/", methods=["GET"])` and when browser try to connect to `http://127.0.0.1:5000/` then Flask doesn't know what to do. – furas Apr 09 '20 at 16:43
  • BTW: you will have problem with `circular import` because in `main.py` you have to import `requests.py` but `requests.py` already imports `main.py`. You would have to create new file ie. `run.py` which imports first from `main.py`, next from `requests.py` and finally it runs `app.run()` – furas Apr 09 '20 at 16:55

1 Answers1

1

In main.py you would have to import requests.py

from requests import *

because main.py doesn't know that you have code in other file .

But there can be problem with circular imports - main.py has to import requests.py but requests.py aready imports main.py and it makes problem.

You may have to split main in two files:

main.py - which doesn't need code from reuqests so it doesn't have to import it

from flask import Flask, jsonify, request

from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from flask_cors import CORS

app = Flask(__name__)

# connect to LiTags database
app.config["MONGO_URI"] = "mongodb://localhost:27017/LiTags"

mongo = PyMongo(app)

CORS(app)

run.py - which needs code from requests (but requests doesn't import it)

from main import *
#from main import app
from requests import *

#from models import *
#from views import *
#from admin import *    

if __name__ == "__main__":
    app.run(debug=True)

and now you would have to use run.py instead of main.py to start server.

python run.py

BTW: most people and tutorials/documentations use name views.py instead of requests.py for file with code which use @app.route. Eventaully I would use name api.py because these views return JSON data like in many APIs.

furas
  • 134,197
  • 12
  • 106
  • 148