skip to Main Content

Encountering difficulties in hosting my Flask application on Ubuntu on AWS due to a persistent error that I’m unable to resolve. The error, identified in the Gunicorn logs, specifically points to a TypeError where the ‘Flask’ object is not iterable. The issue seems to be associated with the handling of the request for the favicon.ico file.

[2023-12-29 22:32:39 +0000] [5686] [ERROR] Error handling request /favicon.ico
Traceback (most recent call last):
  File "/home/ubuntu/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 135, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/ubuntu/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 183, in handle_request
    for item in respiter:
TypeError: 'Flask' object is not iterable

main.py

from myAPP import create_app
from flask_mail import Mail

app = create_app()

@app.template_filter('custom_b64encode')
def custom_b64encode(data):
    return base64.b64encode(data).decode('utf-8')

from flask import render_template

@app.errorhandler(404)
def not_found_error(error):
    return render_template('404.html'), 404


from myAPP.views import views
app.register_blueprint(views, name='my_views')


app.config['UPLOAD_FOLDER'] = '/photos/'

if __name__ == '__main__':
    app.run()

__init__.py

db = SQLAlchemy()
DB_NAME = "database.db"
mail = Mail()  # Initialize mail

def create_app(environ=None, start_response=None):
    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
    mail = Mail(app)
    app.config['SECRET_KEY'] = 'SecretKey'
    app.config['SECURITY_PASSWORD_SALT'] = 'SecurityPasswordSalt'
    
    app.config['MAIL_SERVER'] = 'example.home.pl'
 
    app.config['MAIL_PORT'] = 587
  
    app.config['MAIL_USE_TLS'] = True  # Enable TLS
    
    app.config['MAIL_USE_SSL'] = False  # Keep SSL disabled
    app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME', 'username')

    app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD', 'password')
 
    app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
 
    db.init_app(app)

    mail.init_app(app)  

    with app.app_context():
        db.create_all()

    from .views import views
    from .auth import auth

    app.register_blueprint(views, url_prefix='/')
    app.register_blueprint(auth, url_prefix='/')

    from .models import User, Photo

    login_manager = LoginManager()
    login_manager.login_view = 'auth.loginPage'
    login_manager.init_app(app)

    @login_manager.user_loader
    def load_user(id):
        return User.query.get(int(id))
    
    from . import models

    with app.app_context():
        db.create_all()
    return app

app = create_app()
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

s = URLSafeTimedSerializer(app.config['SECRET_KEY'])

As a beginner, I tried engaging with chatGPT using the entire database, but faced issues and couldn’t achieve the desired results.

2

Answers


  1. Chosen as BEST ANSWER

    Changing calling the gunicorn from

    gunicorn main:create_app
    

    to

    gunicorn 'main:create_app()' 
    

    worked for me


  2. Usually this error is caused by passing something other than the Flask app object to uwsgi (Gunicorn). What is the callable set to for uwsgi? If you are using a function to generate the app make sure you calling the function and not just passing the function object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search