-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_server.py
182 lines (161 loc) · 5.83 KB
/
basic_server.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# IMPORT SECTION #
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from json import dump, load
from markdown import markdown
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from typing import Any, Dict, List, Tuple
# GENERAL FUNCTION SECTION #
def _choose_file_for_save(title: str, filetypes: List[Tuple[str, str]]) -> str:
root = Tk()
filename = asksaveasfilename(
parent=root,
title=title,
filetypes=filetypes
)
root.destroy()
return filename
def _export(json: List[Any]):
html: List[str] = []
html.append("<!DOCTYPE html>")
html.append('<html lang="en">')
html.append('<head>')
html.append(' <meta charset="utf-8">')
html.append(' <title>Export</title>')
html.append('</head>')
html.append('<body>')
current_level = 1
current_levels: List[int] = [0 for _ in range(7)]
counters: Dict[str, int] = {}
for widget in json:
id = widget["id"]
type = widget["type"]
data = widget["data"]
if type == "canvas":
# base64
html.append('<img src="'+data["base64"]+'" alt="Drawn canvas image"/>')
elif type == "caption":
# text
# id
# level
current_level = int(data["level"])
current_levels[current_level-1] += 1
level_text = ".".join([str(x) for x in current_levels[0:current_level]])
h_text = f"h{current_level}"
caption = f'<{h_text} id="{data["id"]}">{level_text} {data["text"]}</{h_text}>'
html.append(caption)
elif type == "counter":
# family
# reference
# br
if data["family"] not in counters.keys():
counters[data["family"]] = 1
else:
counters[data["family"]] += 1
if data["br"]:
html.append("<br>")
if data["reference"] != "":
counter = f'<div id="{data["reference"]}">'
else:
counter = "<div>"
counter += f'{data["family"]} {counters[data["family"]]}</div>'
html.append(counter)
elif type == "text":
# text
# bordercolor
# font
p_element = "<p"
p_style = ' style="font-family: '
if data["font"] == "standard":
p_style += 'serif;'
elif data["font"] == "monospaced":
p_style += 'monospace;'
if data["bordercolor"] != "none":
p_style += f' border: 1px solid {data["bordercolor"]};'
p_style += '"'
p_element += p_style + ">"
print(p_element)
html.append(p_element)
text = data["text"]
# Counter to HTML
text.replace("{COUNTER{", '<a href="#')
text.replace("}COUNTER}", '>↕</a>')
# Caption to HTML
text.replace("{CAPTION{", '<a href="#')
text.replace("}CAPTION}", '>↕</a>')
# Markdown to HTML using markdown package
try:
# Remove initial and terminal <p> tag which
# is added by the markdown function
text = markdown(text)[3:-4]
except Exception:
text = markdown(text)
html.append(text)
html.append("</p>")
html.append('</body>')
html.append("</html>")
html_str = "\n".join(html)
print(html_str)
filename = _choose_file_for_save("Save as HTML", [("HTML", "*.html"), ("all files","*.*")])
if filename != "":
with open(filename, "w") as f:
f.write(html_str)
# SERVER SETTINGS SECTION #
async_mode = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
# SOCKET.IO-RELATED FUNCTIONS SECTION #
@app.route('/')
def index():
return render_template('index.html',
sync_mode=socketio.async_mode)
@socketio.on("broadcastToServer")
def handle_broadcast_to_server(json):
print("SOCKET.IO: Received 'broadcastToServer' event")
emit('broadcastToClient', json, broadcast=True)
@socketio.on("export")
def handle_export(json):
print("SOCKET.IO: Received 'export' event")
_export(json)
@socketio.on("load")
def handle_load(empty):
print("SOCKET.IO: Received 'load' event")
root = Tk()
filename = askopenfilename(
parent=root,
title="Select JSON",
filetypes=[("JSON", "*.json"), ("all files","*.*")]
)
root.destroy()
if len(filename) > 0:
with open(filename, "r") as f:
json = load(f)
print("SOCKET.IO: Emitting 'broadcastToClient' event")
emit('broadcastToClient', json, broadcast=True)
@socketio.on("save")
def handle_save(json):
print("SOCKET.IO: Received 'save' event")
filename = _choose_file_for_save("Save JSON", [("JSON", "*.json"), ("all files","*.*")])
print(f"Saving to {filename}...")
if len(filename) > 0:
with open(filename, "w") as f:
dump(json, f, indent=6)
# MAIN ROUTINE SECTION #
if __name__ == '__main__':
selection = input("Do you want to propagate the server in your whole network\n"
"so that it can be accessed by other network devices\n"
"(WARNING: If you cannot fully trust the network, this\n"
" may pose an additional security risk!)? [type in Y for yes,\n"
"and any other symbol for no, followed by pressing ENTER] ")
# We use the address 0.0.0.0 in order to propagate
# the server in our local network. If you only want
# to use the server on your hosting device only, use
# the address 127.0.0.1.
# Flask's default port number is 5000.
if selection == "Y":
host = "0.0.0.0"
else:
host = "127.0.0.1"
socketio.run(app, host=host, port=5000)