|
1 |
| -# Import the server module |
2 |
| -import http.server |
3 | 1 | import webbrowser
|
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +# Used "tomlkit" instead of "toml" because it doesn't change formatting on "dump" |
| 5 | +import tomlkit |
| 6 | +from flask import ( |
| 7 | + Flask, |
| 8 | + redirect, |
| 9 | + render_template, |
| 10 | + request, |
| 11 | + send_from_directory, |
| 12 | + url_for, |
| 13 | +) |
| 14 | + |
| 15 | +import utils.gui_utils as gui |
4 | 16 |
|
5 | 17 | # Set the hostname
|
6 | 18 | HOST = "localhost"
|
7 | 19 | # Set the port number
|
8 | 20 | PORT = 4000
|
9 | 21 |
|
10 |
| -# Define class to display the index page of the web server |
11 |
| -class PythonServer(http.server.SimpleHTTPRequestHandler): |
12 |
| - def do_GET(self): |
13 |
| - if self.path == "/GUI": |
14 |
| - self.path = "index.html" |
15 |
| - return http.server.SimpleHTTPRequestHandler.do_GET(self) |
16 |
| - |
17 |
| - |
18 |
| -# Declare object of the class |
19 |
| -webServer = http.server.HTTPServer((HOST, PORT), PythonServer) |
20 |
| -# Print the URL of the webserver, new =2 opens in a new tab |
21 |
| -print(f"Server started at http://{HOST}:{PORT}/GUI/") |
22 |
| -webbrowser.open(f"http://{HOST}:{PORT}/GUI/", new=2) |
23 |
| -print("Website opened in new tab") |
24 |
| -print("Press Ctrl+C to quit") |
25 |
| -try: |
26 |
| - # Run the web server |
27 |
| - webServer.serve_forever() |
28 |
| -except KeyboardInterrupt: |
29 |
| - # Stop the web server |
30 |
| - webServer.server_close() |
31 |
| - print("The server is stopped.") |
32 |
| - exit() |
| 22 | +# Configure application |
| 23 | +app = Flask(__name__, template_folder="GUI") |
| 24 | + |
| 25 | +# Configure secret key only to use 'flash' |
| 26 | +app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' |
| 27 | + |
| 28 | + |
| 29 | +# Ensure responses aren't cached |
| 30 | +@app.after_request |
| 31 | +def after_request(response): |
| 32 | + response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" |
| 33 | + response.headers["Expires"] = 0 |
| 34 | + response.headers["Pragma"] = "no-cache" |
| 35 | + return response |
| 36 | + |
| 37 | + |
| 38 | +# Display index.html |
| 39 | +@app.route("/") |
| 40 | +def index(): |
| 41 | + return render_template("index.html", file="videos.json") |
| 42 | + |
| 43 | + |
| 44 | +@app.route("/backgrounds", methods=["GET"]) |
| 45 | +def backgrounds(): |
| 46 | + return render_template("backgrounds.html", file="backgrounds.json") |
| 47 | + |
| 48 | + |
| 49 | +@app.route("/background/add", methods=["POST"]) |
| 50 | +def background_add(): |
| 51 | + # Get form values |
| 52 | + youtube_uri = request.form.get("youtube_uri").strip() |
| 53 | + filename = request.form.get("filename").strip() |
| 54 | + citation = request.form.get("citation").strip() |
| 55 | + position = request.form.get("position").strip() |
| 56 | + |
| 57 | + gui.add_background(youtube_uri, filename, citation, position) |
| 58 | + |
| 59 | + return redirect(url_for("backgrounds")) |
| 60 | + |
| 61 | + |
| 62 | +@app.route("/background/delete", methods=["POST"]) |
| 63 | +def background_delete(): |
| 64 | + key = request.form.get("background-key") |
| 65 | + gui.delete_background(key) |
| 66 | + |
| 67 | + return redirect(url_for("backgrounds")) |
| 68 | + |
| 69 | + |
| 70 | +@app.route("/settings", methods=["GET", "POST"]) |
| 71 | +def settings(): |
| 72 | + config_load = tomlkit.loads(Path("config.toml").read_text()) |
| 73 | + config = gui.get_config(config_load) |
| 74 | + |
| 75 | + # Get checks for all values |
| 76 | + checks = gui.get_checks() |
| 77 | + |
| 78 | + if request.method == "POST": |
| 79 | + # Get data from form as dict |
| 80 | + data = request.form.to_dict() |
| 81 | + |
| 82 | + # Change settings |
| 83 | + config = gui.modify_settings(data, config_load, checks) |
| 84 | + |
| 85 | + return render_template( |
| 86 | + "settings.html", file="config.toml", data=config, checks=checks |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +# Make videos.json accessible |
| 91 | +@app.route("/videos.json") |
| 92 | +def videos_json(): |
| 93 | + return send_from_directory("video_creation/data", "videos.json") |
| 94 | + |
| 95 | + |
| 96 | +# Make backgrounds.json accessible |
| 97 | +@app.route("/backgrounds.json") |
| 98 | +def backgrounds_json(): |
| 99 | + return send_from_directory("utils", "backgrounds.json") |
| 100 | + |
| 101 | + |
| 102 | +# Make videos in results folder accessible |
| 103 | +@app.route("/results/<path:name>") |
| 104 | +def results(name): |
| 105 | + return send_from_directory("results", name, as_attachment=True) |
| 106 | + |
| 107 | + |
| 108 | +# Make voices samples in voices folder accessible |
| 109 | +@app.route("/voices/<path:name>") |
| 110 | +def voices(name): |
| 111 | + return send_from_directory("GUI/voices", name, as_attachment=True) |
| 112 | + |
| 113 | + |
| 114 | +# Run browser and start the app |
| 115 | +if __name__ == "__main__": |
| 116 | + webbrowser.open(f"http://{HOST}:{PORT}", new=2) |
| 117 | + print("Website opened in new tab. Refresh if it didn't load.") |
| 118 | + app.run(port=PORT) |
0 commit comments