I'm trying to get a Flask "point of sale" application working on cPanel with no success.
Here is the directory structure on cPanel File Manager
I have my app = Flask(name) in init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_security import Security,SQLAlchemyUserDatastore
# from app.models import Role,User
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db, render_as_batch=True)
login = LoginManager(app)
from app import routes,models
I have tried using the line this line of code in my passenger_wsgi.py
from app import app as application
and in my pointofsale.py i used:
application = app
I have successfully created the python app and installed requirements.txt, when I try to load the link to the website but I cant reach the site.
Managed to figure out the issue, I had two files importing the application instance -: pointofsale.py and passenger_wsgi.py.
I deleted pointofsale.py, and added the following code to passenger_wsgi.py file
from app import app
application = app
Related
I currently have a flask app hosted on a waitress production server. I'd like to integrate plotly dash into my application without using werkzeug but cannot find any resources on how to do this.
The existing werkzeug solution:
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
from flask_app import flask_app
from app1 import app as app1
from app2 import app as app2
application = DispatcherMiddleware(flask_app, {
'/app1': app1.server,
'/app2': app2.server,
})
if __name__ == '__main__':
run_simple('localhost', 8050, application)
How do I do this without werkzeug?
I am unable to run the python manage.py runserver command from Azure. My manage.py code is
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask_migrate import upgrade as upgrade_database
from glogic import app, db, prepare_app
prepare_app(environment='ms')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()
Where glogic is the package for the bulk of my python files. The prepare app method is called from the __init__.py file which is as follows:
from glogic.config import config_env_files
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# initialises app and db
db = SQLAlchemy()
app = Flask(__name__)
# prepares the app and db environment based on config files
def prepare_app(environment='ms', p_db=db):
app.config.from_object(config_env_files[environment])
p_db.init_app(app)
# load views by importing them
from . import views
return app
Running and hosting locally it all works but I have been unable to find the correct startup command or what to put in a startup.sh file in order to run the server on Azure.
When running locally, I can start it using the command gunicorn --bind=0.0.0.0 --timeout 600 manage:app but this isn't working on Azure
I have a small Flask application. I add an extra security layer which is log in. I based my refactoring on the DO article.
In a nutshell,
__init__.py:
from flask import Flask
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash
from sqlalchemy import create_engine
# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'e56432f4402c9a0c166fe6c6a0a2fbbd'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
#login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
return app
In order to create DB, I need to run in REPL:
from project import db, create_app
db.create_all(app=create_app())
This is both inconvenient and makes Docker image creation harder. I run an application as flask run. I saw similar issues but don't understand how to apply to my scenario.
How can I overcome this complication?
UPDATE 1:
My project structure is the following:
project/
project/
__init__.py
auth.py
main.py
models.py
static/
templates/
Dockerfile
requirements.txt
If you want the database to be created when you run the flask app:
I would put this code
def createMyDatabase():
from project import db, create_app
db.create_all(app=create_app())
into the file makedatabase.py and save it into your program's root directory. Then add from <path to makedatabase.py>/makedatabase import createMyDatabase to the top of your __init__.py file and then just after your import statements in __init__.py write createMyDatabase(). I think that this would be the easiest way to do it in your situation.
If you don't want the database to be created every time that you run the program:
You could just take the function out of the makedatabase.py file and then run the file just as it is before you run the flask application.
I found a more elegant solution
Instead of return app make the following block, so an application will be created in the runtime if and only if the context was initialized and database created.
with app.app_context():
db.create_all()
return app
Surprisingly the Flask-SqlAlchemy is pointing out to REPL db creation as a first solution, not the one above.
UPDATE:
Here is an example of my Dockerfile:
FROM python:3.7-slim
ENV FLASK_APP=.
WORKDIR /app
COPY requirements.txt /app/requirements.txt
COPY . /app
RUN pip install -r requirements.txt
CMD flask run -h 0.0.0.0 -p 5000
The catch is that you have to change how you run flask, not via python command.
I'm trying to use Heroku to push my Flask app to the web. I think I have everything down, except the Procfile. Here is what my file directory looks like:
My main.py is:
from application import app
My init.py is:
from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine()
db.init_app(app)
from application import routes
What is the correct Procfile command here?
Thanks
Since I moved to Google App Engine I cannot run the Flask-Migrate command python manage.py db migrate because I have exceptions regarding some GAE related imports (No module named google.appengine.ext for example).
Is there a way to run this, or an alternative, to upgrade my database on GAE?
Yes, there is a way to run it, though it's not as straightforward as you'd might like.
You need to configure your Google Cloud SQL, add yourself as an authorized user (by entering your ip address) and request to have an IPv4 address. Deal with SSL as appropriate.
Using a script:
Replacing user, password, instance_id, db_name, and path below
# migrate_prod.py
DB_MIGRATION_URI = "mysql+mysqldb://user:password#instance_id/db_name?ssl_key=path/client-key.pem&ssl_cert=path/client-cert.pem&&ssl_ca=path/server-ca.pem"
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from models import * # not needed if migration file is already generated
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = DB_MIGRATION_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Run the script as you would to migrate locally: python migrate_prod.py db upgrade, assuming your migration file is already there.
Release the IPv4, so that you're not charged for it.
I give much credit to other answers: how to connect via SSL and run alembic migrations on GAE (of which this is probably a duplicate).