Skip to content
Snippets Groups Projects
start_server.py 1.26 KiB
Newer Older
Xin-Hao Zhu's avatar
Xin-Hao Zhu committed
from flask import Flask, render_template, send_from_directory
from multiprocessing import Process
from collect_html import collect_html
from generate_html import generate_html
from gevent import pywsgi

from utils import IP2NAME, CACHE_DIR, SERVER_PORT
import os

if not os.path.exists(CACHE_DIR):
    os.makedirs(CACHE_DIR)

app = Flask(__name__, static_url_path="")

@app.route("/")
@app.route("/gpus.html")
def root():
    sections = []

    for machine_ip in IP2NAME:
        machine_name = IP2NAME[machine_ip] + '(' + machine_ip + ')'
        target_file = os.path.join(CACHE_DIR, f"{machine_ip}.html")


        if os.path.exists(target_file):
            with open(target_file, "r", encoding="utf-8") as f:
                html = f.read()
        else:
            html = ""
        
        if html:
            sections.append(html)
        else:
            sections.append(render_template("section-error.html", machine_name=machine_name))
    
    return render_template("index.html", sections=sections)

@app.route('/static/<path:path>')
def sendui(path):
    return send_from_directory('static', path)

if __name__ == "__main__":
    p = Process(target=collect_html)
    p.start()
    
    server = pywsgi.WSGIServer(('0.0.0.0', SERVER_PORT), app)
    server.serve_forever()