i’m doing a bit of a Fullstack tutorial. So please don’t be mean this might be a noob Question:
I wrote this liddle small Flask Application out of a Tutorial
config.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///mydatabase.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
main.py
from flask import request, jsonify
from config import app, db
from models import Contact
@app.route("/contact", methods=["GET"]) # Decorator with route and method
def get_contact():
contacts=Contact.query.all() # Uses Flask SQL Alchemy to get all Contacts inside the Database
json_contacts = list(map(lambda x: x.to_json(),contacts)) # Call the List an MAP them into Json Content
return jsonify({"contacts":json_contacts}) # Write it in Json Data
if __name__ = "__main__":
with app.app_context(): # Does we have the Database?
db.create_all() #
app.run(debug=True) #
from config import db
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(80), unique=False, nullable=False)
last_name = db.Column(db.String(80), unique=False, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def to_json(self):
return {
"id": self.id,
"firstName": self.first_name,
"lastName": self.last_name,
"email": self.email,
}
So in the Tutorial he just do python3 main.py
and the Flask Server is starting. When Im doing this nothing happen. This Tutorial is just 3 Month old so sometimes i can’t believe the coding world. So that mean i have no mistakes in the Code itself.
so i tried : python3 -m flask --app main.py run
Output Error: Failed to find Flask application or factory in module 'main'. Use 'main:name' to specify one.
I double checked if i have any bad Code in there. The problem might be not a coding mistake , more than i start the APP.
Can you help me?
schuse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.