workonexcel / app_frontend.py
rislrohitjain's picture
Initial clean push
a490b79
Raw
History Blame Contribute Delete
2.15 kB
import os
import sys
import requests
from flask import Flask, render_template, request, jsonify, send_from_directory, send_file
from dotenv import load_dotenv
load_dotenv()
def get_base_path():
if hasattr(sys, '_MEIPASS'): return sys._MEIPASS
return os.path.abspath(".")
base_dir = get_base_path()
app = Flask(__name__,
static_folder=os.path.join(base_dir, 'static'),
template_folder=os.path.join(base_dir, 'templates'))
# DYNAMIC CONFIG
BACKEND_URL = os.getenv("BACKEND_URL", "http://172.18.177.164:8000")
@app.route('/')
def index():
return render_template('index.html', backend_url=BACKEND_URL)
@app.route('/download-template')
def download_template():
static_path = os.path.join(base_dir, 'static')
filename = 'sample_template.xlsx'
if not os.path.exists(static_path): os.makedirs(static_path)
return send_from_directory(static_path, filename, as_attachment=True)
@app.route('/download-installer')
def download_installer():
output_path = os.path.join(base_dir, 'Output')
filename = 'WorkOnExcelRohitJainPro_Setup.exe'
if not os.path.exists(output_path): os.makedirs(output_path)
return send_from_directory(output_path, filename, as_attachment=True)
@app.route('/proxy-upload', methods=['POST'])
def proxy_upload():
if 'file' not in request.files: return jsonify({"error": "No file"}), 400
file = request.files['file']
files = {'file': (file.filename, file.stream, file.content_type)}
try:
r = requests.post(f"{BACKEND_URL}/upload", files=files, timeout=60)
return jsonify(r.json())
except Exception as e:
return jsonify({"error": f"Connection failed: {str(e)}"}), 503
@app.route('/download-file')
def download_file():
file_path = request.args.get('path')
if not file_path: return "No path", 400
abs_path = os.path.abspath(file_path)
if os.path.exists(abs_path):
return send_file(abs_path, as_attachment=True, download_name=os.path.basename(abs_path))
return "File not found", 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.getenv("FRONTEND_PORT", 5000)), debug=True)