I am currently working on the user management part of an application through Python while using Flask web framework, and I need a bit of help creating a SQLite database through SQLAlchemy.
Here's a snippet from the __init__.py file from my backend package.
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from backend.config import Config
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'
mail = Mail()
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
from backend.users.routes import users
from backend.stats.routes import stats
from backend.queues.routes import queues
app.register_blueprint(users)
app.register_blueprint(stats)
app.register_blueprint(queues)
return app
Here's a snippet from my main run.py file.
from backend import create_app
app = create_app()
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 80, debug = True)
The config of the app refers the SQLALCHEMY_DATABASE_URI to be 'sqlite:///site.db'.
I have tried pushing the app through app_context. I have tried creating the database tables within it (to be frank, I have tried literally all of the workarounds I could find over the internet). Does anyone have any suggestions as to how I can tackle this problem (apart from creating the tables manually -- doing that does work, but that does not seem to be a long-term solution).
As a quick note, here's the error I am receiving.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 963, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 940, in _execute_for_all_tables
app = self.get_app(app)
File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 912, in get_app
'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.