-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_gui.py
168 lines (147 loc) · 6.21 KB
/
flask_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import Flask, request, make_response
from SongEditorPro7Generic import get_text_block_names, gen_pro_data
import json
app = Flask(__name__)
class MemoryFile(object):
def __init__(self):
self.data = b""
def write(self, stuff):
self.data += stuff.encode()
def strip_song_name(song_name):
s = r"ÇüéâäàåçêëèïîìÄÅÉôöòûùÿÖÜøØ׃áíóúñÑÁÂÀ¥ãÃÐÊËÈıÍÎÏÌÓßÔÒõÕµÚÛÙýݧ¹³²"
r = r"CueaaaaceeeiiiAAEooouuyOU00xfaiounNAAAYaADEEE1IIIIOSOOoOuUUUyYS132"
converted = ""
for character in song_name:
for _s, _r in zip(s, r):
if character == _s:
character = _r
if character not in ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz-_ "):
character = " "
converted += character
# remove multiple spaces
while " " in converted:
converted = converted.replace(" ", " ")
song_name = converted.strip()
return song_name
def json_download(request):
form_data = request.form
text_block_names = get_text_block_names()
song_text = {}
for text_block_name in text_block_names:
a_song_text = form_data[text_block_name].replace("\r", "")
song_text[text_block_name] = (a_song_text+"\n").split("\n")
mem_file = MemoryFile()
json.dump(song_text, mem_file)
response = make_response(mem_file.data)
response.headers.set('Content-Type', 'binary/json')
song_file_name = strip_song_name(form_data["SongName"])
response.headers.set('Content-Disposition', 'attachment', filename=f'{song_file_name}.json')
return response
def song_download(request):
# https://stackoverflow.com/questions/11017466/flask-to-return-image-stored-in-database
form_data = request.form
# print([form_data])
text_block_names = get_text_block_names()
song_text = {}
for text_block_name in text_block_names:
a_song_text = form_data[text_block_name].replace("\r", "")
song_text[text_block_name] = (a_song_text+"\n").split("\n")
line_count = int(form_data["NumLines"])
song_binary = gen_pro_data(text_block_names, song_text, line_count)
response = make_response(song_binary)
response.headers.set('Content-Type', 'binary/pro')
song_file_name = strip_song_name(form_data["SongName"])
response.headers.set('Content-Disposition', 'attachment', filename=f'{song_file_name}.pro')
return response
@app.route("/", methods=["GET", "POST"])
def song_input():
loaded_dict = {}
# https://www.youtube.com/watch?v=mqhxxeeTbu0&list=PLzMcBGfZo4-n4vJJybUVV3Un_NFS5EOgX&index=1
if request.method == "POST":
if "action" in request.files:
if request.files["action"]:
# load song data from json file
json_data = request.files["action"]
loaded_dict = json.load(json_data.stream)
loaded_dict["song_title"] = json_data.filename.replace(".json", "")
if "action" in request.form:
action = request.form["action"]
if action == "pro":
return song_download(request)
if action == "json":
return json_download(request)
title = ""
content = ""
text_block_names = get_text_block_names()
for name in text_block_names:
title += f"<th>{name}</th>"
for name in text_block_names:
if name in loaded_dict:
song_text = "\n".join(loaded_dict[name]).rstrip("\n")
content += f"""<td><textarea style="white-space:pre;" id ="{name}" name="{name}" rows="50" cols="50" class="linked" >{song_text}</textarea></td>\n"""
else:
content += f"""<td><textarea style="white-space:pre;" id ="{name}" name="{name}" rows="50" cols="50" class="linked" ></textarea></td>\n"""
with open("GroupNames.txt", "r") as group_names:
labels = group_names.read().split("\n")
label_string = ""
num_labels_per_line = 13
for index in range((len(labels)//num_labels_per_line)+1):
label_string += "\""
label_string += "\" \"".join(labels[index*num_labels_per_line:(index+1)*num_labels_per_line]) + "\"<br/>\n"
song_title = ""
if "song_title" in loaded_dict:
song_title = loaded_dict["song_title"]
jquiry_scripts = """ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(function(){
$('.linked').scroll(function(){
$('.linked').scrollTop($(this).scrollTop());
})
})
</script>"""
style = """ <style>
input[type=file]{
width:110px;
color:transparent;
}
textarea {
font-family: 'Courier New', monospace;
resize: none;
}
html * {
font-family: 'Sonus', 'Arial';
}
</style>"""
html = f"""<!DOCTYPE html>
<head>
{jquiry_scripts}
{style}
</head>
<body style="background-color:LightGray;">
<form action=# method="post" enctype="multipart/form-data">
<label>Song name:</label>
<input id="SongName" name="SongName" type="text" value="{song_title}" size="100">
<br/>
<br/>
<label>Number of lines per slide:</label>
<input id="NumLines" name="NumLines" type="number" value="2" min="1" max="10" required size="5">
<br/>
<table style='font-family:"Courier New"'>
<tr>{title}</tr>
<tr>{content}</tr>
</table>
<table><tr>
<td>Possible Group labels:</td><td> {label_string}</td>
</tr></table>
<br/><br/>
<button type="submit" name="action" value=pro>Download .pro</button>
<button type="submit" name="action" value=json>Download .json</button>
<br/><br/>
<label name="json_file">Reload .json file:</label>
<input type="file" name="action" value=load_json accept=".json" onchange="form.submit()" />
</form>
</body></html>"""
return html
# for local testing should be commented when used while web hosting.
if __name__ == '__main__':
app.run(debug=True)