5

I'm trying to run my flask app with "python app.py" instead of "flask run" command.

My goal is to launch the app on cpanel server and just about every tutorial requires the applications to be called using the "python" method.

Here is my folder structure:

  • project
    • webapp
      • init.py
      • templates
      • static
      • auth.py
      • main.py
    • app.py <-------------- I want this to be called with python instead of flask run command outside the folder

Here is my init_.py file:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager 

# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()

def create_app():
    
    app = Flask(__name__)
    
    app.config['SECRET_KEY'] = '9OLWxND4o83j4iuopO'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

    db.init_app(app)

    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))

    # 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)

    return app

And app.py is:

from webapp import app

I'm a newbie in flask, any help is appreciated

5
  • 1
    try this app.run(host, port, debug, options) Commented Aug 2, 2020 at 18:37
  • 1
    app.run is missing. You could insert it at init.py and try: > python webapp/init.py Commented Aug 2, 2020 at 18:43
  • Thank you for your suggestions, I've tried both of the solutions but the problem somewhat persist. If I try to put app.run() in init.py works but all links shows 404 Not Found. If I try flask run, app runs without a problem. I'm really consufed, please help @Carlos Bazilio Commented Aug 2, 2020 at 19:02
  • 1
    Insert a call to create_app().run() at the end of init.py. You could call this way: create_app().run(host='0.0.0.0', port=5000, debug=True) Commented Aug 2, 2020 at 19:57
  • Thank you so much @CarlosBazilio It works perfectly now. I wrote create_app().run(debug=True) without a host and port and works like a charm. Thanks Commented Aug 2, 2020 at 21:01

1 Answer 1

9

Insert the call to create_app at the end of init.py:

if __name__ == '__main__':
    create_app().run(host='0.0.0.0', port=5000, debug=True)

The if statement avoid calling the app many times. It can only be called directly. Flask default host is 127.0.0.1 (localhost). Use 0.0.0.0 at production for better traffic monitoring. Default port is also 5000, so it's up to you to include. For better readability, you should explicit it.

Then call it

python webapp/init.py
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.