简体   繁体   中英

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文件夹结构

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

You are literally redirecting in your methods to the exact same method you're doing the redirect from. By doing so you are creating an endless loop, and TBH this should even throw errors locally.

I advise you to give this medium.com article a good read to make sure you set up both Dash and Flask correctly, using the Application Factory Pattern.

===================================================

EDIT:

This is a copy/paste from the article I linked, you can see that when the Flask app gets instantiated, it also creates the Dash apps, using the Flask app as the server...

dashboard.py

def create_app():
    server = Flask(__name__)
    server.config.from_object(BaseConfig)

    register_dashapps(server)
# snipped some here

    return server


def register_dashapps(app):
    from app.dashapp1.layout import layout
    from app.dashapp1.callbacks import register_callbacks

    # Meta tags for viewport responsiveness
    meta_viewport = {"name": "viewport", "content": "width=device-width, initial-scale=1, shrink-to-fit=no"}

    dashapp1 = dash.Dash(__name__,
                         server=app,
                         url_base_pathname='/dashboard/',
                         assets_folder=get_root_path(__name__) + '/dashboard/assets/',
                         meta_tags=[meta_viewport])

    with app.app_context():
        dashapp1.title = 'Dashapp 1'
        dashapp1.layout = layout
        register_callbacks(dashapp1)

    _protect_dashviews(dashapp1)

===================================================

EDIT2:

Thanks for adding the repo, this helped in figuring out what is going on, and I think I have a grasp of what you want to do based on the rest of the code. I'm guessing you want to put authentication logic in your app so not everyone can just access your handlers for the respective Dash apps.

Thing is, you are running into one of the exact same issues described in the first article I linked, which is the authentication.

You will need to refactor your code if you want to use authentication, similar to what is discussed in the aforementioned article.

While researching this question I did come across an article which might interest you, as it discusses how to implement authentication. It's also a simpler article, and it basically allows you to apply Flask stuff to Dash apps. Find it here ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM