Skip to content
Advertisement

ERR_TOO_MANY_REDIRECTS in a Flask application. Works in local but not in server

In local my Flask application works fine, and when I use /editing_buddy it redirects me correctly. The thing is, when I upload it to the server, it always gives me 404 Error.

If I try to use @app.route decorator, I get a TOO MANY REDIRECTS error. My server is hosted by Wikimedia in funpedia.toolforge.org if that helps. It’s a webservice with kubernetes backend.

My code is like this:

app = flask.Flask(__name__)

if __name__ == '__main__':
    app.run_server(host='0.0.0.0', threaded=True, debug=True)


# Load configuration from YAML file
__dir__ = os.path.dirname(__file__)
app.config.update(
    yaml.safe_load(open(os.path.join(__dir__, 'config.yaml'))))

@app.route('/')
def index():

    return flask.redirect(flask.url_for('index'))

@app.route('/editing_buddy')
def buddy():

    return flask.redirect(flask.url_for('buddy'))

@app.errorhandler(404)
def handling_page_not_found(e):
    return "<h1>404</h1><p>The resource could not be found.</p>", 404

The apps for the “/” and “/editing_buddy” are Dash applications with the same url_base_pathname:

Buddy

buddy_app = Dash(__name__, server=application, url_base_pathname="/editing_buddy/",
                             external_stylesheets=external_stylesheets)
buddy_app.config['suppress_callback_exceptions'] = True

Home

 home_app = Dash(__name__, server=application, url_base_pathname="/", 
                           external_stylesheets=external_stylesheets)
 home_app.config['suppress_callback_exceptions'] = True



from view.home import *
from view.editing_buddy_app import *

Locally, I start the server by running wsgi.py. Which contains the code:

from app import app as application

if __name__ == "__main__":
    application.run()

In the server I simply run webservice restart, but I have a .ini file called app.ini with content:

[uwsgi]
module = wsgi
callable = application

And my folder structure is Folder Structure

PS: I’ve also tried hardcoding the redirects like flask.redirect("/editing_buddy/"). Even i’ve tried to hardcode the URL like flask.redirect("funpedia.toolforge.org/editing_buddy/") and either doesn’t find it or Too many redirects.

You can see my repo HERE

Advertisement

Answer

Now it works, why? I don’t have any clue. I did the next:

  1. I eliminated the app.ini from the directory of the app.py and put it on the root directory.
  2. In home.py, I renamed from app import app as application to just from app import app.
  3. Eliminated the unnecessary @app.route for the dash apps, they are not needed because you already have the url_base_pathname.

After doing some testing, I found that the solution is the step 2. By using from app import app in the home.py script.

HOWEVER, in the editing_buddy_app.py, I have it with from app import app as application AND IT WORKS

Definitely, I’m missing something here, but I’m unable to guess what.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement