From 07150fc6ae4f40c0b9f755e91b3ef520cc022229 Mon Sep 17 00:00:00 2001 From: Ian Arawjo Date: Thu, 20 Jul 2023 11:17:58 -0400 Subject: [PATCH] Let users choose hostname and port --- chainforge/app.py | 12 ++++++++---- chainforge/flask_app.py | 16 ++++++++++++++-- chainforge/react-server/src/backend/utils.ts | 8 +++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/chainforge/app.py b/chainforge/app.py index 443f3ec..e46e875 100644 --- a/chainforge/app.py +++ b/chainforge/app.py @@ -19,7 +19,10 @@ def main(): # action='store_true') # TODO: Reimplement this where the React server is given the backend's port before loading. - # serve_parser.add_argument('--port', help='The port to run the server on. Defaults to 8000.', type=int, default=8000, nargs='?') + serve_parser.add_argument('--port', help='The port to run the server on. Defaults to 8000.', type=int, default=8000, nargs='?') + serve_parser.add_argument('--host', + help="The host to run the server on. Defaults to 'localhost'.", + type=str, default="localhost", nargs='?') args = parser.parse_args() @@ -28,10 +31,11 @@ def main(): parser.print_help() exit(0) - port = 8000 # args.port if args.port else 8000 + port = args.port if args.port else 8000 + host = args.host if args.host else "localhost" - print(f"Serving Flask server on port {port}...") - run_server(host="localhost", port=port, cmd_args=args) + print(f"Serving Flask server on {host} on port {port}...") + run_server(host=host, port=port, cmd_args=args) if __name__ == "__main__": main() \ No newline at end of file diff --git a/chainforge/flask_app.py b/chainforge/flask_app.py index a00c2c1..fdb5732 100644 --- a/chainforge/flask_app.py +++ b/chainforge/flask_app.py @@ -14,6 +14,8 @@ import requests as py_requests """ # Setup Flask app to serve static version of React front-end +HOSTNAME = "localhost" +PORT = 8000 BUILD_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'react-server', 'build') STATIC_DIR = os.path.join(BUILD_DIR, 'static') app = Flask(__name__, static_folder=STATIC_DIR, template_folder=BUILD_DIR) @@ -228,7 +230,14 @@ def run_over_responses(eval_func, responses: list, scope: str) -> list: # Serve React app (static; no hot reloading) @app.route("/") def index(): - return render_template("index.html") + # Get the index.html HTML code + html_str = render_template("index.html") + + # Inject global JS variables __CF_HOSTNAME and __CF_PORT at the top so that the application knows + # that it's running from a Flask server, and what the hostname and port of that server is: + html_str = html_str[:60] + f'' + html_str[60:] + + return html_str @app.route('/app/executepy', methods=['POST']) def executepy(): @@ -489,7 +498,10 @@ async def callDalai(): return ret -def run_server(host="", port=8000, cmd_args=None): +def run_server(host="", port=8000, cmd_args=None): + global HOSTNAME, PORT + HOSTNAME = host + PORT = port app.run(host=host, port=port) if __name__ == '__main__': diff --git a/chainforge/react-server/src/backend/utils.ts b/chainforge/react-server/src/backend/utils.ts index 9c0fedc..c68e570 100644 --- a/chainforge/react-server/src/backend/utils.ts +++ b/chainforge/react-server/src/backend/utils.ts @@ -17,8 +17,9 @@ const AI_PROMPT = "\n\nAssistant:"; const fetch = require('node-fetch'); -/** Where the ChainForge Flask server is being hosted. */ -export const FLASK_BASE_URL = 'http://localhost:8000/'; +/** Where the ChainForge Flask server is being hosted, if any. */ +// @ts-ignore +export const FLASK_BASE_URL = (window.__CF_HOSTNAME !== undefined && window.__CF_PORT !== undefined) ? `http://${window.__CF_HOSTNAME}:${window.__CF_PORT}/` : 'http://localhost:8000/'; export async function call_flask_backend(route: string, params: Dict | string): Promise { return fetch(`${FLASK_BASE_URL}app/${route}`, { @@ -42,7 +43,8 @@ export function APP_IS_RUNNING_LOCALLY(): boolean { // Calculate whether we're running the app locally or not, and save the result try { const location = window.location; - _APP_IS_RUNNING_LOCALLY = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "0.0.0.0" || location.hostname === ""; + // @ts-ignore + _APP_IS_RUNNING_LOCALLY = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "0.0.0.0" || location.hostname === "" || window.__CF_HOSTNAME !== undefined; } catch (e) { // ReferenceError --window or location does not exist. // We must not be running client-side in a browser, in this case (e.g., we are running a Node.js server)