Forums

Is Flask_Babel supported by PythonAnywhere?

Is Flask_Babel supported by PythonAnywhere?

I get an error while loading the website when it passes the locale_selector

This is a part of my code:

from flask import Flask, render_template,request
from flask_babel import Babel, gettext



app = Flask(__name__)

babel = Babel(app)


def get_locale():

    return request.accept_languages.best_match(['en','nl','fr','de'])

babel.init_app(app, locale_selector=get_locale)

The error comes at the last line here above on the "locale_selector" variable.

Error message in log:

Error running WSGI application
2024-08-18 18:48:20,899: TypeError: Babel.init_app() got an unexpected keyword argument 'locale_selector'
2024-08-18 18:48:20,899:   File "/var/www/kevinkrueger_pythonanywhere_com_wsgi.py", line 16, in <module>
2024-08-18 18:48:20,899:     from KevinKruger import app as application  # noqa
2024-08-18 18:48:20,900: 
2024-08-18 18:48:20,900:   File "/home/kevinkrueger/mysite/KevinKruger.py", line 15, in <module>
2024-08-18 18:48:20,900:     babel.init_app(app, locale_selector=get_locale)

It does work perfect on my local laptop. Thanks Wim

[edit by admin: formatting]

That sounds like it might be a version mismatch between what you have locally and what you're using on PythonAnywhere. Are you using our default pre-installed version? If so, we're using 2.0.0 -- you can upgrade it by running this in a Bash console

pipX.Y install --upgrade Flask-Babel==SOMEVERSION

...replacing the X.Y with the version of Python that you've selected on the "Web" page inside PythonAnywhere for your website, and the SOMEVERSION with the version of Flask-Babel that you're using locally -- for example, 4.0.0 if you're using the most recent version.

After running the command:

pip3.10 install --upgrade --user Flask-Babel

Babel worked correctly.

Nice!