Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/GUI.py
326 views
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
16
17
# Set the hostname
18
HOST = "localhost"
19
# Set the port number
20
PORT = 4000
21
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("settings.html", file="config.toml", data=config, checks=checks)
86
87
88
# Make videos.json accessible
89
@app.route("/videos.json")
90
def videos_json():
91
return send_from_directory("video_creation/data", "videos.json")
92
93
94
# Make backgrounds.json accessible
95
@app.route("/backgrounds.json")
96
def backgrounds_json():
97
return send_from_directory("utils", "backgrounds.json")
98
99
100
# Make videos in results folder accessible
101
@app.route("/results/<path:name>")
102
def results(name):
103
return send_from_directory("results", name, as_attachment=True)
104
105
106
# Make voices samples in voices folder accessible
107
@app.route("/voices/<path:name>")
108
def voices(name):
109
return send_from_directory("GUI/voices", name, as_attachment=True)
110
111
112
# Run browser and start the app
113
if __name__ == "__main__":
114
webbrowser.open(f"http://{HOST}:{PORT}", new=2)
115
print("Website opened in new tab. Refresh if it didn't load.")
116
app.run(port=PORT)
117
118