-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (75 loc) · 2.5 KB
/
main.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
from browser import document, alert, console, ajax, window, html
from browser.template import Template
# show_alert
def show_alert(e):
alert("Hello !")
document["output-hello"].textContent = "Hello World !"
document['btn'].bind('click', show_alert)
# I/O in the same time
def show_output(e):
console.log(e.target.value)
document['output'].textContent = e.target.value
document['text'].bind('input', show_output)
# Template Variable
Template(document['hi-to']).render(name="Mohammad")
Template(document['sum']).render(a=10, b=5)
# Ajax call
url = "https://api.chucknorris.io/jokes/random"
def on_complete(req):
import json
data = json.loads(req.responseText)
joke = data['value']
document['show-joke'].textContent = joke
def get_joke(e):
req = ajax.ajax()
req.open('GET', url, True)
req.send()
document['show-joke'].textContent = "LOADING....."
req.bind('complete', on_complete)
document['joke-btn'].bind('click', get_joke)
# load file
def on_load(reader):
document['file-text'].value = reader.target.result
def file_reader(e):
file = document['file-upload'].files[0]
reader = window.FileReader.new()
reader.readAsText(file)
reader.bind('load', on_load)
document['file-upload'].bind('input', file_reader)
# Animation
box = document['rotate-box']
angle = 10
def change(e):
global angle
box.style.transform = f"rotate({angle}deg)"
angle += 10
document['rotate-btn'].bind('click', change)
# Local storage
storage = window.localStorage
if storage.getItem('item'):
document['show-saved'].textContent = storage.getItem('item')
def add_item(e):
get_item = document['get-item'].value
storage.setItem('item', get_item)
document['show-saved'].textContent = get_item
alert('You added an object in local storage')
def remove_item(e):
storage.removeItem('item')
document['show-saved'].textContent = ""
alert('You removed an object from local storage')
document['add-btn'].bind('click', add_item)
document['remove-btn'].bind('click', remove_item)
# draw with js and brython
def draw_br(e):
height = document['height'].value
width = document['width'].value
color = document['color'].value
window.draw_js(height, width, color)
document['draw-btn'].bind('click', draw_br)
# draw with js and brython
def draw_br_2(height, width, color):
shap = document['shap2']
shap.attrs['style'] = f"background-color: {color}"
shap.width = width
shap.height = height
window.draw_br_2 = draw_br_2