FAQ: Links are broken in Flask app when debugging inside a workspace

Follow

Comments

1 comment

  • Jaclyn Patterson

    The problem boils down to a header (X-Script-Name ) that is supplied when Domino publishes the app normally, but not when opening the app in a workspace for debugging like above. The solution is to add some lines to the boilerplate "reverse proxy" related code at the beginning of the app-flask.py file.

    First, have the file read in an environment variable specifying your debugging port:

    import os
    workspace_debug_port = os.environ.get('FLASK_WORKSPACE_DEBUG_PORT', '')
    

    Then add the following to the reverse proxy code where it tries to read in the script_name :

        if workspace_debug_port:
          # This is necessary for debugging in a workspace, where HTTP_X_SCRIPT_NAME doesn't exist
          script_name = '/proxy/{}'.format(workspace_debug_port)
    

    Now the whole block will look like this:

    class ReverseProxied(object):
      def __init__(self, app):
        self.app = app
      def __call__(self, environ, start_response):
        script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
        if workspace_debug_port:
          # This is necessary for debugging in a workspace, where HTTP_X_SCRIPT_NAME doesn't exist
          script_name = '/proxy/{}'.format(workspace_debug_port)
        if script_name:
          environ['SCRIPT_NAME'] = script_name
          path_info = environ['PATH_INFO']
          if path_info.startswith(script_name):
            environ['PATH_INFO'] = path_info[len(script_name):]
        return self.app(environ, start_response)  
    

    Then before running the app, add the following export command to the environment variables you export (with whatever port you choose). You can even modify the app run command to use the same environment variable, so you only have to set the port in once place:

    $ export FLASK_WORKSPACE_DEBUG_PORT=8887
    $ python -m flask run --host=0.0.0.0 --port=$FLASK_WORKSPACE_DEBUG_PORT
    

    Now the links in the app will change to correctly contain the /proxy/8887 prefix, and e.g. navigating to another_page will take you to https://5cef31de46e0fb00083f9708-workspace.app.dominodatalab.com/proxy/8887/another_page and load as expected.

    Submitted by: melanie.veale

    0
    Comment actions Permalink

Please sign in to leave a comment.