-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.py
87 lines (65 loc) · 1.77 KB
/
form.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
import typing
from dataclasses import dataclass
from flask import Flask, request
from py2js import js
from py2html.elements import body, button, div, head, html, input, title
from py2html.ext import SASS, Script, util
app = Flask(__name__)
this: object
window: object
fetch: object
JSON: object
class document:
def getElementById(self, id: str): ...
@dataclass
class Message:
ip: str
content: str
messages = []
class MessageData(typing.TypedDict):
content: str
@util.api(app, '/api/message', methods=['POST'])
def send_message():
data: MessageData = request.json
messages.append(Message(request.remote_addr, data['content']))
return {}
def message_component(message: Message):
return div(className='message')[
div(className='username')[
message.ip
],
message.content
]
@app.route('/')
# @util.timer
def index():
@js
def main():
async def button_clicked():
message = document.getElementById('message')
await send_message({
'content': message.value
})
message.value = ''
window.location.reload()
return html(lang='en')[
util.cache(lambda: head[
title['Form'],
SASS(href='styles_form.scss'),
Script[send_message],
Script[main]
]),
body[
div[
div[
input(id='message', name='message', placeholder='...'),
button(onclick='button_clicked()')['>']
],
div(className='message_container')[
*map(message_component, messages)
]
]
]
].render()
if __name__ == '__main__':
app.run(debug=True, port=80)