This article describes how to run a Flask App through your VSCode Workspace for debugging purposes. This is based on the Flask App example in the Domino Documentation, but should work with any Flask app that follows the same App Structure.
First, to __init__.py: we gather some environment variables and add some code to __call__ to help this run in a Workspace:
from flask import Flask
# Needed to build a URL for Workspace Debugging import os workspace_debug_port = os.environ.get('FLASK_WORKSPACE_DEBUG_PORT', '') PROJECT_OWNER = os.environ["DOMINO_PROJECT_OWNER"] PROJECT_NAME = os.environ["DOMINO_PROJECT_NAME"] RUN_ID = os.environ["DOMINO_RUN_ID"] SESSION_URL = f'{PROJECT_OWNER}/{PROJECT_NAME}/r/notebookSession/{RUN_ID}' class ReverseProxied(object): def __init__(self, app): self.app = app def __call__(self, environ, start_response): script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
# Need for Workspace debugging as HTTP_X_SCRIPT_NAME doesn't exist if workspace_debug_port: script_name = '/{}/proxy/{}'.format(SESSION_URL, 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):] # Setting wsgi.url_scheme from Headers set by proxy before app scheme = environ.get('HTTP_X_SCHEME', 'https') if scheme: environ['wsgi.url_scheme'] = scheme # Setting HTTP_HOST from Headers set by proxy before app remote_host = environ.get('HTTP_X_FORWARDED_HOST', '') remote_port = environ.get('HTTP_X_FORWARDED_PORT', '') if remote_host and remote_port: environ['HTTP_HOST'] = f'{remote_host}:{remote_port}' return self.app(environ, start_response) app = Flask(__name__) app.wsgi_app = ReverseProxied(app.wsgi_app)
Second, an .sh file to run the Flask app from your Workspace. Rather than modifying the App's app.sh file, we can create a new file since we'll be calling it manually. Since an App URL in a Workspace can be a bit tricky to find manually, we've included a handy line to echo the URL where you can expect to find this App.
flask-workspace.sh:
#!/usr/bin/env bash export FLASK_APP=run.py export FLASK_DEBUG=1 export FLASK_WORKSPACE_DEBUG_PORT=8887 export BASE_URL="https://sup540ds5748.support-team-sandbox.domino.tech/" export APP_URL=$BASE_URL$DOMINO_PROJECT_OWNER"/"$DOMINO_PROJECT_NAME"/r/notebookSession/"$DOMINO_RUN_ID"/proxy/"$FLASK_WORKSPACE_DEBUG_PORT"/" echo "Your app will be running at "$APP_URL python -m flask run --host=0.0.0.0 --port=$FLASK_WORKSPACE_DEBUG_PORT
Once you've modified your Flask app's __init__.py and created an .sh file, you're ready to run this App from your Workspace!
From VSCode, open a new Terminal (Ctrl + Shift + `). In that Terminal, run bash
flask-workspace.sh
.
If everything starts correctly, you should see output similar to the image below, including a handy link to the App running in your Workspace!
Comments
0 comments
Article is closed for comments.