Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add APPLICATION_ROOT support #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ __pycache__/
*.log

dist/
build/
build/

gpt_code_ui/webapp/static
gpt_code_ui/webapp/templates
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ compile_frontend:
npm install && \
VITE_APP_VERSION=$$(grep -e "^\s*version='[^']*'" ../setup.py | cut -d "'" -f 2) npm run build && \
find ../gpt_code_ui/webapp/static -mindepth 1 ! -name '.gitignore' -delete && \
rsync -av dist/ ../gpt_code_ui/webapp/static
rsync -av dist/ ../gpt_code_ui/webapp/static && \
cd .. && \
python3 scripts/create_template.py

bundle_pypi:
rm -rf dist build && \
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ gptcode
Please do and have a look at the [contributions guide](.github/CONTRIBUTING.md)! This should be a community initiative. I'll try my best to be responsive.


Thank you for your interest in this project!
Thank you for your interest in this project!

## run in subroutes

set ```APPLICATION_ROOT``` in ```.env``` file, and in proxy set host ```localhost:${WEB_PORT}${your APPLICATION_ROOT}```, just like any other flask app
3 changes: 3 additions & 0 deletions frontend/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# taobao npm registry mirror
registry = https://registry.npmmirror.com
legacy-peer-deps = true
4 changes: 2 additions & 2 deletions frontend/src/config.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let resolvedWebAddress = import.meta.env.VITE_WEB_ADDRESS ? import.meta.env.VITE_WEB_ADDRESS : "";
let resolvedWebAddress = import.meta.env.VITE_WEB_ADDRESS ? import.meta.env.VITE_WEB_ADDRESS : location.pathname;

const Config = {
WEB_ADDRESS: resolvedWebAddress,
API_ADDRESS: resolvedWebAddress + "/api"
API_ADDRESS: (resolvedWebAddress + "/api").replace(/\/\//g, "/"),
}

export default Config;
3 changes: 0 additions & 3 deletions gpt_code_ui/main.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you disable webbrowser.open on purpose (for this PR)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, with this new env var set, webbrowser.open wont get the real address, any suggestion?

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import logging
import asyncio
import time
import webbrowser

from multiprocessing import Process

Expand Down Expand Up @@ -84,8 +83,6 @@ def main():
time.sleep(0.1)

print_banner()

webbrowser.open(APP_URL)

webapp_process.join()
kernel_program_process.join()
Expand Down
12 changes: 5 additions & 7 deletions gpt_code_ui/webapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections import deque

from flask_cors import CORS
from flask import Flask, request, jsonify, send_from_directory, Response
from flask import Flask, request, jsonify, send_from_directory, render_template, Response
from dotenv import load_dotenv

from gpt_code_ui.kernel_program.main import APP_PORT as KERNEL_APP_PORT
Expand All @@ -22,6 +22,7 @@
openai.api_version = os.environ.get("OPENAI_API_VERSION")
openai.log = os.getenv("OPENAI_API_LOGLEVEL")
OPENAI_EXTRA_HEADERS = json.loads(os.environ.get("OPENAI_EXTRA_HEADERS", "{}"))
APPLICATION_ROOT = os.environ.get("APPLICATION_ROOT", "/")

if openai.api_type == "open_ai":
AVAILABLE_MODELS = json.loads(os.environ.get("OPENAI_MODELS", '''[{"displayName": "GPT-3.5", "name": "gpt-3.5-turbo"}, {"displayName": "GPT-4", "name": "gpt-4"}]'''))
Expand Down Expand Up @@ -182,18 +183,15 @@ def extract_code(text):

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

app.config['APPLICATION_ROOT'] = APPLICATION_ROOT
app.jinja_env.globals['APPLICATION_ROOT'] = APPLICATION_ROOT
CORS(app)


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

# Check if index.html exists in the static folder
if not os.path.exists(os.path.join(app.root_path, 'static/index.html')):
print("index.html not found in static folder. Exiting. Did you forget to run `make compile_frontend` before installing the local package?")

return send_from_directory('static', 'index.html')
return render_template('index.html')


@app.route("/models")
Expand Down
20 changes: 20 additions & 0 deletions scripts/create_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import re

# current directory absolute path
current_dir = os.path.dirname(os.path.abspath(__file__))

static_html_file_path = os.path.join(current_dir, '../gpt_code_ui/webapp/static/index.html')
template_file_path = os.path.join(current_dir, '../gpt_code_ui/webapp/templates/index.html')
r_reg_src = re.compile(r'(src|href)="([^"]+)"', flags=re.IGNORECASE|re.MULTILINE|re.DOTALL)

# Read the static html file
with open(static_html_file_path, 'r') as file:
static_html = file.read()
# change source of the script to flask func call as it is in the template with regex
static_html = re.sub(r_reg_src, '\\1="{{APPLICATION_ROOT}}{{ url_for(\'static\', filename=\'\\2\') }}"', static_html)
# Write the static html to the template file
# ensure the template directory exists
os.makedirs(os.path.dirname(template_file_path), exist_ok=True)
with open(template_file_path, 'w') as template_file:
template_file.write(static_html)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
long_description=long_description,
long_description_content_type='text/markdown', # This field specifies the format of the `long_description`.
packages=find_packages(),
package_data={'gpt_code_ui.webapp': ['static/*', 'static/assets/*']},
package_data={'gpt_code_ui.webapp': ['static/*', 'static/assets/*', 'templates/*']},
install_requires=[
'ipykernel>=6,<7',
'snakemq>=1,<2',
Expand Down